8000 [Notifier] Added support to answer Telegram callback queries · symfony/symfony@f4ee6aa · GitHub
[go: up one dir, main page]

Skip to content

Commit f4ee6aa

Browse files
committed
[Notifier] Added support to answer Telegram callback queries
1 parent eea8193 commit f4ee6aa

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed

src/Symfony/Component/Notifier/Bridge/Telegram/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Added support to answer callback queries
8+
49
5.3
510
---
611

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,19 @@ public function edit(int $messageId): static
109109

110110
return $this;
111111
}
112+
113+
/**
114+
* @return $this
115+
*/
116+
public function answerCallbackQuery(string $callbackQueryId, bool $showAlert = false, int $cacheTime = 0): static
117+
{
118+
$this->options['callback_query_id'] = $callbackQueryId;
119+
$this->options['show_alert'] = $showAlert;
120+
121+
if ($cacheTime > 0) {
122+
$this->options['cache_time'] = $cacheTime;
123+
}
124+
125+
return $this;
126+
}
112127
}

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ protected function doSend(MessageInterface $message): SentMessage
8484
$options['text'] = preg_replace('/([_*\[\]()~`>#+\-=|{}.!])/', '\\\\$1', $message->getSubject());
8585
}
8686

87-
$path = isset($options['message_id']) ? 'editMessageText' : 'sendMessage';
88-
$endpoint = sprintf('https://%s/bot%s/%s', $this->getEndpoint(), $this->token, $path);
87+
$endpoint = sprintf('https://%s/bot%s/%s', $this->getEndpoint(), $this->token, $this->getPath($options));
8988

9089
$response = $this->client->request('POST', $endpoint, [
9190
'json' => array_filter($options),
@@ -100,14 +99,33 @@ protected function doSend(MessageInterface $message): SentMessage
10099
if (200 !== $statusCode) {
101100
$result = $response->toArray(false);
102101

103-
throw new TransportException('Unable to '.(isset($options['message_id']) ? 'edit' : 'post').' the Telegram message: '.$result['description'].sprintf(' (code %d).', $result['error_code']), $response);
102+
throw new TransportException('Unable to '.$this->getAction($options).' the Telegram message: '.$result['description'].sprintf(' (code %d).', $result['error_code']), $response);
104103
}
105104

106105
$success = $response->toArray(false);
107106

108107
$sentMessage = new SentMessage($message, (string) $this);
109-
$sentMessage->setMessageId($success['result']['message_id']);
110-
108+
if (isset($success['result']['message_id'])) {
109+
$sentMessage->setMessageId($success['result']['message_id']);
110+
}
111111
return $sentMessage;
112112
}
113+
114+
private function getPath(array $options): string
115+
{
116+
return match (true) {
117+
isset($options['message_id']) => 'editMessageText',
118+
isset($options['callback_query_id']) => 'answerCallbackQuery',
119+
default => 'sendMessage',
120+
};
121+
}
122+
123+
private function getAction(array $options): string
124+
{
125+
return match (true) {
126+
isset($options['message_id']) => 'edit',
127+
isset($options['callback_query_id']) => 'answer callback query',
128+
default => 'post',
129+
};
130+
}
113131
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Notifier\Bridge\Telegram\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
9+
10+
final class TelegramOptionsTest extends TestCase
11+
{
12+
public function testAnswerCallbackQuery()
13+
{
14+
$sut = new TelegramOptions();
15+
16+
$returnedSut = $sut->answerCallbackQuery('123', true, 1);
17+
18+
$this->assertSame($sut, $returnedSut);
19+
$this->assertEquals(
20+
[
21+
'callback_query_id' => '123',
22+
'show_alert' => true,
23+
'cache_time' => 1,
24+
],
25+
$sut->toArray(),
26+
);
27+
}
28+
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,37 @@ public function testSendWithOptionForEditMessage()
193193
$transport->send(new ChatMessage('testMessage', $options));
194194
}
195195

196+
public function testSendWithOptionToAnswerCallbackQuery()
197+
{
198+
$response = $this->createMock(ResponseInterface::class);
199+
$response->expects($this->exactly(2))
200+
->method('getStatusCode')
201+
->willReturn(200);
202+
203+
$content = <<<JSON
204+
{
205+
"ok": true,
206+
"result": true
207+
}
208+
JSON;
209+
210+
$response->expects($this->once())
211+
->method('getContent')
212+
->willReturn($content)
213+
;
214+
215+
$client = new MockHttpClient(function (string $method, string $url) use ($response): ResponseInterface {
216+
$this->assertStringEndsWith('/answerCallbackQuery', $url);
217+
218+
return $response;
219+
});
220+
221+
$transport = $this->createTransport($client, 'testChannel');
222+
$options = (new TelegramOptions())->answerCallbackQuery('123', true, 1);
223+
224+
$transport->send(new ChatMessage('testMessage', $options));
225+
}
226+
196227
public function testSendWithChannelOverride()
197228
{
198229
$channelOverride = 'channelOverride';

0 commit comments

Comments
 (0)
0