2022-03-22 14:15:39 +01:00
|
|
|
import logging
|
2024-03-01 13:59:37 +01:00
|
|
|
from . import configuration
|
|
|
|
import socket
|
|
|
|
import random
|
|
|
|
import json
|
2022-03-22 14:15:39 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-08-14 09:21:49 +02:00
|
|
|
def signal_send(recipients: list[str], message: str) -> None:
|
2024-03-01 15:01:54 +01:00
|
|
|
def to_dest(recipient: str) -> dict[str, str]:
|
|
|
|
if recipient.startswith("G:"):
|
|
|
|
return {"groupId": recipient[2:]}
|
2024-03-01 15:08:22 +01:00
|
|
|
return {"recipient": [recipient]}
|
2024-03-01 15:01:54 +01:00
|
|
|
|
2024-03-01 13:59:37 +01:00
|
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as signal_json:
|
|
|
|
signal_json.connect(configuration.SIGNAL_SOCKET)
|
2024-03-01 15:01:54 +01:00
|
|
|
for recipient in recipients:
|
|
|
|
payload = {
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "send",
|
|
|
|
"params": {"message": message, **to_dest(recipient)},
|
|
|
|
"id": random.randint(0, (1 << 24)),
|
|
|
|
}
|
2024-03-01 15:05:08 +01:00
|
|
|
str_payload = json.dumps(payload).encode("utf-8")
|
|
|
|
logger.debug("Sending payload: %s", str_payload)
|
|
|
|
signal_json.send(str_payload)
|
2024-03-01 15:17:39 +01:00
|
|
|
logger.info("Sent messages to %s", ", ".join(recipients))
|