signal backend, app cleanup
This commit is contained in:
parent
4b157ed1d1
commit
6f18890739
43 changed files with 695 additions and 124 deletions
0
app/signalbot/__init__.py
Normal file
0
app/signalbot/__init__.py
Normal file
3
app/signalbot/admin.py
Normal file
3
app/signalbot/admin.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
app/signalbot/apps.py
Normal file
6
app/signalbot/apps.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class SignalbotConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "app.signalbot"
|
||||
54
app/signalbot/cli.py
Normal file
54
app/signalbot/cli.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
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
|
||||
41
app/signalbot/daemon.py
Normal file
41
app/signalbot/daemon.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import subprocess
|
||||
import json
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
|
||||
from . import cli
|
||||
from . import rpc
|
||||
|
||||
|
||||
def main():
|
||||
cmd = cli.BASE_CMD + [
|
||||
'daemon', '--http', '--send-read-receipts'
|
||||
]
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1, universal_newlines=True, start_new_session=True)
|
||||
try:
|
||||
for line in p.stdout:
|
||||
try:
|
||||
msg = json.loads(line)
|
||||
except:
|
||||
if settings.DEBUG:
|
||||
print(">>", line)
|
||||
continue
|
||||
if settings.DEBUG:
|
||||
print('==', msg)
|
||||
source = msg.get('envelope', {}).get('sourceNumber')
|
||||
reaction = msg.get('envelope', {}).get('dataMessage', {}).get('reaction', {})
|
||||
emoji = reaction.get('emoji')
|
||||
target_author = reaction.get('targetAuthorNumber')
|
||||
target_ts = reaction.get('targetSentTimestamp')
|
||||
group = msg.get('envelope', {}).get('dataMessage', {}).get("groupInfo", {}).get("groupId")
|
||||
if group == settings.SIGNAL_MODERATORS_GROUP:
|
||||
if emoji == "👍" and target_author == settings.SIGNAL_ACCOUNT:
|
||||
if source in settings.SIGNAL_MODERATORS:
|
||||
from ..item import models
|
||||
now = timezone.now()
|
||||
models.Comment.objects.filter(data__moderator_ts=target_ts).update(published=now)
|
||||
rpc.send_reaction(target_author, target_ts, "🎉", group=group)
|
||||
else:
|
||||
rpc.send("Ignoring your request, you are not a moderator", group=group)
|
||||
except:
|
||||
p.kill()
|
||||
14
app/signalbot/management/commands/signal-daemon.py
Normal file
14
app/signalbot/management/commands/signal-daemon.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
|
||||
from ... import daemon
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'process incoming singal messages'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
pass
|
||||
|
||||
def handle(self, *args, **options):
|
||||
daemon.main()
|
||||
|
||||
0
app/signalbot/migrations/__init__.py
Normal file
0
app/signalbot/migrations/__init__.py
Normal file
3
app/signalbot/models.py
Normal file
3
app/signalbot/models.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
54
app/signalbot/rpc.py
Normal file
54
app/signalbot/rpc.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import json
|
||||
import subprocess
|
||||
|
||||
import requests
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
rpc_id = 1
|
||||
|
||||
|
||||
def api(method, params):
|
||||
global rpc_id
|
||||
rpc_id += 1
|
||||
url = "http://127.0.0.1:8080/api/v1/rpc"
|
||||
msg = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": str(rpc_id),
|
||||
"method": method,
|
||||
}
|
||||
if params:
|
||||
msg["params"] = params
|
||||
if settings.DEBUG:
|
||||
print("POST signal rpc:", msg)
|
||||
response = requests.post(url, json=msg).json()
|
||||
if "result" in response:
|
||||
return response["result"]
|
||||
else:
|
||||
raise Exception("Error: %s", response)
|
||||
|
||||
|
||||
def send(msg, to=None, group=None):
|
||||
params = {
|
||||
"message": msg
|
||||
}
|
||||
if group:
|
||||
params["groupId"] = group
|
||||
else:
|
||||
params["recipient"] = to
|
||||
return api("send", params)
|
||||
|
||||
|
||||
def send_reaction(target_address, target_ts, emoji, to=None, group=None, remove=False):
|
||||
params = {
|
||||
"emoji": emoji,
|
||||
"targetTimestamp": target_ts,
|
||||
"targetAuthor": target_address,
|
||||
}
|
||||
if group:
|
||||
params["groupId"] = group
|
||||
else:
|
||||
params["recipient"] = to
|
||||
|
||||
return api("sendReaction", params)
|
||||
2
app/signalbot/tasks.py
Normal file
2
app/signalbot/tasks.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
|
||||
3
app/signalbot/tests.py
Normal file
3
app/signalbot/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
3
app/signalbot/views.py
Normal file
3
app/signalbot/views.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
Loading…
Add table
Add a link
Reference in a new issue