]> git.giacomolli.uk - onechat/commit
Escape tags in rich replies as HTML entities
authorAnderson A. Giacomolli <redacted>
Sat, 7 Nov 2026 19:38:29 +0000 (16:38 -0300)
committerAnderson A. Giacomolli <redacted>
Sat, 7 Nov 2026 19:38:29 +0000 (16:38 -0300)
commit5bb22ac2fc501bad7327c119f1d3ac514e3d5398
tree6d3d5b59990c63b248cb2024c0960dabc576915e
parent6b21fcf78372995c6d8130c63f2f8aa2609bf7fe
Escape tags in rich replies as HTML entities

The bot writes each < of a reply outside inline code and fenced code
blocks as &lt; and each & as &amp; before it sends the reply with
sendRichMessage. Rich Markdown does not read a backslash before < as an
escape: it shows the backslash and still reads the tag as HTML. It reads
HTML entities, so &lt; shows as <. The escape of & makes an entity that
the model writes in prose, such as &lt;, show as written.

The entity fallback reads no HTML, so a rich part that Telegram refuses
goes to the fallback with the escape reversed, and the user sees the
text as the model wrote it. The reverse is exact, because after the
escape each & outside code starts an entity of the escape. The escape
and its reverse find the code of a text in the same way, so the reverse
also works on each part of the split.

The split still counts the escaped text against the rich limit, and the
fallback splits the text without the escape at its own limit. When the
split cuts a line longer than the limit, it cuts before an & in the last
four bytes, so that it does not cut an entity.

Issue: d75e312
telegram.go
telegram_markdown.go
telegram_markdown_test.go
telegram_test.go