[Notifier] Fix escaping of MarkdownV2 markup in TelegramTransport - #65644
#65644Merged
nicolas-grekas merged 1 commit intoAug 25, 2026
symfony:6.4symfony/symfony:6.4from
nicolas-grekas:telegram-markdownv2-escaping-64nicolas-grekas/symfony:telegram-markdownv2-escaping-64Copy head branch name to clipboard
Merged
[Notifier] Fix escaping of MarkdownV2 markup in TelegramTransport#65644nicolas-grekas merged 1 commit intosymfony:6.4symfony/symfony:6.4from nicolas-grekas:telegram-markdownv2-escaping-64nicolas-grekas/symfony:telegram-markdownv2-escaping-64Copy head branch name to clipboard
nicolas-grekas merged 1 commit into
symfony:6.4symfony/symfony:6.4from
nicolas-grekas:telegram-markdownv2-escaping-64nicolas-grekas/symfony:telegram-markdownv2-escaping-64Copy head branch name to clipboard
Conversation
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.
When the parse mode is not set or is
MarkdownV2,TelegramTransportescapes reserved characters in the message text before sending it. The list of characters it escapes was adjusted by hand three times (#41600, #42721, #58636) and still contains three markup markers of MarkdownV2:~(strikethrough),|(||spoiler||) and>(block quotation). These formats cannot be used at all. Text that is already escaped by hand is escaped a second time (1\.2becomes1\\.2), which Telegram rejects.This PR replaces the character list with the rule that Telegram's own parser applies: a reserved character is escaped when Telegram would reject it, and the markup is left alone.
*bold*,_italic_,__underline__,`code`,~strikethrough~,||spoiler||,[link](url). The first five were already left alone since [Notifier] Improve Telegrams markdown escaping #58636;~and||now follow the same rule.>is left alone at the start of a line, where it starts a block quotation, including the**>form of expandable quotations. It is escaped anywhere else.!is left alone in front of[, where it starts a custom emoji or a date-time entity. It is escaped anywhere else..,#,+,-,=,{,}and a single|are escaped as before.It also fixes the character class of the old pattern:
+-=was read as a range from+to=, so digits,,,/,:,;and<were escaped too. Telegram accepts a backslash in front of any of them, so nothing was visible in the chat, but every digit counted twice towards the 4096-character limit.Behavior change for plain-text senders: a message with an unescaped
~or||, or a line starting with>, is now sent as markup, the same way*and_are since #58636. Plain text is sent as before when these characters are escaped in the message, or with theHTMLparse mode.Checks run:
./phpunit src/Symfony/Component/Notifier/Bridge/Telegramon 6.4: OK, 70 tests, 161 assertions, 1 skipped (pre-existing).\~; both pass after it.php-cs-fixer fix --dry-runon the touched files: clean.Not covered: no call to the real Telegram API was made. The rules follow the MarkdownV2 section of the Bot API documentation.
Replaces #65643.