refactor(telegram): cleanup dead code, unused imports, constants

keyboardstaff committed Mar 20, 2026 at 19:29 UTC 3389682bd13f68d52acf938be59b17ea3be258fe
3 files changed +5 -45
plugins/_telegram_integration/helpers/bot_manager.py
+3 -9
@@ -1,14 +1,13 @@
1 import asyncio
2 -from dataclasses import dataclass, field
2 +from dataclasses import dataclass
3 from typing import Callable, Awaitable
4
5 from aiogram import Bot, Dispatcher, Router, F
6 from aiogram.client.default import DefaultBotProperties
7 from aiogram.enums import ParseMode, ChatType
8 -from aiogram.exceptions import TelegramBadRequest
8 from aiogram.filters import Command, CommandStart
10 -from aiogram.types import Message, CallbackQuery, Update
11 -from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application
9 +from aiogram.types import Message, CallbackQuery
10 +from aiogram.webhook.aiohttp_server import SimpleRequestHandler
11
12 from helpers.errors import format_error
13 from helpers.print_style import PrintStyle
@@ -96,7 +95,6 @@ def _make_group_mention_filter(handler: Callable, bot: Bot):
95 if message.chat.type == ChatType.PRIVATE:
96 return
97 # Use cached bot_info from the instance
99 - inst = get_bot(handler.__name__.split('_')[-1]) if hasattr(handler, '__name__') else None
98 bot_info = None
99 for b in _bots.values():
100 if b.bot is bot:
@@ -205,10 +203,6 @@ async def stop_bot(name: str):
203 PrintStyle.info(f"Telegram ({name}): stopped")
204
205
208 -async def stop_all_bots():
209 - for name in list(_bots.keys()):
210 - await stop_bot(name)
211 -
206 # Test connection
207
208 async def test_token(token: str) -> tuple[bool, str]:
plugins/_telegram_integration/helpers/handler.py
+2 -13
@@ -6,8 +6,8 @@ import uuid
6
7 from aiogram.types import Message as TgMessage, CallbackQuery
8
9 -from agent import Agent, AgentContext, AgentContextType, UserMessage
10 -from helpers import guids, plugins, files
9 +from agent import AgentContext, UserMessage
10 +from helpers import plugins, files
11 from helpers import message_queue as mq
12 from helpers.notification import NotificationManager, NotificationType, NotificationPriority
13 from helpers.persist_chat import save_tmp_chat
@@ -28,7 +28,6 @@ CTX_TG_BOT = "telegram_bot"
28 CTX_TG_CHAT_ID = "telegram_chat_id"
29 CTX_TG_USER_ID = "telegram_user_id"
30 CTX_TG_USERNAME = "telegram_username"
31 -CTX_TG_LAST_MSG_ID = "telegram_last_msg_id"
31
32 # Transient
33 CTX_TG_ATTACHMENTS = "_telegram_response_attachments"
@@ -210,9 +209,6 @@ async def handle_message(message: TgMessage, bot_name: str, bot_cfg: dict):
209 text = _extract_message_content(message)
210 attachments = await _download_attachments(instance.bot, message, bot_name=bot_name)
211
213 - # Store last incoming message ID for reply threading
214 - context.data[CTX_TG_LAST_MSG_ID] = message.message_id
215 -
212 # Build user message with prompt
213 agent = context.agent0
214 user_msg = agent.read_prompt(
@@ -508,10 +504,3 @@ def _format_user(user) -> str:
504 name += f" (@{user.username})"
505 return name.strip() or str(user.id)
506
511 -
512 -def find_context_for_bot_chat(bot_name: str, chat_id: int) -> AgentContext | None:
513 - """Find active context matching a Telegram bot + chat_id."""
514 - for ctx in AgentContext.all():
515 - if ctx.data.get(CTX_TG_BOT) == bot_name and ctx.data.get(CTX_TG_CHAT_ID) == chat_id:
516 - return ctx
517 - return None
plugins/_telegram_integration/helpers/telegram_client.py
-23
@@ -1,11 +1,8 @@
1 -import asyncio
1 import os
3 -from typing import BinaryIO
2
3 from aiogram import Bot
4 from aiogram.exceptions import TelegramBadRequest
5 from aiogram.types import (
8 - BufferedInputFile,
6 FSInputFile,
7 InlineKeyboardButton,
8 InlineKeyboardMarkup,
@@ -105,26 +102,6 @@ async def send_photo(
102 return None
103
104
108 -async def send_file_bytes(
109 - bot: Bot,
110 - chat_id: int,
111 - content: bytes,
112 - filename: str,
113 - caption: str = "",
114 -) -> int | None:
115 - """Send file from bytes content."""
116 - try:
117 - input_file = BufferedInputFile(content, filename=filename)
118 - msg = await bot.send_document(
119 - chat_id=chat_id,
120 - document=input_file,
121 - caption=caption[:1024] if caption else None,
122 - )
123 - return msg.message_id
124 - except Exception as e:
125 - PrintStyle.error(f"Telegram send_file_bytes failed: {format_error(e)}")
126 - return None
127 -
105 # Inline keyboards
106
107 def build_inline_keyboard(