From 09143e25ac6a1e6afd8c6d1f60d52768c4512ca1 Mon Sep 17 00:00:00 2001 From: Fedor Borshev Date: Mon, 15 Jan 2024 20:58:22 +0300 Subject: [PATCH] Message reactions --- src/bot.py | 35 +++++++++++++++++------- src/celery.py | 20 ++++++++++++++ src/exceptions.py | 4 +++ src/templates/messages/photo_is_sent.txt | 1 - 4 files changed, 49 insertions(+), 11 deletions(-) delete mode 100644 src/templates/messages/photo_is_sent.txt diff --git a/src/bot.py b/src/bot.py index 4a3f36a..f0b26be 100644 --- a/src/bot.py +++ b/src/bot.py @@ -55,12 +55,22 @@ async def send_text_message(update: TextMessageUpdate, user: User) -> None: text = update.message.text subject = get_subject(text) - tasks.send_text.delay( + send = tasks.send_text.si( user_id=user.pk, subject=subject, text=text, ) - await update.message.reply_text(text=render("message_is_sent", invite_to_change_email=("@" in text))) + if "@" in text: + send.apply_async() + await update.message.reply_text(text=render("message_is_sent", invite_to_change_email=("@" in text))) + else: + send.apply_async( + link=tasks.react.si( + chat_id=update.message.chat_id, + message_id=update.message.message_id, + reaction="👌", + ) + ) @reply @@ -75,14 +85,19 @@ async def send_photo(update: MessageUpdate, user: User) -> None: if text: subject = f"Photo: {get_subject(text)}" - await update.message.reply_text(text=render("photo_is_sent")) - - tasks.send_file.delay( - user_id=user.pk, - file=photo, - filename=Path(file.file_path).name, # type: ignore[arg-type] - subject=subject, - text=text, + tasks.send_file.apply_async( + kwargs={ + "user_id": user.pk, + "file": photo, + "filename": Path(file.file_path).name, # type: ignore[arg-type] + "subject": subject, + "text": text, + }, + link=tasks.react.si( + chat_id=update.message.chat_id, + message_id=update.message.message_id, + reaction="👌", + ), ) diff --git a/src/celery.py b/src/celery.py index 84df238..466823b 100644 --- a/src/celery.py +++ b/src/celery.py @@ -1,9 +1,11 @@ import os from io import BytesIO +import httpx from celery import Celery from dotenv import load_dotenv +from .exceptions import WrongTelegramResponse from .helpers import init_sentry from .mail import send_mail from .models import User @@ -56,3 +58,21 @@ def send_file(user_id: int, file: BytesIO, filename: str, subject: str, text: st attachment=file, attachment_name=filename, ) + + +@celery.task +def react(chat_id: str, message_id: int, reaction: str) -> None: + """Temporary task while PTB rolls out their own reactions""" + bot_token = os.getenv("BOT_TOKEN") + + response = httpx.post( + f"https://api.telegram.org/bot{bot_token}/setMessageReaction", + json={ + "chat_id": chat_id, + "message_id": message_id, + "reaction": [{"type": "emoji", "emoji": reaction}], + }, + ) + + if response.status_code != 200: + raise WrongTelegramResponse diff --git a/src/exceptions.py b/src/exceptions.py index 57edb9e..878f765 100644 --- a/src/exceptions.py +++ b/src/exceptions.py @@ -4,3 +4,7 @@ class AppException(Exception): class AnonymousMessage(AppException): """Message without a user""" + + +class WrongTelegramResponse(AppException): + """Wrong response what direct calling telegram API""" diff --git a/src/templates/messages/photo_is_sent.txt b/src/templates/messages/photo_is_sent.txt deleted file mode 100644 index e0d0712..0000000 --- a/src/templates/messages/photo_is_sent.txt +++ /dev/null @@ -1 +0,0 @@ -Ok, sending