From be01558ddb217627b351a1dd8a032483753b9082 Mon Sep 17 00:00:00 2001 From: exostring Date: Fri, 14 Aug 2026 22:14:31 +0500 Subject: [PATCH] Remove non-functional MAX auto-reaction, add real TG auto-reaction MAX Bot API has no reaction endpoint (confirmed 404 in production logs), so drop the dead code path entirely instead of eating a warning every post. Telegram's setMessageReaction is a real Bot API method (verified against aiogram 3.21.0, the pinned version) - wire it up as a non-fatal best-effort call after a successful send, mirroring the same try/except-and-log pattern MAX already used. --- .env.example | 6 +++--- src/config.py | 5 ++--- src/max_poster.py | 16 ---------------- src/tg_poster.py | 18 +++++++++++++++++- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/.env.example b/.env.example index fa682e7..8f86999 100644 --- a/.env.example +++ b/.env.example @@ -39,9 +39,6 @@ MAX_BOT_TOKEN=your_max_bot_token_here MAX_CHAT_ID=123456 # MAX API Base URL MAX_API_BASE_URL=https://platform-api2.max.ru -# MAX Auto Reaction emoji on published post -MAX_AUTO_REACTION=๐Ÿ‘ -MAX_AUTO_REACTION_ENABLED=true # ========================================== # Polling, Reporting & Maintenance @@ -83,6 +80,9 @@ FOOTER_TEXT= COMMON_TAGS=#redairsoft #ัั‚ั€ะฐะนะบะฑะพะป # Make the first line of the post bold FORMAT_FIRST_LINE_BOLD=true +# Telegram auto-reaction emoji on published posts (real Bot API method, unlike MAX) +TG_AUTO_REACTION=๐Ÿ‘ +TG_AUTO_REACTION_ENABLED=true # Logging Level (DEBUG, INFO, WARNING, ERROR) LOG_LEVEL=INFO diff --git a/src/config.py b/src/config.py index 790d772..236572e 100644 --- a/src/config.py +++ b/src/config.py @@ -34,9 +34,6 @@ class Settings(BaseSettings): max_bot_token: str = "" max_chat_id: str = "" # Destination chat ID in MAX max_api_base_url: str = "https://platform-api2.max.ru" - max_auto_reaction: str = "๐Ÿ‘" - max_auto_reaction_enabled: bool = True - max_reaction_path_template: str = "/messages/{message_id}/reactions" max_video_ready_attempts: int = 6 max_video_ready_delay_sec: float = 8.0 @@ -67,6 +64,8 @@ class Settings(BaseSettings): footer_text: str = "" common_tags: str = "" format_first_line_bold: bool = True + tg_auto_reaction: str = "๐Ÿ‘" + tg_auto_reaction_enabled: bool = True # Logging log_level: str = "INFO" diff --git a/src/max_poster.py b/src/max_poster.py index efeab4a..81d26ad 100644 --- a/src/max_poster.py +++ b/src/max_poster.py @@ -129,11 +129,6 @@ class MAXAPIClient: raise RuntimeError(f"MAX upload {resp.status}: {data}") return data - async def react(self, path_template: str, message_id: str, reaction: str) -> None: - path = path_template.format(message_id=message_id) - payload = {"reaction": reaction} - await self.request("POST", path, json=payload) - class MAXPoster: def __init__(self) -> None: @@ -295,17 +290,6 @@ class MAXPoster: msg_obj = res.get("message") if isinstance(res.get("message"), dict) else res first_url = self.message_url_from_response(msg_obj) - # Try auto reaction if enabled - if settings.max_auto_reaction_enabled and settings.max_auto_reaction: - try: - await client.react( - settings.max_reaction_path_template, - first_mid, - settings.max_auto_reaction, - ) - except Exception as exc: - logger.warning("MAX auto reaction failed: {}", exc) - for group in media_groups[1:]: attachments = await self.upload_media_group(client, group) if not attachments: diff --git a/src/tg_poster.py b/src/tg_poster.py index a4ad6b2..522a674 100644 --- a/src/tg_poster.py +++ b/src/tg_poster.py @@ -9,7 +9,7 @@ from aiogram import Bot from aiogram.client.session.aiohttp import AiohttpSession from aiogram.client.telegram import TelegramAPIServer from aiogram.exceptions import TelegramRetryAfter -from aiogram.types import FSInputFile, InputMediaPhoto, InputMediaVideo +from aiogram.types import FSInputFile, InputMediaPhoto, InputMediaVideo, ReactionTypeEmoji try: from .config import settings from .media_processor import ProcessedMedia @@ -119,6 +119,18 @@ class TelegramPoster: await asyncio.sleep(2 ** attempt) raise RuntimeError("Telegram retries exhausted") + async def set_reaction(self, message_id: int) -> None: + if not (settings.tg_auto_reaction_enabled and settings.tg_auto_reaction and self.bot): + return + try: + await self.bot.set_message_reaction( + chat_id=self.chat_id, + message_id=message_id, + reaction=[ReactionTypeEmoji(emoji=settings.tg_auto_reaction)], + ) + except Exception as exc: + logger.warning("Telegram auto reaction failed: {}", exc) + async def upload_media_for_file_ids( self, media_items: list[ProcessedMedia] ) -> dict[str, str]: @@ -405,6 +417,8 @@ class TelegramPoster: mids = await self.send_rich_message(rich_msg) url = tg_message_url(self.chat_id, mids[0]) if mids else None logger.info("Sent Telegram rich message: {}", mids) + if mids: + await self.set_reaction(mids[0]) return mids, url except RichMessageUnavailable as exc: logger.warning("Telegram sendRichMessage failed: {}. Falling back to standard send.", exc) @@ -412,4 +426,6 @@ class TelegramPoster: mids = await self.send_media_post(formatted_text, valid_media, file_ids) url = tg_message_url(self.chat_id, mids[0]) if mids else None logger.info("Sent Telegram message: {}", mids) + if mids: + await self.set_reaction(mids[0]) return mids, url