Skip to content

Commit

Permalink
feat: #465 add full reactions support as defined in api 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bernard-ng committed Jul 30, 2024
1 parent 3b44cb2 commit 8b75285
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file
- Add method `\TelegramBot\Api\BotApi::validateWebAppData` to validate `window.Telegram.WebApp.initData`
- Add `\TelegramBot\Api\Types\Message::$videoNote` field
- Drop php < 8.1
- Add `\TelegramBot\Api\Types\Update::$messageReaction` field
- Add `\TelegramBot\Api\Types\Update::$messageReactionCount` field
- Add `\TelegramBot\Api\BotApi::setMessageReaction` api method

## 2.5.0 - 2023-08-09

Expand Down
3 changes: 2 additions & 1 deletion src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public static function validate($data)
return true;
}

throw new InvalidArgumentException();
$missingParams = implode(', ', array_diff(static::$requiredParams, array_keys($data)));
throw new InvalidArgumentException(sprintf("Validation failed. Missing required parameters: %s", $missingParams));
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/BotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use TelegramBot\Api\Http\CurlHttpClient;
use TelegramBot\Api\Http\HttpClientInterface;
use TelegramBot\Api\Types\ArrayOfBotCommand;
use TelegramBot\Api\Types\ArrayOfReactionType;
use TelegramBot\Api\Types\ArrayOfChatMemberEntity;
use TelegramBot\Api\Types\ArrayOfMessageEntity;
use TelegramBot\Api\Types\ArrayOfMessages;
Expand Down Expand Up @@ -2771,6 +2772,31 @@ public function setProxy($proxyString = '', $socks5 = false)
return $this;
}

/**
* Use this method to change the chosen reactions on a message.
* Service messages can't be reacted to.
* Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel.
*
* @param string|int $chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param int $messageId Identifier of the target message.
* @param ArrayOfReactionType $reaction A list of reaction types to set on the message.
* @param bool $isBig Pass `true` to set the reaction with a big animation
*
* @return bool
* @throws Exception
*
* @author bernard-ng <[email protected]>
*/
public function setMessageReaction($chatId, $messageId, $reaction, $isBig = false)
{
return $this->call('setMessageReaction', [
'chat_id' => $chatId,
'message_id' => $messageId,
'reaction' => $reaction,
'is_big' => $isBig
]);
}

/**
* Set an option for a cURL transfer
*
Expand Down
2 changes: 1 addition & 1 deletion src/Types/ArrayOfReactionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function fromResponse($data)
{
$arrayOfReactionTypes = [];
foreach ($data as $reactionTypeData) {
// В зависимости от типа реакции, создаем соответствующий объект
// Depending on the type of reaction, create an appropriate object
if ($reactionTypeData['type'] === 'emoji') {
$arrayOfReactionTypes[] = ReactionTypeEmoji::fromResponse($reactionTypeData);
} elseif ($reactionTypeData['type'] === 'custom_emoji') {
Expand Down
55 changes: 55 additions & 0 deletions src/Types/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Update extends BaseType implements TypeInterface
'my_chat_member' => ChatMemberUpdated::class,
'chat_member' => ChatMemberUpdated::class,
'chat_join_request' => ChatJoinRequest::class,
'message_reaction' => MessageReactionUpdated::class,
'message_reaction_count' => MessageReactionCountUpdated::class
];

/**
Expand Down Expand Up @@ -159,6 +161,25 @@ class Update extends BaseType implements TypeInterface
*/
protected $chatJoinRequest;

/**
* Optional. A reaction to a message was changed by a user.
* The bot must be an administrator in the chat and must explicitly specify 'message_reaction'
* in the list of allowed_updates to receive these updates. The update isn't received for reactions set by bots.
*
* @var MessageReactionUpdated|null
*/
protected $messageReaction;

