Agent Zero AI framework
fix(telegram): group attachment detection, typing lifecycle, per-chat session isolation
keyboardstaff24eb76296317
diff --git a/plugins/_telegram_integration/extensions/python/job_loop/_10_telegram_bot.py b/plugins/_telegram_integration/extensions/python/job_loop/_10_telegram_bot.pyindex c7e9fc090..e5d055ab3 100644--- a/plugins/_telegram_integration/extensions/python/job_loop/_10_telegram_bot.py+++ b/plugins/_telegram_integration/extensions/python/job_loop/_10_telegram_bot.py@@ -64,11 +64,11 @@ class TelegramBotManager(Extension):try:# Create handler closures that capture bot_name and config- _on_start = partial(_make_handler("handle_start"), bot_name=name, bot_cfg=bot_cfg)- _on_clear = partial(_make_handler("handle_clear"), bot_name=name, bot_cfg=bot_cfg)- _on_message = partial(_make_handler("handle_message"), bot_name=name, bot_cfg=bot_cfg)- _on_callback = partial(_make_handler("handle_callback_query"), bot_name=name, bot_cfg=bot_cfg)- _on_new_members = partial(_make_handler("handle_new_members"), bot_name=name, bot_cfg=bot_cfg)+ _on_start = partial(_make_handler(handle_start), bot_name=name, bot_cfg=bot_cfg)+ _on_clear = partial(_make_handler(handle_clear), bot_name=name, bot_cfg=bot_cfg)+ _on_message = partial(_make_handler(handle_message), bot_name=name, bot_cfg=bot_cfg)+ _on_callback = partial(_make_handler(handle_callback_query), bot_name=name, bot_cfg=bot_cfg)+ _on_new_members = partial(_make_handler(handle_new_members), bot_name=name, bot_cfg=bot_cfg)instance = create_bot(name=name,@@ -115,10 +115,8 @@ def _get_current_bot_cfg(bot_name: str) -> dict:return {}-def _make_handler(handler_func):+def _make_handler(handler_fn):"""Create a wrapper that resolves fresh bot config on every call."""async def _wrapped(event, bot_name: str, bot_cfg: dict):- from plugins._telegram_integration.helpers import handler- fn = getattr(handler, handler_func)- await fn(event, bot_name, _get_current_bot_cfg(bot_name) or bot_cfg)+ await handler_fn(event, bot_name, _get_current_bot_cfg(bot_name) or bot_cfg)return _wrappeddiff --git a/plugins/_telegram_integration/extensions/python/process_chain_end/_55_telegram_reply.py b/plugins/_telegram_integration/extensions/python/process_chain_end/_55_telegram_reply.pyindex c7b2ff005..11de2b57c 100644--- a/plugins/_telegram_integration/extensions/python/process_chain_end/_55_telegram_reply.py+++ b/plugins/_telegram_integration/extensions/python/process_chain_end/_55_telegram_reply.py@@ -2,7 +2,10 @@ from helpers.extension import Extensionfrom helpers.print_style import PrintStylefrom helpers.errors import format_errorfrom agent import AgentContext, LoopData, UserMessage-from plugins._telegram_integration.helpers.handler import CTX_TG_BOT, CTX_TG_ATTACHMENTS, CTX_TG_KEYBOARD+from plugins._telegram_integration.helpers.handler import (+ CTX_TG_BOT, CTX_TG_ATTACHMENTS, CTX_TG_KEYBOARD,+ CTX_TG_TYPING_STOP, CTX_TG_REPLY_TO,+)MAX_SEND_RETRIES: int = 2CTX_SEND_FAILURES: str = "_telegram_send_failures"@@ -29,6 +32,12 @@ class TelegramAutoReply(Extension):await self._send_reply(context, response_text, attachments, keyboard)except Exception as e:PrintStyle.error(f"Telegram auto-reply error: {format_error(e)}")+ finally:+ # Cancel typing and clean up reply_to after final send+ typing_stop = context.data.pop(CTX_TG_TYPING_STOP, None)+ if typing_stop:+ typing_stop.set()+ context.data.pop(CTX_TG_REPLY_TO, None)async def _send_reply(self,diff --git a/plugins/_telegram_integration/helpers/bot_manager.py b/plugins/_telegram_integration/helpers/bot_manager.pyindex 14b057903..cd300fa2f 100644--- a/plugins/_telegram_integration/helpers/bot_manager.py+++ b/plugins/_telegram_integration/helpers/bot_manager.py@@ -115,19 +115,21 @@ def _make_group_mention_filter(handler: Callable, bot: Bot):await handler(message)return- # Check for @mention in text- if message.text and f"@{bot_username}" in message.text:+ # Check for @mention in text or caption (media messages use caption)+ text = message.text or message.caption or ""+ entities = message.entities or message.caption_entities or []++ if text and f"@{bot_username}" in text:await handler(message)return# Check entities for mention- if message.entities:- for entity in message.entities:- if entity.type == "mention":- mention_text = message.text[entity.offset:entity.offset + entity.length]- if mention_text.lower() == f"@{bot_username.lower()}":- await handler(message)- return+ for entity in entities:+ if entity.type == "mention":+ mention_text = text[entity.offset:entity.offset + entity.length]+ if mention_text.lower() == f"@{bot_username.lower()}":+ await handler(message)+ return_group_handler.__name__ = f"_group_handler_{id(handler)}"return _group_handlerdiff --git a/plugins/_telegram_integration/helpers/handler.py b/plugins/_telegram_integration/helpers/handler.pyindex 84ba1a39b..dd8cfda53 100644--- a/plugins/_telegram_integration/helpers/handler.py+++ b/plugins/_telegram_integration/helpers/handler.py@@ -61,8 +61,8 @@ def _save_state(state: dict):files.write_file(path, json.dumps(state))-def _map_key(bot_name: str, user_id: int) -> str:- return f"{bot_name}:{user_id}"+def _map_key(bot_name: str, user_id: int, chat_id: int) -> str:+ return f"{bot_name}:{user_id}:{chat_id}"def cleanup_old_attachments():@@ -160,7 +160,7 @@ async def handle_clear(message: TgMessage, bot_name: str, bot_cfg: dict):if not _is_allowed(bot_cfg, user.id, user.username):return- key = _map_key(bot_name, user.id)+ key = _map_key(bot_name, user.id, message.chat.id)with _chat_map_lock:state = _load_state()@@ -349,7 +349,7 @@ async def _get_or_create_context_from_user(username: str | None,chat_id: int,) -> AgentContext | None:- key = _map_key(bot_name, user_id)+ key = _map_key(bot_name, user_id, chat_id)with _chat_map_lock:state = _load_state()@@ -360,7 +360,6 @@ async def _get_or_create_context_from_user(if ctx_id:ctx = AgentContext.get(ctx_id)if ctx:- ctx.data[CTX_TG_CHAT_ID] = chat_id # always track latest chatreturn ctx# Context was garbage collected, remove stale mappingchats.pop(key, None)@@ -490,12 +489,7 @@ async def send_telegram_reply(if not chat_id:return "No chat_id on context"- # Cancel persistent typing indicator- typing_stop = context.data.pop(CTX_TG_TYPING_STOP, None)- if typing_stop:- typing_stop.set()-- reply_to = context.data.pop(CTX_TG_REPLY_TO, None)+ reply_to = context.data.get(CTX_TG_REPLY_TO)try:async with _temp_bot(instance.bot.token, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) as reply_bot: