fix(telegram): switch formatting from MarkdownV2 to HTML - #1457
Closed
llbn wants to merge 1 commit into
Closed
Conversation
- Replace _escape_mdv2/_strip_mdv2 with _escape_html/_strip_html
- Refactor format_message() pipeline to produce Telegram HTML tags
- Reverse send flow to chunk-then-format, fixing invalid chunks
caused by unescaped (N/M) indicators in formatted MarkdownV2
- Add blockquote, strikethrough, table, and horizontal rule support
- Rewrite telegram-format tests for HTML assertions
Contributor
This was referenced Jul 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Switches the Telegram adapter's outbound message formatting from MarkdownV2 to HTML parse mode, fixing broken formatting on chunked messages.
Problem
The adapter formats the entire message to MarkdownV2 first, then chunks the result to fit Telegram's 4096-char limit. After chunking,
truncate_message()appends a raw(1/2)indicator to each chunk. Since(and)are MarkdownV2 special characters that must be escaped, the unescaped indicator makes the chunk invalid MarkdownV2. Telegram rejects it and the adapter falls back to plain text.An alternative to switching parse modes would be to fix the chunk indicator escaping. However, this is awkward in practice —
truncate_message()is shared across all adapters inbase.py, and nearly every bracket character ((),[],{}) is MDV2-special, making it hard to choose a safe indicator format. Fixing the indicator would also leave the underlying complexity of the MDV2 pipeline in place (18-char escaping with a placeholder protection system).Solution
Switch to Telegram's HTML parse mode. HTML only requires escaping 3 characters (
&,<,>) — parentheses are not special, so chunk indicators work without modification.The send flow is also reversed to chunk-then-format — raw markdown is chunked first (where the base chunker's splitting and indicator appending is safe), then each chunk is formatted to HTML independently.
Additional improvements
The new HTML pipeline supports elements that the MDV2 pipeline did not handle:
> text-><blockquote>) — Telegram renders these natively with a left-border indicator~~text~~-><s>) — not mapped in the old pipeline|---|separator rows -><pre>) — rendered as monospace for column alignment---->———) — visual separatorRelated Issue
Fixes #1456
Type of Change
Bug fix (non-breaking change that fixes an issue)
Changes Made
gateway/platforms/telegram.py— replace_escape_mdv2/_strip_mdv2with_escape_html/_strip_html, rewriteformat_message()pipeline for HTML output, reverse send flow to chunk-then-format, updatesend()/edit_message()to useParseMode.HTMLtests/gateway/test_telegram_format.py— rewrite all assertions for HTML output (68 tests, including chunking regression test)How to Test
pytest tests/gateway/test_telegram_format.py -v— all 68 tests should passChecklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping