Added ntfy router #2

Squashed commit of the following:

commit 3ba0c9b4c6
Author: Ashish D'Souza <[email protected]>
Date:   Sun Nov 19 05:21:27 2023 -0600

    Another bugfix

commit 8a76bddbf1
Author: Ashish D'Souza <[email protected]>
Date:   Sun Nov 19 05:12:40 2023 -0600

    Added ntfy router to ansible scripts

commit 7b51846e1b
Author: Ashish D'Souza <[email protected]>
Date:   Sun Nov 19 05:12:14 2023 -0600

    Fixed inconsistent variable names bug

commit a96a14e261
Author: Ashish D'Souza <[email protected]>
Date:   Sun Nov 19 04:53:09 2023 -0600

    Added ntfy router to docker compose

commit d2f30faa5e
Author: Ashish D'Souza <[email protected]>
Date:   Sun Nov 19 04:52:07 2023 -0600

    Added ntfy routing
This commit is contained in:
ashish committed 2023-11-19 06:17:01 -06:00
1 parent 1640a7a590
commit a14ae8581a
9 files changed
+219

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
import json
import asyncio
import pika
from pika.adapters.asyncio_connection import AsyncioConnection
class RabbitMQPublisher:
def __init__(self, rabbitmq_host: str, rabbitmq_port: int):
self._connection = AsyncioConnection(
pika.ConnectionParameters(host=rabbitmq_host, port=rabbitmq_port),
on_open_callback=self._on_connection_open,
on_open_error_callback=self._on_connection_open_error,
on_close_callback=self._on_connection_closed,
)
self._channel = None
def _on_connection_open(self, connection: AsyncioConnection) -> None:
self._connection.channel(on_open_callback=self._on_channel_open)
def _on_connection_open_error(self, connection: AsyncioConnection, error: Exception) -> None:
print(f'Failed to open connection')
self._connection.ioloop.stop()
raise error
def _on_connection_closed(self, connection: AsyncioConnection, reason: Exception) -> None:
print(f'Connection closed')
raise reason
self._connection.ioloop.stop()
def _on_channel_open(self, channel: pika.channel.Channel) -> None:
self._channel = channel
def _publish(self, topic: str, data: str) -> None:
self._channel.basic_publish(exchange='broadcast', routing_key=topic, body=data)
def send_notification(self, system: str, user: str, priority: str, message: str) -> None:
self._publish(f'notification.{system}.{user}.{priority}', message)
def send_call(self, caller: str, user: str, message: str) -> None:
self._publish(f'call.{caller}.{user}', json.dumps({
'ringtone': {
'filename': 'Magic.mp3',
'trim': {
'start_time': '00:01:34.64',
'end_time': '00:01:41.53'
}
},
'message': message
}))