phantasmobile/app/signalbot/cli.py

55 lines
1.3 KiB
Python
Raw Normal View History

2023-07-24 11:05:45 +00:00
import json
import subprocess
from django.conf import settings
BASE_CMD = ['signal-cli', '-a', settings.SIGNAL_ACCOUNT, '--output=json']
def send(msg, to=None, group=None):
cmd = BASE_CMD + [
'send', '--message-from-stdin'
]
if group:
cmd += ['-g', group]
else:
cmd += [to]
r = subprocess.check_output(cmd, input=msg, encoding='utf-8')
response = []
if r:
for row in r.strip().split('\n'):
response.append(json.loads(row))
return response
def send_reaction(target_address, target_ts, emoji, to=None, group=None, remove=False):
cmd = BASE_CMD + [
'sendReaction', '-t', str(target_ts), '-e', emoji,
'-a', target_address
]
if remove:
cmd += ['-r']
if group:
cmd += ['-g', group]
else:
cmd += [to]
r = subprocess.check_output(cmd, encoding='utf-8')
response = []
if r:
for row in r.strip().split('\n'):
response.append(json.loads(row))
return response
def receive(timeout=1):
cmd = BASE_CMD + [
'receive', '--timeout', str(timeout), '--send-read-receipts'
]
r = subprocess.check_output(cmd, encoding='utf-8')
response = []
if r:
for row in r.strip().split('\n'):
response.append(json.loads(row))
return response