feat(telegram): add react_emoji config for incoming messages

This commit is contained in:
flobo3
2026-03-21 16:23:05 +03:00
committed by chengyongru
parent aba0b83a77
commit 9a2b1a3f1a

View File

@@ -11,7 +11,7 @@ from typing import Any, Literal
from loguru import logger from loguru import logger
from pydantic import Field from pydantic import Field
from telegram import BotCommand, ReplyParameters, Update from telegram import BotCommand, ReactionTypeEmoji, ReplyParameters, Update
from telegram.error import TimedOut from telegram.error import TimedOut
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
from telegram.request import HTTPXRequest from telegram.request import HTTPXRequest
@@ -173,6 +173,7 @@ class TelegramConfig(Base):
allow_from: list[str] = Field(default_factory=list) allow_from: list[str] = Field(default_factory=list)
proxy: str | None = None proxy: str | None = None
reply_to_message: bool = False reply_to_message: bool = False
react_emoji: str = "👀"
group_policy: Literal["open", "mention"] = "mention" group_policy: Literal["open", "mention"] = "mention"
connection_pool_size: int = 32 connection_pool_size: int = 32
pool_timeout: float = 5.0 pool_timeout: float = 5.0
@@ -812,6 +813,7 @@ class TelegramChannel(BaseChannel):
"session_key": session_key, "session_key": session_key,
} }
self._start_typing(str_chat_id) self._start_typing(str_chat_id)
await self._add_reaction(str_chat_id, message.message_id, self.config.react_emoji)
buf = self._media_group_buffers[key] buf = self._media_group_buffers[key]
if content and content != "[empty message]": if content and content != "[empty message]":
buf["contents"].append(content) buf["contents"].append(content)
@@ -822,6 +824,7 @@ class TelegramChannel(BaseChannel):
# Start typing indicator before processing # Start typing indicator before processing
self._start_typing(str_chat_id) self._start_typing(str_chat_id)
await self._add_reaction(str_chat_id, message.message_id, self.config.react_emoji)
# Forward to the message bus # Forward to the message bus
await self._handle_message( await self._handle_message(
@@ -861,6 +864,19 @@ class TelegramChannel(BaseChannel):
if task and not task.done(): if task and not task.done():
task.cancel() task.cancel()
async def _add_reaction(self, chat_id: str, message_id: int, emoji: str) -> None:
"""Add emoji reaction to a message (best-effort, non-blocking)."""
if not self._app or not emoji:
return
try:
await self._app.bot.set_message_reaction(
chat_id=int(chat_id),
message_id=message_id,
reaction=[ReactionTypeEmoji(emoji=emoji)],
)
except Exception as e:
logger.debug("Telegram reaction failed: {}", e)
async def _typing_loop(self, chat_id: str) -> None: async def _typing_loop(self, chat_id: str) -> None:
"""Repeatedly send 'typing' action until cancelled.""" """Repeatedly send 'typing' action until cancelled."""
try: try: