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 ## Install
```bash ```bash
virtualenv -p python3 --system-site-packages venv virtualenv -p python3 venv
source venv/bin/activate source venv/bin/activate
pip install -r requirements.txt pip install -r requirements.txt
@ -13,6 +13,3 @@ pip install -r requirements.txt
cp signal_webhook/configuration.sample.py signal_webhook/configuration.py cp signal_webhook/configuration.sample.py signal_webhook/configuration.py
$EDITOR 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 Flask
pydbus

View file

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

View file

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