From d9d4fe5fd2c13faeeef6c1642bc9056fbb148eb3 Mon Sep 17 00:00:00 2001 From: bernard-ng Date: Tue, 30 Jul 2024 23:20:17 +0200 Subject: [PATCH] feat: #465 add Chat Boost as defined in api 7.0 --- CHANGELOG.md | 1 + src/BaseType.php | 2 +- src/BotApi.php | 21 +++++++++++ src/Types/Update.php | 54 +++++++++++++++++++++++++++- tests/Types/ChatBoostRemovedTest.php | 50 ++++++++++++++++++++++++++ tests/Types/ChatBoostSourceTest.php | 42 ++++++++++++++++++++++ tests/Types/ChatBoostTest.php | 50 ++++++++++++++++++++++++++ tests/Types/ChatBoostUpdatedTest.php | 42 ++++++++++++++++++++++ tests/Types/UpdateTest.php | 6 ++++ 9 files changed, 266 insertions(+), 2 deletions(-) create mode 100644 tests/Types/ChatBoostRemovedTest.php create mode 100644 tests/Types/ChatBoostSourceTest.php create mode 100644 tests/Types/ChatBoostTest.php create mode 100644 tests/Types/ChatBoostUpdatedTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index af12a3dc..60817072 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file - Add `\TelegramBot\Api\BotApi::deleteMessages` api method - Add `\TelegramBot\Api\BotApi::copyMessages` api method - Add `\TelegramBot\Api\BotApi::forwardMessages` api method +- Add `\TelegramBot\Api\BotApi::getUserChatBoosts` api method ### Deprecated - Deprecate `reply_to_message_id` and `allow_sending_without_reply` parameters to `\TelegramBot\Api\BotApi` methods. Use `reply_parameters` instead. diff --git a/src/BaseType.php b/src/BaseType.php index 69853391..f7f2f9ee 100644 --- a/src/BaseType.php +++ b/src/BaseType.php @@ -40,7 +40,7 @@ public static function validate($data) } $missingParams = implode(', ', array_diff(static::$requiredParams, array_keys($data))); - throw new InvalidArgumentException(sprintf('Validation failed. Missing required parameters: %s', $missingParams)); + throw new InvalidArgumentException(sprintf('%s Validation failed. Missing required parameters: %s', static::class, $missingParams)); } /** diff --git a/src/BotApi.php b/src/BotApi.php index a5603f18..ef9ddccd 100644 --- a/src/BotApi.php +++ b/src/BotApi.php @@ -3,6 +3,7 @@ namespace TelegramBot\Api; use TelegramBot\Api\Http\CurlHttpClient; +use TelegramBot\Api\Types\UserChatBoosts; use TelegramBot\Api\Types\ReplyParameters; use TelegramBot\Api\Http\HttpClientInterface; use TelegramBot\Api\Types\ArrayOfBotCommand; @@ -3160,6 +3161,26 @@ public function copyMessages( ]); } + /** + * Use this method to get the list of boosts added to a chat by a user. Requires administrator rights in the chat. + * Returns a UserChatBoosts object. + * + * @param string|int $chatId Unique identifier for the chat or username of the channel (in the format @channelusername) + * @param int $userId Unique identifier of the target user + * + * @return UserChatBoosts + * @throws Exception + * + * @author bernard-ng + */ + public function getUserChatBoosts($chatId, $userId) + { + return UserChatBoosts::fromResponse($this->call('getUserChatBoosts', [ + 'chat_id' => $chatId, + 'user_id' => $userId + ])); + } + /** * Set an option for a cURL transfer * diff --git a/src/Types/Update.php b/src/Types/Update.php index 77ea5f69..5ad03a9c 100644 --- a/src/Types/Update.php +++ b/src/Types/Update.php @@ -47,7 +47,9 @@ class Update extends BaseType implements TypeInterface 'chat_member' => ChatMemberUpdated::class, 'chat_join_request' => ChatJoinRequest::class, 'message_reaction' => MessageReactionUpdated::class, - 'message_reaction_count' => MessageReactionCountUpdated::class + 'message_reaction_count' => MessageReactionCountUpdated::class, + 'chat_boost' => ChatBoostUpdated::class, + 'chat_boost_removed' => ChatBoostRemoved::class, ]; /** @@ -180,6 +182,22 @@ class Update extends BaseType implements TypeInterface */ protected $messageReactionCount; + /** + * Optional. A chat boost was added or changed. + * The bot must be an administrator in the chat to receive these updates. + * + * @var ChatBoostUpdated|null + */ + protected $chatBoost; + + /** + * Optional. A boost was removed from a chat. + * The bot must be an administrator in the chat to receive these updates. + * + * @var ChatBoostRemoved|null + */ + protected $removedChatBoost; + /** * @return int */ @@ -488,4 +506,38 @@ public function setMessageReactionCount(?MessageReactionCountUpdated $messageRea { $this->messageReactionCount = $messageReactionCount; } + + /** + * @return ChatBoostUpdated|null + */ + public function getChatBoost() + { + return $this->chatBoost; + } + + /** + * @param ChatBoostUpdated|null $chatBoost + * @return void + */ + public function setChatBoost($chatBoost) + { + $this->chatBoost = $chatBoost; + } + + /** + * @return ChatBoostRemoved|null + */ + public function getChatBoostRemoved() + { + return $this->removedChatBoost; + } + + /** + * @param ChatBoostRemoved|null $removedChatBoost + * @return void + */ + public function setChatBoostRemoved($removedChatBoost) + { + $this->removedChatBoost = $removedChatBoost; + } } diff --git a/tests/Types/ChatBoostRemovedTest.php b/tests/Types/ChatBoostRemovedTest.php new file mode 100644 index 00000000..1fb18e80 --- /dev/null +++ b/tests/Types/ChatBoostRemovedTest.php @@ -0,0 +1,50 @@ + ChatTest::getMinResponse(), + 'boost_id' => 1, + 'remove_date' => 1682343643, + 'source' => ChatBoostSourceTest::getMinResponse() + ]; + } + + public static function getFullResponse() + { + return [ + 'chat' => ChatTest::getMinResponse(), + 'boost_id' => 1, + 'remove_date' => 1682343643, + 'source' => ChatBoostSourceTest::getMinResponse() + ]; + } + + protected function assertMinItem($item) + { + $this->assertEquals(ChatTest::createMinInstance(), $item->getChat()); + $this->assertEquals(1, $item->getBoostId()); + $this->assertEquals(1682343643, $item->getRemoveDate()); + $this->assertEquals(ChatBoostSourceTest::createMinInstance(), $item->getSource()); + } + + protected function assertFullItem($item) + { + $this->assertEquals(ChatTest::createMinInstance(), $item->getChat()); + $this->assertEquals(1, $item->getBoostId()); + $this->assertEquals(1682343643, $item->getRemoveDate()); + $this->assertEquals(ChatBoostSourceTest::createMinInstance(), $item->getSource()); + } +} diff --git a/tests/Types/ChatBoostSourceTest.php b/tests/Types/ChatBoostSourceTest.php new file mode 100644 index 00000000..35e21646 --- /dev/null +++ b/tests/Types/ChatBoostSourceTest.php @@ -0,0 +1,42 @@ + 'premium', + 'user' => UserTest::getMinResponse(), + ]; + } + + public static function getFullResponse() + { + return [ + 'source' => 'premium', + 'user' => UserTest::getMinResponse(), + ]; + } + + protected function assertMinItem($item) + { + $this->assertEquals('premium', $item->getSource()); + $this->assertEquals(UserTest::createMinInstance(), $item->getUser()); + } + + protected function assertFullItem($item) + { + $this->assertEquals('premium', $item->getSource()); + $this->assertEquals(UserTest::createMinInstance(), $item->getUser()); + } +} diff --git a/tests/Types/ChatBoostTest.php b/tests/Types/ChatBoostTest.php new file mode 100644 index 00000000..2a8fa88f --- /dev/null +++ b/tests/Types/ChatBoostTest.php @@ -0,0 +1,50 @@ + 1, + 'add_date' => 1682343643, + 'expiration_date' => 1725042370, + 'source' => ChatBoostSourceTest::getMinResponse() + ]; + } + + public static function getFullResponse() + { + return [ + 'boost_id' => 1, + 'add_date' => 1682343643, + 'expiration_date' => 1725042370, + 'source' => ChatBoostSourceTest::getMinResponse() + ]; + } + + protected function assertMinItem($item) + { + $this->assertEquals(1, $item->getBoostId()); + $this->assertEquals(1682343643, $item->getAddDate()); + $this->assertEquals(1725042370, $item->getExpirationDate()); + $this->assertEquals(ChatBoostSourceTest::createMinInstance(), $item->getSource()); + } + + protected function assertFullItem($item) + { + $this->assertEquals(1, $item->getBoostId()); + $this->assertEquals(1682343643, $item->getAddDate()); + $this->assertEquals(1725042370, $item->getExpirationDate()); + $this->assertEquals(ChatBoostSourceTest::createMinInstance(), $item->getSource()); + } +} diff --git a/tests/Types/ChatBoostUpdatedTest.php b/tests/Types/ChatBoostUpdatedTest.php new file mode 100644 index 00000000..1dc8bb74 --- /dev/null +++ b/tests/Types/ChatBoostUpdatedTest.php @@ -0,0 +1,42 @@ + ChatTest::getMinResponse(), + 'boost' => ChatBoostTest::getMinResponse(), + ]; + } + + public static function getFullResponse() + { + return [ + 'chat' => ChatTest::getMinResponse(), + 'boost' => ChatBoostTest::getMinResponse(), + ]; + } + + protected function assertMinItem($item) + { + $this->assertEquals(ChatTest::createMinInstance(), $item->getChat()); + $this->assertEquals(ChatBoostTest::createMinInstance(), $item->getBoost()); + } + + protected function assertFullItem($item) + { + $this->assertEquals(ChatTest::createMinInstance(), $item->getChat()); + $this->assertEquals(ChatBoostTest::createMinInstance(), $item->getBoost()); + } +} diff --git a/tests/Types/UpdateTest.php b/tests/Types/UpdateTest.php index e974184e..fb730af2 100644 --- a/tests/Types/UpdateTest.php +++ b/tests/Types/UpdateTest.php @@ -41,6 +41,8 @@ public static function getFullResponse() 'chat_join_request' => ChatJoinRequestTest::getMinResponse(), 'message_reaction' => MessageReactionUpdatedTest::getMinResponse(), 'message_reaction_count' => MessageReactionCountUpdatedTest::getMinResponse(), + 'chat_boost' => ChatBoostUpdatedTest::getMinResponse(), + 'chat_boost_removed' => ChatBoostRemovedTest::getMinResponse(), ]; } @@ -67,6 +69,8 @@ protected function assertMinItem($item) $this->assertNull($item->getChatJoinRequest()); $this->assertNull($item->getMessageReaction()); $this->assertNull($item->getMessageReactionCount()); + $this->assertNull($item->getChatBoost()); + $this->assertNull($item->getChatBoostRemoved()); } /** @@ -90,5 +94,7 @@ protected function assertFullItem($item) $this->assertEquals(ChatJoinRequestTest::createMinInstance(), $item->getChatJoinRequest()); $this->assertEquals(MessageReactionUpdatedTest::createMinInstance(), $item->getMessageReaction()); $this->assertEquals(MessageReactionCountUpdatedTest::createMinInstance(), $item->getMessageReactionCount()); + $this->assertEquals(ChatBoostUpdatedTest::createMinInstance(), $item->getChatBoost()); + $this->assertEquals(ChatBoostRemovedTest::createMinInstance(), $item->getChatBoostRemoved()); } }