/**
* Optional. Reactions to a message with anonymous reactions were changed.
* The bot must be an administrator in the chat and must explicitly specify 'message_reaction_count'
* in the list of allowed_updates to receive these updates.
* The updates are grouped and can be sent with delay up to a few minutes.
*
* @var MessageReactionCountUpdated|null
*/
protected $messageReactionCount;

/**
* @return int
*/
Expand Down Expand Up @@ -433,4 +454,38 @@ public function setChatJoinRequest($chatJoinRequest)
{
$this->chatJoinRequest = $chatJoinRequest;
}

/**
* @return MessageReactionUpdated|null
*/
public function getMessageReaction()
{
return $this->messageReaction;
}

/**
* @param MessageReactionUpdated|null $messageReaction
* @return void
*/
public function setMessageReaction(?MessageReactionUpdated $messageReaction)
{
$this->messageReaction = $messageReaction;
}

/**
* @return MessageReactionCountUpdated|null
*/
public function getMessageReactionCount()
{
return $this->messageReactionCount;
}

/**
* @param MessageReactionCountUpdated|null $messageReactionCount
* @return void
*/
public function setMessageReactionCount(?MessageReactionCountUpdated $messageReactionCount)
{
$this->messageReactionCount = $messageReactionCount;
}
}
40 changes: 40 additions & 0 deletions tests/Types/ArrayOfReactionTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace TelegramBot\Api\Test\Types;

use PHPUnit\Framework\TestCase;
use TelegramBot\Api\Types\ReactionTypeEmoji;
use TelegramBot\Api\Types\ArrayOfReactionType;
use TelegramBot\Api\Types\ReactionTypeCustomEmoji;

class ArrayOfReactionTypeTest extends TestCase
{
public function testFromResponse()
{
$items = ArrayOfReactionType::fromResponse([
[
'emoji' => '👍',
'type' => 'emoji'
],
[
'custom_emoji_id' => 'custom_emoji_123',
'type' => 'custom_emoji'
]
]);

$expected = [
ReactionTypeEmoji::fromResponse([
'emoji' => '👍',
'type' => 'emoji'
]),
ReactionTypeCustomEmoji::fromResponse([
'custom_emoji_id' => 'custom_emoji_123',
'type' => 'custom_emoji'
])
];

foreach ($items as $key => $item) {
$this->assertEquals($expected[$key], $item);
}
}
}
55 changes: 55 additions & 0 deletions tests/Types/MessageReactionCountUpdatedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace TelegramBot\Api\Test\Types;

use TelegramBot\Api\Test\AbstractTypeTest;
use TelegramBot\Api\Types\MessageReactionCountUpdated;

class MessageReactionCountUpdatedTest extends AbstractTypeTest
{
protected static function getType()
{
return MessageReactionCountUpdated::class;
}

public static function getMinResponse()
{
return [
'chat' => ChatTest::getMinResponse(),
'message_id' => 1,
'date' => 1682343644,
'reactions' => [
ReactionTypeEmojiTest::getMinResponse()
]
];
}

public static function getFullResponse()
{
return [
'chat' => ChatTest::getFullResponse(),
'message_id' => 1,
'date' => 1682343644,
'reactions' => [
ReactionTypeEmojiTest::getFullResponse()
]
];
}


protected function assertMinItem($item)
{
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
$this->assertEquals(1, $item->getMessageId());
$this->assertEquals(1682343644, $item->getDate());
$this->assertEquals([ReactionTypeEmojiTest::createMinInstance()], $item->getReactions());
}

protected function assertFullItem($item)
{
$this->assertEquals(ChatTest::createFullInstance(), $item->getChat());
$this->assertEquals(1, $item->getMessageId());
$this->assertEquals(1682343644, $item->getDate());
$this->assertEquals([ReactionTypeEmojiTest::createFullInstance()], $item->getReactions());
}
}
68 changes: 68 additions & 0 deletions tests/Types/MessageReactionUpdatedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace TelegramBot\Api\Test\Types;

use TelegramBot\Api\Test\AbstractTypeTest;
use TelegramBot\Api\Types\MessageReactionUpdated;

