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:]}
|
|
|
|
return {"userId": recipient}
|
|
|
|
|
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)),
|
|
|
|
}
|
|
|
|
signal_json.send(json.dumps(payload).encode("utf-8"))
|