8000 bug #58636 [Notifier] Improve Telegrams markdown escaping (codedge) · symfony/symfony@0b6d0e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0b6d0e1

Browse files
bug #58636 [Notifier] Improve Telegrams markdown escaping (codedge)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Notifier] Improve Telegrams markdown escaping | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #51330 | License | MIT This PR reverts/improves the escaping of Telegrams MarkdownV2 characters. It fixes the currently broken escaping of every single Markdown char while not taking into account that they come in pairs. Commits ------- a210bda [Notifier] Improve Telegrams markdown escaping
2 parents 0dff554 + a210bda commit 0b6d0e1

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,20 @@ protected function doSend(MessageInterface $message): SentMessage
8989

9090
if (!isset($options['parse_mode']) || TelegramOptions::PARSE_MODE_MARKDOWN_V2 === $options['parse_mode']) {
9191
$options['parse_mode'] = TelegramOptions::PARSE_MODE_MARKDOWN_V2;
92-
$text = preg_replace('/([_*\[\]()~`>#+\-=|{}.!\\\\])/', '\\\\$1', $text);
92+
/*
93+
* Just replace the obvious chars according to Telegram documentation.
94+
* Do not try to find pairs or replace chars, that occur in pairs like
95+
* - *bold text*
96+
* - _italic text_
97+
* - __underlined text__
98+
* - various notations of images, f. ex. [title](url)
99+
* - `code samples`.
100+
*
101+
* These formats should be taken care of when the message is constructed.
102+
*
103+
* @see https://core.telegram.org/bots/api#markdownv2-style
104+
*/
105+
$text = preg_replace('/([.!#>+-=|{}~])/', '\\\\$1', $text);
93106
}
94107

95108
if (isset($options['upload'])) {

src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function testSendWithMarkdownShouldEscapeSpecialCharacters()
265265

266266
$expectedBody = [
267267
'chat_id' => 'testChannel',
268-
'text' => 'I contain special characters \_ \* \[ \] \( \) \~ \` \> \# \+ \- \= \| \{ \} \. \! \\\\ to send\.',
268+
'text' => 'I contain special characters _ * [ ] ( ) \~ ` \> \# \+ \- \= \| \{ \} \. \! \ to send\.',
269269
'parse_mode' => 'MarkdownV2',
270270
];
271271

0 commit comments

Comments
 (0)
0