Update to use jsonrpc

This commit is contained in:
Théophile Bastian 2024-03-01 13:59:37 +01:00
parent 46a83d1c23
commit b335b52b7f
4 changed files with 20 additions and 18 deletions

View file

@ -5,7 +5,7 @@ Provide ways to send notifications to signal-cli through webhooks
## Install
```bash
virtualenv -p python3 --system-site-packages venv
virtualenv -p python3 venv
source venv/bin/activate
pip install -r requirements.txt
@ -13,6 +13,3 @@ pip install -r requirements.txt
cp signal_webhook/configuration.sample.py signal_webhook/configuration.py
$EDITOR signal_webhook/configuration.py
```
**Beware!** Sending messages to Signal requires `gi`, which cannot be installed
in a virtualenv; hence the need for `--system-site-packages`.

View file

@ -1,2 +1 @@
Flask
pydbus

View file

@ -3,3 +3,6 @@ RECIPIENTS = {"foo": ["+42..."]} # FIXME
# Recipient from the dict above to send to by default
DEFAULT_RECIPIENT = "foo" # FIXME
# Unix socket location
SIGNAL_SOCKET = "/run/signal-cli/socket"

View file

@ -1,19 +1,22 @@
import logging
from pydbus import SystemBus
from gi.repository.GLib import GError
from . import configuration
import socket
import random
import json
logger = logging.getLogger(__name__)
def signal_send(recipients: list[str], message: str) -> None:
try:
bus = SystemBus()
signal_bus = bus.get("org.asamk.Signal")
for recipient in recipients:
signal_bus.sendMessage(
message,
[],
recipient,
)
except GError as exn:
logger.error("Cannot send Signal notification: %s", exn)
payload = {
"jsonrpc": "2.0",
"method": "send",
"params": {
"message": message,
"recipient": recipients,
},
"id": random.randint(0, (1 << 24)),
}
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as signal_json:
signal_json.connect(configuration.SIGNAL_SOCKET)
signal_json.send(json.dumps(payload).encode("utf-8"))