From 89d9928336b2e675ce19ec27d29dbd0ac212c08a Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Fri, 18 Sep 2020 17:32:40 +0200 Subject: handle attachments (maybe?) --- libs/pysignald | 2 +- main.py | 38 ++++++++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/libs/pysignald b/libs/pysignald index a4c7361..64a3de1 160000 --- a/libs/pysignald +++ b/libs/pysignald @@ -1 +1 @@ -Subproject commit a4c7361fdaa91c805081abfd7f593244fc008217 +Subproject commit 64a3de18188aae3c9376bb50a542ee26ca1bb65c diff --git a/main.py b/main.py index ebb03aa..f542c0e 100755 --- a/main.py +++ b/main.py @@ -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 @@ -36,22 +38,34 @@ def register_signald(number): def handle_replies(replies): answer = "" for reply in replies: - answer = reply['text'] + 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": - answer = f"/{answer}: {a_extra}\n" + l_answer = f"/{l_answer}: {a_extra}\n" elif a_type == "link": - answer = f"{a_extra} ({answer})" + l_answer = f"{a_extra} ({l_answer})" elif a_type == "bold": - answer = f"*{answer}*" + l_answer = f"*{l_answer}*" elif a_type == "italic": - answer = f"_{answer}_" + l_answer = f"_{l_answer}_" elif a_type == "strikethrough": - answer = f"~{answer}~" - return answer + 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, False def handle_response(response): @@ -73,9 +87,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. @@ -88,9 +102,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() -- cgit v1.2.3-54-g00ecf