Repository navigation
Description
Problem
All Telegram send paths already use parse_mode="HTML" (src/backend/adapters/telegram_adapter.py:905, services/proactive_message_service.py:485, services/channel_completion_report.py:253, routers/telegram.py:313), but the converter feeding them — _markdown_to_html() at src/backend/adapters/telegram_adapter.py:1143 — only handles **bold**, *italic*, fenced/inline code, and ~~strike~~. Everything else an agent emits arrives ugly or breaks the parse entirely.
1. Bug: no HTML escaping → messages silently lose ALL formatting
The converter never escapes &, <, >. Any agent output containing an angle bracket (Optional<str>, ->, pasted XML/HTML, a shell heredoc) makes Bot API reject the message with an entity-parse error; the fallback at telegram_adapter.py:938 then strips every tag and resends with parse_mode="" — so precisely the messages that contain code are the ones delivered as unformatted plain text.
2. Markdown constructs rendered as literal source
#/##/###headers → literal hash marks[text](url)links → raw markdown> quoteblocks → literal>characters; Telegram supports<blockquote>and<blockquote expandable>(collapsible — ideal for long agent reports)- markdown tables → proportional-font garbage (should be wrapped in
<pre>) ||spoiler||→ unsupported
3. Code-fence language discarded
```python becomes bare <pre>. Telegram renders real syntax highlighting for <pre><code class="language-python">…</code></pre> — the regex at telegram_adapter.py:1152 captures the language and throws it away.
4. _split_message() is not entity-safe
The chunker (telegram_adapter.py:1167) splits >4096-char messages at any paragraph/sentence boundary, including inside an open <pre>/<b> pair. The continuation chunk then fails HTML parse → plain-text fallback again. Splits must close open tags at the cut and reopen them in the next chunk.
Proposed fix
Rewrite _markdown_to_html as an ordered pipeline (single file, benefits every agent on every instance):
- Extract fenced code blocks to placeholders (preserving language)
- HTML-escape the remaining text
- Convert: headers →
<b><u>…</u></b>/<b>…</b>; links →<a href>;>-runs →<blockquote>(auto-expandablebeyond ~5 lines); tables →<pre>;||…||→<tg-spoiler>; existing bold/italic/inline-code/strike rules - Re-insert code blocks as
<pre><code class="language-X"> - Make
_split_messageclose/reopen open entities across chunk boundaries
Telegram HTML reference: https://core.telegram.org/bots/api#html-style
Verified against v0.8.5 runtime and main (telegram_adapter.py is byte-identical on both, md5 c4bfc4dc0c698badce96d7895d3f63ef).
Activity
fix(telegram): escape-first markdown→HTML converter + entity-safe spl…