class MessageReactionUpdatedTest extends AbstractTypeTest
{
protected static function getType()
{
return MessageReactionUpdated::class;
}

public static function getMinResponse()
{
return [
'chat' => ChatTest::getMinResponse(),
'message_id' => 1,
'date' => 1682343644,
'old_reaction' => [
ReactionTypeEmojiTest::getMinResponse()
],
'new_reaction' => [
ReactionTypeCustomEmojiTest::getMinResponse()
]
];
}

public static function getFullResponse()
{
return [
'chat' => ChatTest::getMinResponse(),
'message_id' => 1,
'user' => UserTest::getMinResponse(),
'actor_chat' => ChatTest::getMinResponse(),
'date' => 1682343644,
'old_reaction' => [
ReactionTypeEmojiTest::getMinResponse()
],
'new_reaction' => [
ReactionTypeCustomEmojiTest::getMinResponse()
]
];
}

protected function assertMinItem($item)
{
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
$this->assertEquals(1, $item->getMessageId());
$this->assertEquals(1682343644, $item->getDate());
$this->assertEquals([ReactionTypeEmojiTest::createMinInstance()], $item->getOldReaction());
$this->assertEquals([ReactionTypeCustomEmojiTest::createMinInstance()],$item->getNewReaction());

$this->assertNull($item->getUser());
$this->assertNull($item->getActorChat());
}

protected function assertFullItem($item)
{
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
$this->assertEquals(1, $item->getMessageId());
$this->assertEquals(UserTest::createMinInstance(), $item->getUser());
$this->assertEquals(ChatTest::createMinInstance(), $item->getActorChat());
$this->assertEquals([ReactionTypeEmojiTest::createMinInstance()], $item->getOldReaction());
$this->assertEquals([ReactionTypeCustomEmojiTest::createMinInstance()],$item->getNewReaction());
}
}
8 changes: 8 additions & 0 deletions tests/Types/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace TelegramBot\Api\Test\Types;

use TelegramBot\Api\Test\AbstractTypeTest;
use TelegramBot\Api\Types\MessageReactionUpdated;
use TelegramBot\Api\Types\MessageReactionCountUpdated;
use TelegramBot\Api\Test\Types\Inline\ChosenInlineResultTest;
use TelegramBot\Api\Test\Types\Inline\InlineQueryTest;
use TelegramBot\Api\Test\Types\Payments\Query\PreCheckoutQueryTest;
Expand Down Expand Up @@ -39,6 +41,8 @@ public static function getFullResponse()
'poll_answer' => PollAnswerTest::getMinResponse(),
'poll' => PollTest::getMinResponse(),
'chat_join_request' => ChatJoinRequestTest::getMinResponse(),
'message_reaction' => MessageReactionUpdatedTest::getMinResponse(),
'message_reaction_count' => MessageReactionCountUpdatedTest::getMinResponse(),
];
}

Expand All @@ -63,6 +67,8 @@ protected function assertMinItem($item)
$this->assertNull($item->getMyChatMember());
$this->assertNull($item->getChatMember());
$this->assertNull($item->getChatJoinRequest());
$this->assertNull($item->getMessageReaction());
$this->assertNull($item->getMessageReactionCount());
}

/**
Expand All @@ -84,5 +90,7 @@ protected function assertFullItem($item)
$this->assertEquals(PollAnswerTest::createMinInstance(), $item->getPollAnswer());
$this->assertEquals(PollTest::createMinInstance(), $item->getPoll());
$this->assertEquals(ChatJoinRequestTest::createMinInstance(), $item->getChatJoinRequest());
$this->assertEquals(MessageReactionUpdatedTest::createMinInstance(), $item->getMessageReaction());
$this->assertEquals(MessageReactionCountUpdatedTest::createMinInstance(), $item->getMessageReactionCount());
}
}

0 comments on commit 8b75285

Please sign in to comment.