From f4531feee8988ebad8d366cb2e0c044315271faa Mon Sep 17 00:00:00 2001 From: ruangraung Date: Tue, 9 Jun 2026 00:12:31 +0700 Subject: [PATCH] fix(telegram): improve MarkdownV2 edit fallback and fix _strip_mdv2 bold handling When edit_message(finalize=True) fails with a MarkdownV2 parse error, the silent fallback previously sent raw content with escape sequences. Now it logs the error and strips markdown formatting via _strip_mdv2() for clean plain-text fallback. Also fixes _strip_mdv2 to handle standard markdown bold (\*\*text\*\*) before MarkdownV2 bold (\*text\*), preventing half-stripped asterisks. Refs: #41955, #41732 --- gateway/platforms/telegram.py | 12 ++++++++++-- tests/gateway/test_telegram_format.py | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/gateway/platforms/telegram.py b/gateway/platforms/telegram.py index b97d430d4a4..fca6fa66255 100644 --- a/gateway/platforms/telegram.py +++ b/gateway/platforms/telegram.py @@ -181,6 +181,8 @@ def _strip_mdv2(text: str) -> str: """ # Remove escape backslashes before special characters cleaned = re.sub(r'\\([_*\[\]()~`>#\+\-=|{}.!\\])', r'\1', text) + # Remove standard markdown bold (**text** → text) BEFORE MarkdownV2 bold + cleaned = re.sub(r'\*\*([^*]+)\*\*', r'\1', cleaned) # Remove MarkdownV2 bold markers that format_message converted from **bold** cleaned = re.sub(r'\*([^*]+)\*', r'\1', cleaned) # Remove MarkdownV2 italic markers that format_message converted from *italic* @@ -2208,11 +2210,17 @@ class TelegramAdapter(BasePlatformAdapter): # "Message is not modified" is a no-op, not an error if "not modified" in str(fmt_err).lower(): return SendResult(success=True, message_id=message_id) - # Fallback: retry without markdown formatting + # Fallback: strip MarkdownV2 escapes and retry as clean plain text + logger.warning( + "[%s] MarkdownV2 edit failed, falling back to plain text: %s", + self.name, + fmt_err, + ) + _plain = _strip_mdv2(content) if content else content await self._bot.edit_message_text( chat_id=int(chat_id), message_id=int(message_id), - text=content, + text=_plain, ) return SendResult(success=True, message_id=message_id) except Exception as e: diff --git a/tests/gateway/test_telegram_format.py b/tests/gateway/test_telegram_format.py index c8fb121a173..1d3a2375a78 100644 --- a/tests/gateway/test_telegram_format.py +++ b/tests/gateway/test_telegram_format.py @@ -835,7 +835,7 @@ class TestEditMessageStreamingSafety: assert second_call == { "chat_id": 123, "message_id": 456, - "text": "final **bold**", + "text": "final bold", } @pytest.mark.asyncio