Skip to content

feat(telegram): escape-first markdown→HTML converter — headers/links/blockquotes/code-language, entity-safe splitting #2277

Description

@AndriiPasternak31

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
  • > quote blocks → 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):

  1. Extract fenced code blocks to placeholders (preserving language)
  2. HTML-escape the remaining text
  3. Convert: headers → <b><u>…</u></b>/<b>…</b>; links → <a href>; >-runs → <blockquote> (auto-expandable beyond ~5 lines); tables → <pre>; ||…|| → <tg-spoiler>; existing bold/italic/inline-code/strike rules
  4. Re-insert code blocks as <pre><code class="language-X">
  5. Make _split_message close/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

  1. added
    priority-p2
    type-feature
    theme-channels
    complexity-medium
    on Aug 18, 2026
  2. added
    status-ready
    on Aug 18, 2026
  3. added
    status-in-dev
    on Aug 20, 2026
  4. removed
    status-ready
    on Aug 24, 2026
  5. removed
    status-in-dev
    on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-mediumpriority-p2theme-channelstype-feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions