8000 [Notifier] Improve Telegrams markdown escaping · symfony/symfony@aa38cf6 · GitHub
[go: up one dir, main page]

Skip to content

Commit aa38cf6

Browse files
committed
[Notifier] Improve Telegrams markdown escaping
1 parent 0dff554 commit aa38cf6

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,19 @@ 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+
* These formats should be takes care of, when the message is constructed.
101+
*
102+
* @see https://core.telegram.org/bots/api#markdownv2-style
103+
*/
104+
$text = preg_replace('/([.!#>+-=|{}~])/', '\\\\$1', $text);
93105
}
94106

95107
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