8000 [Notifier] Add support for editing Telegram messages · symfony/symfony@e489512 · GitHub
[go: up one dir, main page]

Skip to content

Commit e489512

Browse files
chr-hertelfabpot
authored andcommitted
[Notifier] Add support for editing Telegram messages
1 parent 455bd43 commit e489512

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,14 @@ public function replyMarkup(AbstractTelegramReplyMarkup $markup): static
9999

100100
return $this;
101101
}
102+
103+
/**
104+
* @return $this
105+
*/
106+
public function edit(int $messageId): static
107+
{
108+
$this->options['message_id'] = $messageId;
109+
110+
return $this;
111+
}
102112
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ protected function doSend(MessageInterface $message): SentMessage
7272
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, TelegramOptions::class));
7373
}
7474

75-
$endpoint = sprintf('https://%s/bot%s/sendMessage', $this->getEndpoint(), $this->token);
7675
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
7776
if (!isset($options['chat_id'])) {
7877
$options['chat_id'] = $message->getRecipientId() ?: $this->chatChannel;
@@ -85,6 +84,9 @@ protected function doSend(MessageInterface $message): SentMessage
8584
$options['text'] = preg_replace('/([_*\[\]()~`>#+\-=|{}.!])/', '\\\\$1', $message->getSubject());
8685
}
8786

87+
$path = isset($options['message_id']) ? 'editMessageText' : 'sendMessage';
88+
$endpoint = sprintf('https://%s/bot%s/%s', $this->getEndpoint(), $this->token, $path);
89+
8890
$response = $this->client->request('POST', $endpoint, [
8991
'json' => array_filter($options),
9092
]);
@@ -98,7 +100,7 @@ protected function doSend(MessageInterface $message): SentMessage
98100
if (200 !== $statusCode) {
99101
$result = $response->toArray(false);
100102

101-
throw new TransportException('Unable to post the Telegram message: '.$result['description'].sprintf(' (code %s).', $result['error_code']), $response);
103+
throw new TransportException('Unable to '.(isset($options['message_id']) ? 'edit' : 'post').' the Telegram message: '.$result['description'].sprintf(' (code %d).', $result['error_code']), $response);
102104
}
103105

104106
$success = $response->toArray(false);

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

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ public function unsupportedMessagesProvider(): iterable
4949
public function testSendWithErrorResponseThrowsTransportException()
5050
{
5151
$this->expectException(TransportException::class);
52-
$this->expectExceptionMessageMatches('/testDescription.+testErrorCode/');
52+
$this->expectExceptionMessageMatches('/post.+testDescription.+400/');
5353

5454
$response = $this->createMock(ResponseInterface::class);
5555
$response->expects($this->exactly(2))
5656
->method('getStatusCode')
5757
->willReturn(400);
5858
$response->expects($this->once())
5959
->method('getContent')
60-
->willReturn(json_encode(['description' => 'testDescription', 'error_code' => 'testErrorCode']));
60+
->willReturn(json_encode(['description' => 'testDescription', 'error_code' => 400]));
6161

6262
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
6363
return $response;
@@ -68,6 +68,28 @@ public function testSendWithErrorResponseThrowsTransportException()
6868
$transport->send(new ChatMessage('testMessage'));
6969
}
7070

71+
public function testSendWithErrorResponseThrowsTransportExceptionForEdit()
72+
{
73+
$this->expectException(TransportException::class);
74+
$this->expectExceptionMessageMatches('/edit.+testDescription.+404/');
75+
76+
$response = $this->createMock(ResponseInterface::class);
77+
$response->expects($this->exactly(2))
78+
->method('getStatusCode')
79+
->willReturn(400);
80+
$response->expects($this->once())
81+
->method('getContent')
82+
->willReturn(json_encode(['description' => 'testDescription', 'error_code' => 404]));
83+
84+
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
85+
return $response;
86+
});
87+
88+
$transport = $this->createTransport($client, 'testChannel');
89+
90+
$transport->send(new ChatMessage('testMessage', (new TelegramOptions())->edit(123)));
91+
}
92+
7193
public function testSendWithOptions()
7294
{
7395
$response = $this->createMock(ResponseInterface::class);
@@ -110,6 +132,7 @@ public function testSendWithOptions()
110132
];
111133

112134
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
135+
$this->assertStringEndsWith('/sendMessage', $url);
113136
$this->assertSame($expectedBody, json_decode($options['body'], true));
114137

115138
return $response;
@@ -123,6 +146,53 @@ public function testSendWithOptions()
123146
$this->assertEquals('telegram://api.telegram.org?channel=testChannel', $sentMessage->getTransport());
124147
}
125148

149+
public function testSendWithOptionForEditMessage()
150+
{
151+
$response = $this->createMock(ResponseInterface::class);
152+
$response->expects($this->exactly(2))
153+
->method('getStatusCode')
154+
->willReturn(200);
155+
156+
$content = <<<JSON
157+
{
158+
"ok": true,
159+
"result": {
160+
"message_id": 1,
161+
"from": {
162+
"id": 12345678,
163+
"first_name": "YourBot",
164+
"username": "YourBot"
165+
},
166+
"chat": {
167+
"id": 1234567890,
168+
"first_name": "John",
169+
"last_name": "Doe",
170+
"username": "JohnDoe",
171+
"type": "private"
172+
},
173+
"date": 1459958199,
174+
"text": "Hello from Bot!"
175+
}
176+
}
177+
JSON;
178+
179+
$response->expects($this->once())
180+
->method('getContent')
181+
->willReturn($content)
182+
;
183+
184+
$client = new MockHttpClient(function (string $method, string $url) use ($response): ResponseInterface {
185+
$this->assertStringEndsWith('/editMessageText', $url);
186+
187+
return $response;
188+
});
189+
190+
$transport = $this->createTransport($client, 'testChannel');
191+
$options = (new TelegramOptions())->edit(123);
192+
193+
$transport->send(new ChatMessage('testMessage', $options));
194+
}
195+
126196
public function testSendWithChannelOverride()
127197
{
128198
$channelOverride = 'channelOverride';

0 commit comments

Comments
 (0)
0