Agent Zero AI framework
refactor(telegram): optimize message handling and unify system context
keyboardstaffd6811c1ec492
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 3c2f0e60e..7a87ce8bb 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@@ -53,7 +53,8 @@ class TelegramBotManager(Extension):inst = running[name]current_mode = bot_cfg.get("mode", "polling")running_mode = "webhook" if inst.webhook_active else "polling"- if current_mode == running_mode:+ current_group_mode = bot_cfg.get("group_mode", "mention")+ if current_mode == running_mode and current_group_mode == inst.group_mode:# Same mode and still alive → skipif (inst.task and not inst.task.done()) or inst.webhook_active:continuediff --git a/plugins/_telegram_integration/extensions/python/system_prompt/_20_telegram_context.py b/plugins/_telegram_integration/extensions/python/system_prompt/_20_telegram_context.pyindex 7d34e7a9c..cac462245 100644--- a/plugins/_telegram_integration/extensions/python/system_prompt/_20_telegram_context.py+++ b/plugins/_telegram_integration/extensions/python/system_prompt/_20_telegram_context.py@@ -1,6 +1,6 @@from helpers.extension import Extensionfrom agent import LoopData-from plugins._telegram_integration.helpers.handler import CTX_TG_BOT+from plugins._telegram_integration.helpers.handler import CTX_TG_BOT, CTX_TG_BOT_CFGclass TelegramContextPrompt(Extension):@@ -18,3 +18,14 @@ class TelegramContextPrompt(Extension):system_prompt.append(self.agent.read_prompt("fw.telegram.system_context_reply.md"))++ # Inject per-bot agent instructions (once in system prompt)+ bot_cfg = self.agent.context.data.get(CTX_TG_BOT_CFG, {})+ instructions = bot_cfg.get("agent_instructions", "")+ if instructions:+ system_prompt.append(+ self.agent.read_prompt(+ "fw.telegram.user_message_instructions.md",+ instructions=instructions,+ )+ )diff --git a/plugins/_telegram_integration/extensions/python/tool_execute_after/_50_telegram_response.py b/plugins/_telegram_integration/extensions/python/tool_execute_after/_50_telegram_response.pyindex fd91213d7..8b8885d60 100644--- a/plugins/_telegram_integration/extensions/python/tool_execute_after/_50_telegram_response.py+++ b/plugins/_telegram_integration/extensions/python/tool_execute_after/_50_telegram_response.py@@ -1,5 +1,4 @@from helpers.extension import Extension-from helpers.print_style import PrintStylefrom helpers.tool import Responsefrom plugins._telegram_integration.helpers.handler import (CTX_TG_BOT,diff --git a/plugins/_telegram_integration/helpers/bot_manager.py b/plugins/_telegram_integration/helpers/bot_manager.pyindex 0b7e6f82c..ef2d138eb 100644--- a/plugins/_telegram_integration/helpers/bot_manager.py+++ b/plugins/_telegram_integration/helpers/bot_manager.py@@ -6,7 +6,7 @@ from aiogram import Bot, Dispatcher, Router, Ffrom aiogram.client.default import DefaultBotPropertiesfrom aiogram.enums import ParseMode, ChatTypefrom aiogram.filters import Command, CommandStart-from aiogram.types import Message, CallbackQuery+from aiogram.types import Messagefrom helpers.errors import format_errorfrom helpers.print_style import PrintStyle@@ -22,6 +22,7 @@ class BotInstance:task: asyncio.Task | None = None # polling taskwebhook_active: bool = False # True when webhook mode is registeredwebhook_secret: str = "" # secret for webhook verification+ group_mode: str = "mention" # current group_mode settingbot_info: object | None = None # cached result of bot.get_me()# Bot registry (singleton, persists across module reloads)@@ -77,7 +78,7 @@ def create_bot(router.message.register(on_message)dp.include_router(router)- instance = BotInstance(name=name, bot=bot, dispatcher=dp, router=router)+ instance = BotInstance(name=name, bot=bot, dispatcher=dp, router=router, group_mode=group_mode)_bots[name] = instancereturn instancediff --git a/plugins/_telegram_integration/helpers/handler.py b/plugins/_telegram_integration/helpers/handler.pyindex f48bb4abc..d4927212f 100644--- a/plugins/_telegram_integration/helpers/handler.py+++ b/plugins/_telegram_integration/helpers/handler.py@@ -29,6 +29,7 @@ STATE_FILE = "usr/telegram/state.json"# Context data keysCTX_TG_BOT = "telegram_bot"+CTX_TG_BOT_CFG = "telegram_bot_cfg"CTX_TG_CHAT_ID = "telegram_chat_id"CTX_TG_USER_ID = "telegram_user_id"CTX_TG_USERNAME = "telegram_username"@@ -232,19 +233,9 @@ async def handle_message(message: TgMessage, bot_name: str, bot_cfg: dict):body=text,)- instructions = bot_cfg.get("agent_instructions", "")- if instructions:- user_msg += agent.read_prompt(- "fw.telegram.user_message_instructions.md",- instructions=instructions,- )-- system_ctx = agent.read_prompt("fw.telegram.system_context.md")-mq.log_user_message(context, user_msg, attachments, source=" (telegram)")context.communicate(UserMessage(message=user_msg,- system_message=[system_ctx],attachments=attachments,))@@ -331,6 +322,7 @@ 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)@@ -342,6 +334,7 @@ async def _get_or_create_context_from_user(ctx = AgentContext(config, name=f"Telegram: {display_name}")ctx.data[CTX_TG_BOT] = bot_name+ ctx.data[CTX_TG_BOT_CFG] = bot_cfgctx.data[CTX_TG_CHAT_ID] = chat_idctx.data[CTX_TG_USER_ID] = user_idctx.data[CTX_TG_USERNAME] = username or ""diff --git a/plugins/_telegram_integration/prompts/fw.telegram.cleared.md b/plugins/_telegram_integration/prompts/fw.telegram.cleared.mddeleted file mode 100644index a0b41a3b3..000000000--- a/plugins/_telegram_integration/prompts/fw.telegram.cleared.md+++ /dev/null@@ -1,3 +0,0 @@-chat cleared by user via /clear command-previous context has been reset-greet user and wait for new input\ No newline at end of filediff --git a/plugins/_telegram_integration/prompts/fw.telegram.system_context.md b/plugins/_telegram_integration/prompts/fw.telegram.system_context.mddeleted file mode 100644index f015b8ac7..000000000--- a/plugins/_telegram_integration/prompts/fw.telegram.system_context.md+++ /dev/null@@ -1,4 +0,0 @@-telegram session user communicates via Telegram-response tool sends the message to the user dont use python code-break_loop true > stop working and wait for user reply-break_loop false > only for mid-task progress updates then keep working\ No newline at end of file