File tree Expand file tree Collapse file tree 2 files changed +15
-2
lines changed
src/Symfony/Component/Notifier/Bridge/Telegram Expand file tree Collapse file tree 2 files changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -89,7 +89,20 @@ protected function doSend(MessageInterface $message): SentMessage
89
89
90
90
if (!isset ($ options ['parse_mode ' ]) || TelegramOptions::PARSE_MODE_MARKDOWN_V2 === $ options ['parse_mode ' ]) {
91
91
$ 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 );
93
106
}
94
107
95
108
if (isset ($ options ['upload ' ])) {
Original file line number Diff line number Diff line change @@ -265,7 +265,7 @@ public function testSendWithMarkdownShouldEscapeSpecialCharacters()
265
265
266
266
$ expectedBody = [
267
267
'chat_id ' => 'testChannel ' ,
268
- 'text ' => 'I contain special characters \_ \* \[ \] \( \ ) \~ \ ` \> \# \+ \- \= \| \{ \} \. \! \\\ \ to send\. ' ,
268
+ 'text ' => 'I contain special characters _ * [ ] ( ) \~ ` \> \# \+ \- \= \| \{ \} \. \! \ to send\. ' ,
269
269
'parse_mode ' => 'MarkdownV2 ' ,
270
270
];
271
271
You can’t perform that action at this time.
0 commit comments