8000 [Notifier] Escape `.` char for Telegram transport · symfony/symfony@0aa0fcb · GitHub
[go: up one dir, main page]

Skip to content

Commit 0aa0fcb

Browse files
Clémentderrabus
Clément
authored andcommitted
[Notifier] Escape . char for Telegram transport
1 parent 09ef709 commit 0aa0fcb

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ protected function doSend(MessageInterface $message): SentMessage
7979

8080
$options['text'] = $message->getSubject();
8181

82-
if (!isset($options['parse_mode'])) {
82+
if (!isset($options['parse_mode']) || TelegramOptions::PARSE_MODE_MARKDOWN_V2 === $options['parse_mode']) {
8383
$options['parse_mode'] = TelegramOptions::PARSE_MODE_MARKDOWN_V2;
84+
$options['text'] = str_replace('.', '\.', $message->getSubject());
8485
}
8586

8687
$response = $this->client->request('POST', $endpoint, [

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,56 @@ public function testSendWithChannelOverride()
185185
$this->assertEquals(1, $sentMessage->getMessageId());
186186
$this->assertEquals('telegram://api.telegram.org?channel=defaultChannel', $sentMessage->getTransport());
187187
}
188+
189+
public function testSendWithMarkdownShouldEscapeDots()
190+
{
191+
$response = $this->createMock(ResponseInterface::class);
192+
$response->expects($this->exactly(2))
193+
->method('getStatusCode')
194+
->willReturn(200);
195+
196+
$content = <<<JSON
197+
{
198+
"ok": true,
199+
"result": {
200+
"message_id": 1,
201+
"from": {
202+
"id": 12345678,
203+
"first_name": "YourBot",
204+
"username": "YourBot"
205+
},
206+
"chat": {
207+
"id": 1234567890,
208+
"first_name": "John",
209+
"last_name": "Doe",
210+
"username": "JohnDoe",
211+
"type": "private"
212+
},
213+
"date": 1459958199,
214+
"text": "Hello from Bot!"
215+
}
216+
}
217+
JSON;
218+
219+
$response->expects($this->once())
220+
->method('getContent')
221+
->willReturn($content)
222+
;
223+
224+
$expectedBody = [
225+
'chat_id' => 'testChannel',
226+
'text' => 'I contain a \.',
227+
'parse_mode' => 'MarkdownV2',
228+
];
229+
230+
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
231+
$this->assertSame($expectedBody, json_decode($options['body'], true));
232+
233+
return $response;
234+
});
235+
236+
$transport = $this->createTransport($client, 'testChannel');
237+
238+
$transport->send(new ChatMessage('I contain a .'));
239+
}
188240
}

0 commit comments

Comments
 (0)
0