c3lf-system-3/core/notifications/dispatch.py

81 lines
3.4 KiB
Python
Raw Normal View History

2024-04-26 13:18:41 +00:00
from aiohttp.client import ClientSession
2024-04-25 21:45:57 +00:00
from channels.layers import get_channel_layer
2024-04-25 23:35:12 +00:00
from channels.db import database_sync_to_async
2024-04-26 13:18:41 +00:00
from urllib.parse import quote as urlencode
from core.settings import TELEGRAM_BOT_TOKEN, TELEGRAM_GROUP_CHAT_ID
2024-05-02 20:37:34 +00:00
from mail.protocol import send_smtp, make_notification
2024-05-03 21:34:47 +00:00
from notifications.models import UserNotificationChannel
2024-05-03 23:03:51 +00:00
from notifications.templates import render_notification_new_ticket_async, render_notification_reply_ticket_async
2024-05-03 21:58:38 +00:00
from tickets.models import IssueThread
2024-04-26 13:18:41 +00:00
async def http_get(url):
async with ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def telegram_notify(message, chat_id):
encoded_message = urlencode(message)
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage?chat_id={chat_id}&text={encoded_message}"
return await http_get(url)
2024-04-25 21:45:57 +00:00
2024-05-02 20:37:34 +00:00
async def email_notify(message, email):
2024-05-02 20:53:13 +00:00
mail = make_notification(message, email)
2024-05-02 20:37:34 +00:00
await send_smtp(mail)
2024-04-25 21:45:57 +00:00
class NotificationDispatcher:
channel_layer = None
2024-04-25 23:22:08 +00:00
room_group_name = "general"
2024-04-25 21:45:57 +00:00
def __init__(self):
2024-04-25 23:22:08 +00:00
self.channel_layer = get_channel_layer('default')
2024-04-25 22:56:12 +00:00
if not self.channel_layer:
raise Exception("Could not get channel layer")
2024-04-25 21:45:57 +00:00
2024-04-25 23:35:12 +00:00
@database_sync_to_async
def get_notification_targets(self):
2024-04-26 13:18:41 +00:00
channels = UserNotificationChannel.objects.filter(active=True)
return list(channels)
2024-04-25 23:35:12 +00:00
2024-05-03 21:58:38 +00:00
@database_sync_to_async
def get_ticket(self, ticket_id):
2024-05-03 22:47:32 +00:00
return IssueThread.objects.filter(id=ticket_id).select_related('event').first()
2024-05-03 21:58:38 +00:00
2024-04-25 21:45:57 +00:00
async def run_forever(self):
# Infinite loop to continuously listen for messages
2024-04-25 22:31:34 +00:00
print("Listening for messages...")
2024-04-25 23:22:08 +00:00
channel_name = await self.channel_layer.new_channel()
await self.channel_layer.group_add(self.room_group_name, channel_name)
print("Channel name:", channel_name)
2024-04-25 21:45:57 +00:00
while True:
# Blocking receive to get the message from the channel layer
2024-04-25 23:22:08 +00:00
message = await self.channel_layer.receive(channel_name)
2024-04-25 21:45:57 +00:00
2024-04-25 23:28:44 +00:00
if (message and 'type' in message and message['type'] == 'generic.event' and 'name' in message and
message['name'] == 'user_notification'):
2024-05-03 21:58:38 +00:00
if 'ticket_id' in message and 'event_id' in message and 'new' in message:
ticket = await self.get_ticket(message['ticket_id'])
await self.dispatch(ticket, message['event_id'], message['new'])
2024-04-25 23:35:12 +00:00
else:
print("Error: Invalid message format")
2024-04-25 22:56:12 +00:00
2024-05-03 21:34:47 +00:00
async def dispatch(self, ticket, event_id, new):
2024-05-03 23:03:51 +00:00
message = await render_notification_new_ticket_async(
ticket) if new else await render_notification_reply_ticket_async(ticket)
2024-04-25 23:28:44 +00:00
print("Dispatching message:", message, "with event_id:", event_id)
2024-04-25 23:35:12 +00:00
targets = await self.get_notification_targets()
2024-05-03 22:47:32 +00:00
# await telegram_notify(message, TELEGRAM_GROUP_CHAT_ID)
2024-04-25 23:35:12 +00:00
for target in targets:
2024-04-26 13:18:41 +00:00
if target.channel_type == 'telegram':
2024-05-02 21:02:49 +00:00
print("Sending telegram notification to:", target.channel_target)
2024-04-26 13:18:41 +00:00
await telegram_notify(message, target.channel_target)
2024-04-26 13:56:15 +00:00
elif target.channel_type == 'email':
2024-05-02 21:02:49 +00:00
print("Sending email notification to:", target.channel_target)
2024-05-02 20:37:34 +00:00
await email_notify(message, target.channel_target)
2024-04-26 13:18:41 +00:00
else:
print("Unknown channel type:", target.channel_type)