phantasmobile/app/signalbot/rpc.py

55 lines
1.1 KiB
Python
Raw Normal View History

2023-07-24 11:05:45 +00:00
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)