notifications
This commit is contained in:
20
daemons/management/commands/email_sender.py
Normal file
20
daemons/management/commands/email_sender.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from django.core.mail import send_mail
|
||||
|
||||
from Sprint.settings import EMAIL_HOST_USER
|
||||
from SprintLib.queue import MessagingSupport
|
||||
|
||||
|
||||
class Command(MessagingSupport):
|
||||
help = "starts file email sender"
|
||||
queue_name = "email"
|
||||
|
||||
def process(self, payload: dict):
|
||||
subject = payload['subject']
|
||||
message = payload['message']
|
||||
email = payload['email']
|
||||
send_mail(
|
||||
subject,
|
||||
message,
|
||||
EMAIL_HOST_USER,
|
||||
[email]
|
||||
)
|
@@ -1,20 +1,53 @@
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from SprintLib.queue import MessagingSupport
|
||||
from daemons.management.commands.bot import bot
|
||||
from Main.models import Solution, Progress
|
||||
from SprintLib.queue import MessagingSupport, send_to_queue
|
||||
|
||||
|
||||
class Command(MessagingSupport):
|
||||
help = "starts file notification manager"
|
||||
queue_name = "notifications"
|
||||
queue_name = "notification"
|
||||
|
||||
def handle_solution(self, payload):
|
||||
solution = Solution.objects.get(id=payload["solution_id"])
|
||||
user = solution.user
|
||||
if user.userinfo.notification_solution_result:
|
||||
message = f"Задача: {solution.task.name}\n" \
|
||||
f"Результат: {solution.result}\n" \
|
||||
f"Очки решения: {Progress.by_solution(solution).score}\n" \
|
||||
f"Текущий рейтинг: {solution.user.userinfo.rating}"
|
||||
if user.userinfo.telegram_chat_id is not None:
|
||||
yield "telegram", {"chat_id": user.userinfo.telegram_chat_id, "text": message}
|
||||
if user.email:
|
||||
yield "email", {"subject": "Тестирование завершено", "message": message, "email": user.email}
|
||||
|
||||
def handle_friends_add(self, payload):
|
||||
user = User.objects.get(id=payload['to_user_id'])
|
||||
from_user = User.objects.get(id=payload['from_user_id'])
|
||||
if user.userinfo.notification_friends:
|
||||
message = f"Пользователь {from_user.username} хочет добавить тебя в друзья"
|
||||
if user.userinfo.telegram_chat_id:
|
||||
yield "telegram", {"chat_id": user.userinfo.telegram_chat_id, "text": message}
|
||||
if user.email:
|
||||
yield "email", {"subject": "Новая заявка в друзья", "message": message, "email": user.email}
|
||||
|
||||
def handle_friends_accept(self, payload):
|
||||
user = User.objects.get(id=payload['to_user_id'])
|
||||
from_user = User.objects.get(id=payload['from_user_id'])
|
||||
if user.userinfo.notification_friends:
|
||||
if payload['accepted']:
|
||||
message = f"Пользователь {from_user} одобрил заявку в друзья"
|
||||
else:
|
||||
message = f"Пользователь {from_user} отклонил заявку в друзья"
|
||||
if user.userinfo.telegram_chat_id:
|
||||
yield "telegram", {"chat_id": user.userinfo.telegram_chat_id, "text": message}
|
||||
if user.email:
|
||||
yield "email", {"subject": "Новая заявка в друзья", "message": message, "email": user.email}
|
||||
|
||||
def process(self, payload: dict):
|
||||
user = User.objects.get(id=payload['user_id'])
|
||||
notification_type = payload['type']
|
||||
text = payload['text']
|
||||
if notification_type == "any" or getattr(user.userinfo, "notification_" + notification_type):
|
||||
bot.send_message(
|
||||
user.userinfo.telegram_chat_id,
|
||||
text,
|
||||
parse_mode="html",
|
||||
)
|
||||
notification_type = payload["type"]
|
||||
handler = getattr(self, "handle_" + notification_type, None)
|
||||
if handler is None:
|
||||
raise ValueError(f"Unknown type: {notification_type}")
|
||||
for queue, payload in handler(payload):
|
||||
send_to_queue(queue, payload)
|
||||
|
16
daemons/management/commands/telegram_sender.py
Normal file
16
daemons/management/commands/telegram_sender.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from SprintLib.queue import MessagingSupport
|
||||
from daemons.management.commands.bot import bot
|
||||
|
||||
|
||||
class Command(MessagingSupport):
|
||||
help = "starts file telegram sender"
|
||||
queue_name = "telegram"
|
||||
|
||||
def process(self, payload: dict):
|
||||
chat_id = payload['chat_id']
|
||||
text = payload['text']
|
||||
bot.send_message(
|
||||
int(chat_id),
|
||||
text,
|
||||
parse_mode="html",
|
||||
)
|
Reference in New Issue
Block a user