signal-gateway-dockerized/receiver/receiver.py

80 lines
1.8 KiB
Python
Executable file

#!/usr/bin/python3
# Makes a periodic requests to signal-cli-api and writes incoming messages to RabbitMQ
import sys
sys.path.append("./pysignald")
from pysignald import Signal
import json
import pika
import os
from threading import Thread
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-receive')
connection.close()
def get_registrations():
regfile = open('./registrations.json')
regs = json.load(regfile)
return regs
def save_message(message):
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_host))
channel = connection.channel()
channel.queue_declare(queue='signal-receive')
message_json = json.dumps(message)
channel.basic_publish(exchange='',
routing_key='signal-receive',
body=message_json)
connection.close()
def get_messages(regid):
s = Signal(regid, socket_path=SOCKET_PATH)
for message in s.receive_messages():
# For testing purposes, just echo message back
# Example message
# Message(username='+420777811038', source={'number': '+420606130958'}, text='Now it works!', source_device=1, timestamp=1601302777104, timestamp_iso='2020-09-28T14:19:37.104Z', expiration_secs=0, attachments=[], quote=None, group_info={})
msg = [
message.text,
message.timestamp,
message.source['number'],
message.username
]
save_message(msg)
def main():
#rabbitmq_connect()
regs = get_registrations()
if regs:
for reg in regs:
regid = reg["id"]
print("Starting thread for number " + regid)
Thread(target=get_messages,args=(regid,)).start()
else:
print("Missing registration list.")
connection.close()
sys.exit(1)
if __name__ == "__main__":
main()