diff --git a/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php b/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php index 60eee07d9c5d..5b5cad8e0fb8 100644 --- a/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php @@ -82,7 +82,7 @@ protected function doSend(MessageInterface $message): SentMessage if (!isset($options['parse_mode']) || TelegramOptions::PARSE_MODE_MARKDOWN_V2 === $options['parse_mode']) { $options['parse_mode'] = TelegramOptions::PARSE_MODE_MARKDOWN_V2; - $options['text'] = str_replace('.', '\.', $message->getSubject()); + $options['text'] = preg_replace('/([_*\[\]()~`>#+\-=|{}.!])/', '\\\\$1', $message->getSubject()); } $response = $this->client->request('POST', $endpoint, [ diff --git a/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportTest.php index 2b0cbbdf17c4..718f566b0c24 100644 --- a/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportTest.php @@ -186,7 +186,7 @@ public function testSendWithChannelOverride() $this->assertEquals('telegram://api.telegram.org?channel=defaultChannel', $sentMessage->getTransport()); } - public function testSendWithMarkdownShouldEscapeDots() + public function testSendWithMarkdownShouldEscapeSpecialCharacters() { $response = $this->createMock(ResponseInterface::class); $response->expects($this->exactly(2)) @@ -223,7 +223,7 @@ public function testSendWithMarkdownShouldEscapeDots() $expectedBody = [ 'chat_id' => 'testChannel', - 'text' => 'I contain a \.', + 'text' => 'I contain special characters \_ \* \[ \] \( \) \~ \` \> \# \+ \- \= \| \{ \} \. \! to send\.', 'parse_mode' => 'MarkdownV2', ]; @@ -235,6 +235,6 @@ public function testSendWithMarkdownShouldEscapeDots() $transport = $this->createTransport($client, 'testChannel'); - $transport->send(new ChatMessage('I contain a .')); + $transport->send(new ChatMessage('I contain special characters _ * [ ] ( ) ~ ` > # + - = | { } . ! to send.')); } }