10BC0 [Notifier] [Telegram] Add support to answer callback queries by alexsoft · Pull Request #48374 · symfony/symfony · GitHub
[go: up one dir, main page]

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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Telegram/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add support to answer callback queries

5.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,19 @@ public function edit(int $messageId): static

return $this;
}

/**
* @return $this
*/
public function answerCallbackQuery(string $callbackQueryId, bool $showAlert = false, int $cacheTime = 0): static
{
$this->options['callback_query_id'] = $callbackQueryId;
$this->options['show_alert'] = $showAlert;

if ($cacheTime > 0) {
$this->options['cache_time'] = $cacheTime;
}

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ protected function doSend(MessageInterface $message): SentMessage
$options['text'] = preg_replace('/([_*\[\]()~`>#+\-=|{}.!])/', '\\\\$1', $message->getSubject());
}

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

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

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

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

$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($success['result']['message_id']);
if (isset($success['result']['message_id'])) {
$sentMessage->setMessageId($success['result']['message_id']);
}

return $sentMessage;
}

private function getPath(array $options): string
{
return match (true) {
isset($options['message_id']) => 'editMessageText',
isset($options['callback_query_id']) => 'answerCallbackQuery',
default => 'sendMessage',
};
}

private function getAction(array $options): string
{
return match (true) {
isset($options['message_id']) => 'edit',
isset($options['callback_query_id']) => 'answer callback query',
default => 'post',
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Telegram\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;

final class TelegramOptionsTest extends TestCase
{
/**
* @dataProvider validCacheTimeDataProvider
*/
public function testAnswerCallbackQueryWithCacheTime(int $cacheTime)
{
$options = new TelegramOptions();

$returnedOptions = $options->answerCallbackQuery('123', true, $cacheTime);

$this->assertSame($options, $returnedOptions);
$this->assertEquals(
[
'callback_query_id' => '123',
'show_alert' => true,
'cache_time' => $cacheTime,
],
$options->toArray(),
);
}

public function validCacheTimeDataProvider(): iterable
{
yield 'cache time equals 1' => [1];
yield 'cache time equals 2' => [2];
yield 'cache time equals 10' => [10];
}

/**
* @dataProvider invalidCacheTimeDataProvider
*/
public function testAnswerCallbackQuery(int $cacheTime)
{
$options = new TelegramOptions();

$returnedOptions = $options->answerCallbackQuery('123', true, $cacheTime);

$this->assertSame($options, $returnedOptions);
$this->assertEquals(
[
'callback_query_id' => '123',
'show_alert' => true,
],
$options->toArray(),
);
}

public function invalidCacheTimeDataProvider(): iterable
{
yield 'cache time equals 0' => [0];
yield 'cache time equals -1' => [-1];
yield 'cache time equals -10' => [-10];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,37 @@ public function testSendWithOptionForEditMessage()
$transport->send(new ChatMessage('testMessage', $options));
}

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

$content = <<<JSON
{
"ok": true,
"result": true
}
JSON;

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

$client = new MockHttpClient(function (string $method, string $url) use ($response): ResponseInterface {
$this->assertStringEndsWith('/answerCallbackQuery', $url);

return $response;
});

$transport = $this->createTransport($client, 'testChannel');
$options = (new TelegramOptions())->answerCallbackQuery('123', true, 1);

$transport->send(new ChatMessage('testMessage', $options));
}

public function testSendWithChannelOverride()
{
$channelOverride = 'channelOverride';
Expand Down
0