aboutsummaryrefslogtreecommitdiffstats
path: root/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'main.py')
-rwxr-xr-xmain.py38
1 files changed, 26 insertions, 12 deletions
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()