Skip to content

Commit

Permalink
Message reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
f213 committed Jan 15, 2024
1 parent c2b14ec commit 09143e2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
35 changes: 25 additions & 10 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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="👌",
),
)


Expand Down
20 changes: 20 additions & 0 deletions src/celery.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ class AppException(Exception):

class AnonymousMessage(AppException):
"""Message without a user"""


class WrongTelegramResponse(AppException):
"""Wrong response what direct calling telegram API"""
1 change: 0 additions & 1 deletion src/templates/messages/photo_is_sent.txt

This file was deleted.

0 comments on commit 09143e2

Please sign in to comment.