Agent Zero AI framework
Send Telegram intermediate responses separately
Anmol Malik8bc81c7a6a50
diff --git a/plugins/_telegram_integration/README.md b/plugins/_telegram_integration/README.mdindex bf486184e..a9793fa35 100644--- a/plugins/_telegram_integration/README.md+++ b/plugins/_telegram_integration/README.md@@ -35,7 +35,7 @@ This plugin connects one or more Telegram bots to Agent Zero. Each bot runs inde- **Reply delivery**- Streams tool progress and response text through real Telegram messages updated with `editMessageText`.- Tool progress and the AI response are separate messages; only the AI response replies to the user message.- - `tool_execute_after` intercepts the `response` tool — sends inline progress for `break_loop=false`.+ - `tool_execute_after` intercepts the `response` tool — sends `break_loop=false` updates as separate intermediate Telegram messages.- `process_chain_end` auto-sends the final response, with retry logic on failure.- **Formatting**- Converts Markdown output to Telegram-compatible HTML (bold, italic, strikethrough, code, links, blockquotes, lists).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 43fcbae24..c959eaf52 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@@ -47,6 +47,7 @@ class TelegramResponseIntercept(Extension):async def _send_inline(self, context, tool, response: Response):ensure_dependencies()from plugins._telegram_integration.helpers.handler import send_telegram_reply+ from plugins._telegram_integration.helpers import draft_streamagent = self.agentassert agent is not None@@ -55,7 +56,12 @@ class TelegramResponseIntercept(Extension):attachments = context.data.pop(CTX_TG_ATTACHMENTS, [])keyboard = context.data.pop(CTX_TG_KEYBOARD, None)- error = await send_telegram_reply(context, text, attachments or None, keyboard)+ if attachments:+ error = await send_telegram_reply(context, text, attachments or None, keyboard)+ elif await draft_stream.send_intermediate_response(context, text, keyboard):+ error = None+ else:+ error = "Telegram intermediate update was not sent"if error:result = agent.read_prompt("fw.telegram.update_error.md", error=error)diff --git a/plugins/_telegram_integration/helpers/draft_stream.py b/plugins/_telegram_integration/helpers/draft_stream.pyindex 491c7d2a2..b246f8ab3 100644--- a/plugins/_telegram_integration/helpers/draft_stream.py+++ b/plugins/_telegram_integration/helpers/draft_stream.py@@ -84,6 +84,32 @@ async def update_response(context: AgentContext, response_text: str) -> None:context.data[CTX_TG_RESPONSE_LAST_UPDATE] = now+async def send_intermediate_response(+ context: AgentContext,+ response_text: str,+ keyboard: list[list[dict]] | None = None,+) -> bool:+ html = _format_response(response_text)+ if not html:+ return False+ bot = _bot_instance(context)+ chat_id = context.data.get(CTX_TG_CHAT_ID)+ if not bot or not chat_id:+ return False+ try:+ sent_id = await tc.raw_send_text(+ bot.bot.token,+ int(chat_id),+ html,+ parse_mode="HTML",+ reply_markup=_keyboard_markup(keyboard),+ )+ return bool(sent_id)+ except Exception as e:+ PrintStyle.debug(f"Telegram intermediate response failed: {e}")+ return False++async def finalize_response(context: AgentContext,response_text: str,diff --git a/tests/test_telegram_intermediate_response.py b/tests/test_telegram_intermediate_response.pynew file mode 100644index 000000000..cb56946ed--- /dev/null+++ b/tests/test_telegram_intermediate_response.py@@ -0,0 +1,70 @@+import asyncio++from plugins._telegram_integration.helpers import draft_stream+from plugins._telegram_integration.helpers.constants import (+ CTX_TG_BOT,+ CTX_TG_CHAT_ID,+ CTX_TG_REPLY_TO,+ CTX_TG_RESPONSE_MESSAGE_ID,+)+++class FakeBot:+ class Bot:+ token = "token"++ bot = Bot()+++class FakeContext:+ def __init__(self):+ self.data = {+ CTX_TG_BOT: "main",+ CTX_TG_CHAT_ID: 123,+ CTX_TG_REPLY_TO: 456,+ }++ def get_data(self, key):+ return self.data.get(key)+++def test_intermediate_response_sends_separate_non_reply_message(monkeypatch):+ calls = []++ async def fake_send(token, chat_id, text, reply_to_message_id=None, parse_mode="HTML", reply_markup=None):+ calls.append(+ {+ "token": token,+ "chat_id": chat_id,+ "text": text,+ "reply_to_message_id": reply_to_message_id,+ "parse_mode": parse_mode,+ "reply_markup": reply_markup,+ }+ )+ return 789++ context = FakeContext()+ monkeypatch.setattr(draft_stream, "_bot_instance", lambda ctx: FakeBot())+ monkeypatch.setattr(draft_stream.tc, "raw_send_text", fake_send)++ sent = asyncio.run(+ draft_stream.send_intermediate_response(+ context,+ "**Working** on the brief.",+ keyboard=[[{"text": "Open", "callback_data": "open"}]],+ )+ )++ assert sent is True+ assert calls == [+ {+ "token": "token",+ "chat_id": 123,+ "text": "<b>Working</b> on the brief.",+ "reply_to_message_id": None,+ "parse_mode": "HTML",+ "reply_markup": {"inline_keyboard": [[{"text": "Open", "callback_data": "open"}]]},+ }+ ]+ assert CTX_TG_RESPONSE_MESSAGE_ID not in context.data