aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNiklas Halle <niklas@niklashalle.net>2020-09-18 17:32:40 +0200
committerNiklas Halle <niklas@niklashalle.net>2020-09-18 17:32:40 +0200
commit89d9928336b2e675ce19ec27d29dbd0ac212c08a (patch)
treef7eef335350bbdf0ef30dd2601302f4d493fde03
parent6a82d692200606a25054857739c71f54db812be9 (diff)
downloadn_frontend_signal-89d9928336b2e675ce19ec27d29dbd0ac212c08a.tar.gz
n_frontend_signal-89d9928336b2e675ce19ec27d29dbd0ac212c08a.zip
handle attachments (maybe?)
m---------libs/pysignald0
-rwxr-xr-xmain.py38
2 files changed, 26 insertions, 12 deletions
diff --git a/libs/pysignald b/libs/pysignald
-Subproject a4c7361fdaa91c805081abfd7f593244fc00821
+Subproject 64a3de18188aae3c9376bb50a542ee26ca1bb65
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()