Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Notifier] Escape . char for Telegram transport
  • Loading branch information
Clément authored and derrabus committed Jun 10, 2021
commit 0aa0fcbbbcf049950ca3418c59e81cb0c7a1544b
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ protected function doSend(MessageInterface $message): SentMessage

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

if (!isset($options['parse_mode'])) {
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());
}

$response = $this->client->request('POST', $endpoint, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,56 @@ public function testSendWithChannelOverride()
$this->assertEquals(1, $sentMessage->getMessageId());
$this->assertEquals('telegram://api.telegram.org?channel=defaultChannel', $sentMessage->getTransport());
}

public function testSendWithMarkdownShouldEscapeDots()
{
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->exactly(2))
->method('getStatusCode')
->willReturn(200);

$content = <<<JSON
{
"ok": true,
"result": {
"message_id": 1,
"from": {
"id": 12345678,
"first_name": "YourBot",
"username": "YourBot"
},
"chat": {
"id": 1234567890,
"first_name": "John",
"last_name": "Doe",
"username": "JohnDoe",
"type": "private"
},
"date": 1459958199,
"text": "Hello from Bot!"
}
}
JSON;

$response->expects($this->once())
->method('getContent')
->willReturn($content)
;

$expectedBody = [
'chat_id' => 'testChannel',
'text' => 'I contain a \.',
'parse_mode' => 'MarkdownV2',
];

$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
$this->assertSame($expectedBody, json_decode($options['body'], true));

return $response;
});

$transport = $this->createTransport($client, 'testChannel');

$transport->send(new ChatMessage('I contain a .'));
}
}