Agent Zero AI framework
feat(telegram): message formatting rewrite, cross-event-loop fixes & hot mode switching
keyboardstaffaf60a6e29193
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 a7eddb7a9..39f8fd7aa 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@@ -52,10 +52,13 @@ class TelegramBotManager(Extension):continueif name in running:inst = running[name]- # Already running: polling (task alive) or webhook (active)- if (inst.task and not inst.task.done()) or inst.webhook_active:- continue- # Instance exists but is dead — stop and recreate+ current_mode = bot_cfg.get("mode", "polling")+ running_mode = "webhook" if inst.webhook_active else "polling"+ if current_mode == running_mode:+ # Same mode and still alive → skip+ if (inst.task and not inst.task.done()) or inst.webhook_active:+ continue+ # Mode changed or instance died — stop and recreateawait stop_bot(name)try:diff --git a/plugins/_telegram_integration/helpers/bot_manager.py b/plugins/_telegram_integration/helpers/bot_manager.pyindex 580032c0b..0b7e6f82c 100644--- a/plugins/_telegram_integration/helpers/bot_manager.py+++ b/plugins/_telegram_integration/helpers/bot_manager.py@@ -47,7 +47,7 @@ def create_bot(on_callback_query: Callable[..., Awaitable] | None = None,group_mode: str = "mention",) -> BotInstance:- bot = Bot(token=token, default=DefaultBotProperties(parse_mode=ParseMode.MARKDOWN))+ bot = Bot(token=token, default=DefaultBotProperties(parse_mode=ParseMode.HTML))dp = Dispatcher()router = Router()diff --git a/plugins/_telegram_integration/helpers/handler.py b/plugins/_telegram_integration/helpers/handler.pyindex 40159be5c..0b3e354b2 100644--- a/plugins/_telegram_integration/helpers/handler.py+++ b/plugins/_telegram_integration/helpers/handler.py@@ -4,10 +4,13 @@ import threadingimport timeimport uuid+from aiogram import Bot+from aiogram.client.default import DefaultBotProperties+from aiogram.enums import ParseModefrom aiogram.types import Message as TgMessage, CallbackQueryfrom agent import AgentContext, UserMessage-from helpers import plugins, files+from helpers import plugins, files, projectsfrom helpers import message_queue as mqfrom helpers.notification import NotificationManager, NotificationType, NotificationPriorityfrom helpers.persist_chat import save_tmp_chat@@ -28,6 +31,7 @@ CTX_TG_BOT = "telegram_bot"CTX_TG_CHAT_ID = "telegram_chat_id"CTX_TG_USER_ID = "telegram_user_id"CTX_TG_USERNAME = "telegram_username"+CTX_TG_TYPING_STOP = "_telegram_typing_stop"# TransientCTX_TG_ATTACHMENTS = "_telegram_response_attachments"@@ -122,17 +126,19 @@ async def handle_start(message: TgMessage, bot_name: str, bot_cfg: dict):returnif not _is_allowed(bot_cfg, user.id, user.username):- await message.reply("⛔ You are not authorized to use this bot.")+ await message.reply("You are not authorized to use this bot.")returninstance = get_bot(bot_name)if not instance:return- await message.reply(- f"👋 Hello {user.first_name}! I'm connected to Agent Zero.\n\n"+ await _send_with_temp_bot(+ instance.bot.token, message.chat.id,+ f"\U0001f44b Hello {user.first_name}! I'm connected to Agent Zero.\n\n""Send me a message and I'll process it.\n"- "Use /clear to reset the conversation."+ "Use /clear to reset the conversation.",+ parse_mode=None,)# Ensure a chat context exists@@ -161,9 +167,9 @@ async def handle_clear(message: TgMessage, bot_name: str, bot_cfg: dict):instance = get_bot(bot_name)if instance:- await tc.send_text(- instance.bot, message.chat.id,- "🗑 Chat cleared. Send a new message to start fresh.",+ await _send_with_temp_bot(+ instance.bot.token, message.chat.id,+ "Chat cleared. Send a new message to start fresh.",parse_mode=None,)@@ -192,22 +198,35 @@ async def handle_message(message: TgMessage, bot_name: str, bot_cfg: dict):if not instance:return- # Send typing indicator- await tc.send_typing(instance.bot, message.chat.id)+ # Start persistent typing indicator (thread-based, works across event loops)+ typing_stop = _start_typing(instance.bot.token, message.chat.id)# Get or create agent contextcontext = await _get_or_create_context(bot_name, bot_cfg, message)if not context:- await tc.send_text(- instance.bot, message.chat.id,- "❌ Failed to create chat session.",+ typing_stop.set()+ await _send_with_temp_bot(+ instance.bot.token, message.chat.id,+ "Failed to create chat session.",parse_mode=None,)return+ # Store stop event so send_telegram_reply can cancel typing+ context.data[CTX_TG_TYPING_STOP] = typing_stop+# Build user message texttext = _extract_message_content(message)- attachments = await _download_attachments(instance.bot, message, bot_name=bot_name)++ # Use temp bot for downloads (cross-event-loop safe)+ dl_bot = Bot(token=instance.bot.token)+ try:+ attachments = await _download_attachments(dl_bot, message, bot_name=bot_name)+ finally:+ try:+ await dl_bot.session.close()+ except Exception:+ pass# Build user message with promptagent = context.agent0@@ -332,7 +351,6 @@ async def _get_or_create_context_from_user(project = _get_project(bot_cfg, user_id)if project:- from helpers import projectsprojects.activate_project(ctx.id, project)chats[key] = ctx.id@@ -445,10 +463,6 @@ async def send_telegram_reply(keyboard: list[list[dict]] | None = None,) -> str | None:"""Send reply to Telegram user. Returns error string or None on success."""- from aiogram import Bot- from aiogram.client.default import DefaultBotProperties- from aiogram.enums import ParseMode-bot_name = context.data.get(CTX_TG_BOT)if not bot_name:return "No Telegram bot configured on context"@@ -461,11 +475,14 @@ async def send_telegram_reply(if not chat_id:return "No chat_id on context"- # Create a temporary Bot bound to the current event loop to avoid- # cross-event-loop issues with the shared instance's aiohttp session.+ # Cancel persistent typing indicator+ typing_stop = context.data.pop(CTX_TG_TYPING_STOP, None)+ if typing_stop:+ typing_stop.set()+reply_bot = Bot(token=instance.bot.token,- default=DefaultBotProperties(parse_mode=ParseMode.MARKDOWN),+ default=DefaultBotProperties(parse_mode=ParseMode.HTML),)try:# Send attachments first@@ -476,14 +493,15 @@ async def send_telegram_reply(else:await tc.send_file(reply_bot, chat_id, path)- # Send text (with or without keyboard)+ # Send text (with or without keyboard), convert Markdown → HTMLif response_text:+ html_text = tc.md_to_telegram_html(response_text)if keyboard:await tc.send_text_with_keyboard(- reply_bot, chat_id, response_text, keyboard,+ reply_bot, chat_id, html_text, keyboard,)else:- await tc.send_text(reply_bot, chat_id, response_text)+ await tc.send_text(reply_bot, chat_id, html_text)return None@@ -499,6 +517,50 @@ async def send_telegram_reply(# Helpers+async def _send_with_temp_bot(token: str, chat_id: int, text: str, parse_mode: str | None = None):+ """Send text using a temporary Bot to avoid cross-event-loop session issues."""+ bot = Bot(token=token)+ try:+ await tc.send_text(bot, chat_id, text, parse_mode=parse_mode)+ finally:+ try:+ await bot.session.close()+ except Exception:+ pass+++def _start_typing(token: str, chat_id: int) -> threading.Event:+ """Spawn a daemon thread that sends typing every 4s. Returns a stop Event."""+ stop = threading.Event()++ def _run():+ import asyncio++ async def _loop():+ bot = Bot(token=token)+ try:+ while not stop.is_set():+ await tc.send_typing(bot, chat_id)+ # Sleep 4s total, checking stop every 0.5s+ for _ in range(8):+ if stop.is_set():+ return+ await asyncio.sleep(0.5)+ except Exception:+ pass+ finally:+ try:+ await bot.session.close()+ except Exception:+ pass++ asyncio.run(_loop())++ t = threading.Thread(target=_run, daemon=True)+ t.start()+ return stop++def _format_user(user) -> str:name = user.first_name or ""if user.last_name:diff --git a/plugins/_telegram_integration/helpers/telegram_client.py b/plugins/_telegram_integration/helpers/telegram_client.pyindex bd509b6ea..62aa7c4e3 100644--- a/plugins/_telegram_integration/helpers/telegram_client.py+++ b/plugins/_telegram_integration/helpers/telegram_client.py@@ -1,4 +1,5 @@import os+import refrom aiogram import Botfrom aiogram.exceptions import TelegramBadRequest@@ -11,6 +12,8 @@ from aiogram.types import (from helpers.errors import format_errorfrom helpers.print_style import PrintStyle+_UNSET = object() # sentinel: "not provided" (lets Bot default apply)+# Text messagesMAX_MESSAGE_LENGTH: int = 4096 # Telegram message length limit@@ -21,26 +24,34 @@ async def send_text(chat_id: int,text: str,reply_to_message_id: int | None = None,- parse_mode: str | None = None,+ parse_mode: object = _UNSET,) -> int | None:- """Send text message, splitting if too long. Returns last message_id or None on error."""+ """Send text message, splitting if too long. Returns last message_id or None on error.++ parse_mode behaviour:+ - _UNSET (default): omitted from send_message → Bot's DefaultBotProperties applies.+ - None: explicitly no formatting.+ - "HTML"/"Markdown"/etc.: that specific mode.+ """try:chunks = _split_text(text, MAX_MESSAGE_LENGTH)last_msg_id = None+ pm_kwargs: dict = {} if parse_mode is _UNSET else {"parse_mode": parse_mode}for chunk in chunks:try:msg = await bot.send_message(chat_id=chat_id,text=chunk,reply_to_message_id=reply_to_message_id,- parse_mode=parse_mode,+ **pm_kwargs,)last_msg_id = msg.message_idexcept TelegramBadRequest:- # Retry without markdown if parse fails+ # Retry as plain text, stripping HTML tags+ plain = re.sub(r"<[^>]+>", "", chunk)msg = await bot.send_message(chat_id=chat_id,- text=chunk,+ text=plain,reply_to_message_id=reply_to_message_id,parse_mode=None,)@@ -133,15 +144,18 @@ async def send_text_with_keyboard(text: str,buttons: list[list[dict]],reply_to_message_id: int | None = None,+ parse_mode: object = _UNSET,) -> int | None:"""Send text with inline keyboard buttons."""try:keyboard = build_inline_keyboard(buttons)+ pm_kwargs: dict = {} if parse_mode is _UNSET else {"parse_mode": parse_mode}msg = await bot.send_message(chat_id=chat_id,text=text,reply_markup=keyboard,reply_to_message_id=reply_to_message_id,+ **pm_kwargs,)return msg.message_idexcept Exception as e:@@ -201,3 +215,156 @@ _IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"}def is_image_file(path: str) -> bool:_, ext = os.path.splitext(path.lower())return ext in _IMAGE_EXTENSIONS+++def md_to_telegram_html(text: str) -> str:+ """Convert standard Markdown to Telegram-compatible HTML.++ Handles: fenced code blocks, inline code, bold, italic, strikethrough,+ links, headings, blockquotes, tables, and nested lists.+ """+ stash: list[str] = []++ def _put(html: str) -> str:+ stash.append(html)+ return f"\x00B{len(stash) - 1}\x00"++ def _esc(t: str) -> str:+ return t.replace("&", "&").replace("<", "<").replace(">", ">")++ # Stash fenced code blocks+ def _code_block(m: re.Match) -> str:+ lang, code = m.group(1) or "", _esc(m.group(2))+ tag = f'<pre><code class="language-{lang}">{code}</code></pre>' if lang else f"<pre>{code}</pre>"+ return _put(tag)++ text = re.sub(r"```(\w*)\n(.*?)```", _code_block, text, flags=re.DOTALL)++ # Stash inline code+ text = re.sub(r"`([^`]+)`", lambda m: _put(f"<code>{_esc(m.group(1))}</code>"), text)++ # Stash Markdown tables as list-style text+ text = _stash_tables(text, _put, _esc)++ # Escape remaining HTML chars+ text = _esc(text)++ # Inline formatting+ text = re.sub(r"\*\*(.+?)\*\*", r"<b>\1</b>", text)+ text = re.sub(r"__(.+?)__", r"<b>\1</b>", text)+ text = re.sub(r"(?<!\w)\*([^*]+?)\*(?!\w)", r"<i>\1</i>", text)+ text = re.sub(r"(?<!\w)_([^_]+?)_(?!\w)", r"<i>\1</i>", text)+ text = re.sub(r"~~(.+?)~~", r"<s>\1</s>", text)+ text = re.sub(r"\[([^\]]+)\]\(([^)]+)\)", r'<a href="\2">\1</a>', text)+ text = re.sub(r"^#{1,6}\s+(.+)$", r"<b>\1</b>", text, flags=re.MULTILINE)++ # Blockquotes: > text → <blockquote>+ text = _convert_blockquotes(text)++ # Lists (nested + ordered)+ text = _convert_lists(text)++ # Restore all stashed blocks+ for i, block in enumerate(stash):+ text = text.replace(f"\x00B{i}\x00", block)+ return text+++# ── Helpers for md_to_telegram_html ──++_TABLE_ROW = re.compile(r"^\|(.+)\|$")+_TABLE_SEP = re.compile(r"^[\s|:-]+$")+++def _stash_tables(text: str, put_fn, esc_fn) -> str:+ """Convert Markdown tables to key-value list format and stash them.++ | Name | Age |+ |-------|-----|+ | Alice | 30 | → • Name: Alice, Age: 30+ | Bob | 25 | • Name: Bob, Age: 25+ """+ lines = text.split("\n")+ out: list[str] = []+ headers: list[str] = []+ data_rows: list[list[str]] = []++ def _flush():+ if not headers and not data_rows:+ return+ if headers and data_rows:+ items: list[str] = []+ for row in data_rows:+ pairs = ", ".join(+ f"{headers[i]}: {row[i]}" if i < len(row) else headers[i]+ for i in range(len(headers))+ )+ items.append(f"\u2022 {pairs}")+ out.append(put_fn(esc_fn("\n".join(items))))+ elif data_rows:+ # No header row — just bullet each row+ for row in data_rows:+ out.append(put_fn(esc_fn(f"\u2022 {', '.join(row)}")))+ headers.clear()+ data_rows.clear()++ for line in lines:+ m = _TABLE_ROW.match(line.strip())+ if m:+ if _TABLE_SEP.match(line.strip()):+ continue+ cells = [c.strip() for c in m.group(1).split("|")]+ if not headers:+ headers.extend(cells)+ else:+ data_rows.append(cells)+ else:+ _flush()+ out.append(line)+ _flush()+ return "\n".join(out)+++_LIST_ITEM = re.compile(r"^( *)([-*+]|\d+\.)\s+(.*)$")+_BULLETS = ["\u2022", "\u25e6", "\u25aa"] # • ◦ ▪+++def _convert_lists(text: str) -> str:+ """Convert Markdown list items to indented bullet / numbered lines."""+ lines = text.split("\n")+ out: list[str] = []+ for line in lines:+ m = _LIST_ITEM.match(line)+ if m:+ depth = len(m.group(1)) // 2+ marker, content = m.group(2), m.group(3)+ px = " " * depth+ if marker.rstrip(".").isdigit():+ out.append(f"{px}{marker} {content}")+ else:+ out.append(f"{px}{_BULLETS[min(depth, len(_BULLETS) - 1)]} {content}")+ else:+ out.append(line)+ return "\n".join(out)+++def _convert_blockquotes(text: str) -> str:+ """Convert Markdown blockquotes (> text) to <blockquote> tags."""+ lines = text.split("\n")+ out: list[str] = []+ quote_buf: list[str] = []++ def _flush():+ if quote_buf:+ out.append("<blockquote>" + "\n".join(quote_buf) + "</blockquote>")+ quote_buf.clear()++ for line in lines:+ m = re.match(r"^>\s?(.*)", line) # > is already HTML-escaped at this point+ if m:+ quote_buf.append(m.group(1))+ else:+ _flush()+ out.append(line)+ _flush()+ return "\n".join(out)