diff options
Diffstat (limited to 'main.py')
-rwxr-xr-x | main.py | 25 |
1 files changed, 19 insertions, 6 deletions
@@ -2,8 +2,10 @@ import os import sys +import base64 import getopt import requests +import tempfile from libs.pysignald.signald import Signal from configparser import ConfigParser @@ -41,7 +43,7 @@ def handle_replies(replies): l_answer = reply['text'] for annotation in reply["annotations"]: a_type = annotation["type"] - a_extra = handle_replies(annotation["extra"]) + a_extra, is_attachment = handle_replies(annotation["extra"]) if a_type == "command": l_answer = f"/{l_answer}: {a_extra}\n" @@ -53,8 +55,19 @@ def handle_replies(replies): l_answer = f"_{l_answer}_" elif a_type == "strikethrough": l_answer = f"~{l_answer}~" + elif a_type == "attachment": + [file_name, file_type, file_content] = a_extra.split(';') + try: + tmp_path = tempfile.TemporaryDirectory() + file_content = base64.b64decode(file_content) + file_path = tmp_path.name + '/' + file_name + with open(file_path, "wb") as f: + f.write(file_content) + return answer, True + except Exception as e: + print(str(e)) answer += l_answer - return answer + return answer, False def handle_response(response): @@ -76,9 +89,9 @@ def startup(number): response = requests.post('http://localhost:18080', json={"command": match.group(1), "arguments": match.group(2).strip()}) - answer = handle_response(response) + answer, is_attachment = handle_response(response) - return stop, answer + return stop, answer, is_attachment # no args @s.chat_handler("^/(.+)$", order=20) # This is case-insensitive. @@ -91,9 +104,9 @@ def startup(number): response = requests.post('http://localhost:18080', json={"command": match.group(1), "arguments": ""}) - answer = handle_response(response) + answer, is_attachment = handle_response(response) - return stop, answer + return stop, answer, is_attachment s.run_chat() |