From 9b149064624a8d15378b5e22123d835c74a7ad36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Fri, 1 Mar 2024 13:59:37 +0100 Subject: [PATCH] Update to use jsonrpc --- README.md | 5 +---- requirements.txt | 1 - signal_webhook/configuration.sample.py | 3 +++ signal_webhook/signal.py | 30 ++++++++++++++------------ 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 1b41ca8..e57c31e 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/requirements.txt b/requirements.txt index 94f992c..e3e9a71 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ Flask -pydbus diff --git a/signal_webhook/configuration.sample.py b/signal_webhook/configuration.sample.py index 8869e32..d993ff1 100644 --- a/signal_webhook/configuration.sample.py +++ b/signal_webhook/configuration.sample.py @@ -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 = "/var/signal-cli/socket" diff --git a/signal_webhook/signal.py b/signal_webhook/signal.py index 497318a..a58a7e6 100644 --- a/signal_webhook/signal.py +++ b/signal_webhook/signal.py @@ -1,19 +1,21 @@ 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) + client.send(json.dumps(payload))