signal-gateway-dockerized/sender/sender.py

67 lines
1.3 KiB
Python

#!/usr/bin/python3
# Reads messages from RabbitMQ and sends them to given destination by calling signal-cli-api
import pika
import json
import os
import sys
sys.path.append("./pysignald")
from pysignald import Signal
rabbitmq_host = os.environ.get('RABBITMQ_HOST')
SOCKET_PATH = os.environ.get('SOCKET_PATH')
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_host))
channel = connection.channel()
channel.queue_declare(queue='signal-send')
def get_registrations():
regfile = open('./registrations.json')
regs = json.load(regfile)
return regs
def send_message(text, sender, destination):
regs = get_registrations()
sender_found = False
for reg in regs:
if sender in reg['id']:
sender_id = reg['id']
sender_found = True
if sender_found:
s = Signal(sender_id, socket_path=SOCKET_PATH)
s.send_message(destination, text)
return True
else:
print('Sender not in registrations.json')
return False
def callback(ch, method, properties, body):
body = json.loads(body)
text = body[0]
sender = body[1]
destination = body[2]
if send_message(text, sender, destination):
channel.basic_ack(delivery_tag=method.delivery_tag)
return True
channel.basic_consume('signal-send', callback, auto_ack=False)
try:
channel.start_consuming()
except KeyboardInterrupt:
channel.stop_consuming()
connection.close()