xat/mastobot.py

210 líneas
4,9 KiB
Python
Original Vista normal Històric

2023-01-22 17:28:40 +01:00
from setup import Setup
from xatapi import Xat
from mastodon import Mastodon
2023-01-27 13:16:10 +01:00
from post import Post
2023-01-22 17:28:40 +01:00
import re
import time
import sys
2023-01-24 11:39:08 +01:00
import os
import requests
import base64
2023-01-22 17:28:40 +01:00
import pdb
def cleanhtml(raw_html):
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', raw_html)
return cleantext
def unescape(s):
s = s.replace("&apos;", "'")
2023-01-26 13:11:36 +01:00
s = s.replace("&#39;", "'")
2023-01-22 17:28:40 +01:00
return s
def replying():
reply = False
content = cleanhtml(text)
content = unescape(content)
try:
start = content.index("@")
end = content.index(" ")
if len(content) > end:
content = content[0: start:] + content[end +1::]
neteja = content.count('@')
i = 0
while i < neteja :
start = content.rfind("@")
end = len(content)
content = content[0: start:] + content[end +1::]
i += 1
question = content.lower()
query_word = question
reply = True
return (reply, query_word)
except ValueError as v_error:
print(v_error)
query_word = ''
return (reply, query_word)
if __name__ == '__main__':
setup = Setup()
xat = Xat()
mastodon = Mastodon(
access_token = setup.mastodon_app_token,
api_base_url= setup.mastodon_hostname
)
bot_id = mastodon.me().id
notifications = mastodon.notifications()
if len(notifications) == 0:
print('No mentions')
sys.exit(0)
for notif in notifications:
post_array = []
2023-01-22 17:28:40 +01:00
notification_id = notif.id
if notif.type != 'mention':
print(f'dismissing notification {notification_id}')
mastodon.notifications_dismiss(notification_id)
continue
account_id = notif.account.id
username = notif.account.acct
status_id = notif.status.id
text = notif.status.content
visibility = notif.status.visibility
reply, query_word = replying()
if reply == True:
content = cleanhtml(text)
search_char = '@'
counter = len([element for element in content.split() if search_char in element])
if counter > 1:
print(f'Notification {notification_id} dissmised')
mastodon.notifications_dismiss(notification_id)
continue
2023-01-26 13:11:36 +01:00
prompt = unescape(query_word)
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
if prompt[:7] == 'imatge:':
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
try:
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
imatge = xat.create_image(prompt=prompt[7:])
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
if not 'error' in imatge:
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
with open("images/imatge.png", "wb") as fh:
fh.write(base64.b64decode(imatge['data'][0]['b64_json']))
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
post_text = f'@{username} {prompt[8:]}'
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
image_id = mastodon.media_post('images/imatge.png', "image/png", description=prompt[8:]).id
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
mastodon.status_post(post_text, in_reply_to_id=status_id, media_ids={image_id}, visibility=visibility)
2023-01-22 17:28:40 +01:00
mastodon.notifications_dismiss(notification_id)
2023-01-24 11:39:08 +01:00
else:
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
post_text = f'@{username} {prompt[8:]}'
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
post_text += f"\n\n{imatge['error']['message']}"
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
mastodon.status_post(post_text, in_reply_to_id=status_id, visibility=visibility)
2023-01-22 17:28:40 +01:00
mastodon.notifications_dismiss(notification_id)
2023-01-24 11:39:08 +01:00
except:
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
pass
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
post_text = f'@{username} {prompt[8:]}\n\n{imatge}'
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
mastodon.status_post(post_text, in_reply_to_id=status_id, visibility=visibility)
2023-01-22 17:28:40 +01:00
print(f'Notification {notification_id} replied')
mastodon.notifications_dismiss(notification_id)
2023-01-24 11:39:08 +01:00
else:
2023-01-22 17:28:40 +01:00
2023-01-27 13:16:10 +01:00
post = Post()
2023-01-24 11:39:08 +01:00
answer = xat.completions(prompt=prompt)
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
answer = answer.choices[0]['text']
2023-01-22 17:28:40 +01:00
2023-01-24 11:39:08 +01:00
if 'com.example.demo.controller' not in answer:
2023-01-27 13:16:10 +01:00
post_array = post.split(answer, username)
2023-01-24 11:39:08 +01:00
i = 0
while i < len(post_array):
print(f'array element: {i} of {len(post_array)}, username: {username}, status_id: {status_id}, post: {post_array[i]}')
2023-01-24 11:39:08 +01:00
post_id = mastodon.status_post(post_array[i], in_reply_to_id=status_id, visibility=visibility).id
2023-01-24 11:39:08 +01:00
2023-01-27 13:16:10 +01:00
status_id = post_id
2023-01-22 17:28:40 +01:00
i += 1
2023-01-22 17:28:40 +01:00
print(f'Notification {notification_id} replied')
2023-01-22 17:28:40 +01:00
mastodon.notifications_dismiss(notification_id)
if isinstance(post, Post):
post.clear()
time.sleep(1)
continue
2023-01-22 17:28:40 +01:00
else:
print(f'Notification {notification_id} dissmised')
mastodon.notifications_dismiss(notification_id)