Skip to content

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.py
index 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 → skip
if (inst.task and not inst.task.done()) or inst.webhook_active:
continue
diff --git a/plugins/_telegram_integration/extensions/python/system_prompt/_20_telegram_context.py b/plugins/_telegram_integration/extensions/python/system_prompt/_20_telegram_context.py
index 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 Extension
from 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_CFG
class 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.py
index 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 PrintStyle
from helpers.tool import Response
from 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.py
index 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, F
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode, ChatType
from aiogram.filters import Command, CommandStart
-from aiogram.types import Message, CallbackQuery
+from aiogram.types import Message
from helpers.errors import format_error
from helpers.print_style import PrintStyle
@@ -22,6 +22,7 @@ class BotInstance:
task: asyncio.Task | None = None # polling task
webhook_active: bool = False # True when webhook mode is registered
webhook_secret: str = "" # secret for webhook verification
+ group_mode: str = "mention" # current group_mode setting
bot_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] = instance
return instance
diff --git a/plugins/_telegram_integration/helpers/handler.py b/plugins/_telegram_integration/helpers/handler.py
index 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 keys
CTX_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 chat
return ctx
# Context was garbage collected, remove stale mapping
chats.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_cfg
ctx.data[CTX_TG_CHAT_ID] = chat_id
ctx.data[CTX_TG_USER_ID] = user_id
ctx.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.md
deleted file mode 100644
index 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 file
diff --git a/plugins/_telegram_integration/prompts/fw.telegram.system_context.md b/plugins/_telegram_integration/prompts/fw.telegram.system_context.md
deleted file mode 100644
index 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