From 1189fd47f87bb1898cba1d42ed4df310b8a9a76b Mon Sep 17 00:00:00 2001 From: bernard-ng Date: Fri, 11 Oct 2024 22:21:34 +0200 Subject: [PATCH] feat: Integration with Business Accounts --- CHANGELOG.md | 5 + src/BotApi.php | 19 ++++ src/Types/Update.php | 101 ++++++++++++++++++++ tests/Types/BusinessConnectionTest.php | 61 ++++++++++++ tests/Types/BusinessMessagesDeletedTest.php | 48 ++++++++++ tests/Types/UpdateTest.php | 13 +++ 6 files changed, 247 insertions(+) create mode 100644 tests/Types/BusinessConnectionTest.php create mode 100644 tests/Types/BusinessMessagesDeletedTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 60817072..e24fba9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file ## 3.0.0 - YYYY-MM-DD +- Add `\TelegramBot\Api\BotApi::getBusinessConnection` api method +- Add `\TelegramBot\Api\Types\Update::$deletedBusinessMessages` field +- Add `\TelegramBot\Api\Types\Update::$editedBusinessMessage` field +- Add `\TelegramBot\Api\Types\Update::$businessMessage` field +- Add `\TelegramBot\Api\Types\Update::$businessConnection` field - Add `\TelegramBot\Api\Types\Update::$myChatMember` field - Add `\TelegramBot\Api\Types\Update::$chatMember` field - Add `\TelegramBot\Api\Types\Update::$chatJoinRequest` field diff --git a/src/BotApi.php b/src/BotApi.php index ef9ddccd..4fd9f96f 100644 --- a/src/BotApi.php +++ b/src/BotApi.php @@ -8,6 +8,7 @@ use TelegramBot\Api\Http\HttpClientInterface; use TelegramBot\Api\Types\ArrayOfBotCommand; use TelegramBot\Api\Types\LinkPreviewOptions; +use TelegramBot\Api\Types\BusinessConnection; use TelegramBot\Api\Types\ArrayOfReactionType; use TelegramBot\Api\Types\ArrayOfChatMemberEntity; use TelegramBot\Api\Types\ArrayOfMessageEntity; @@ -3181,6 +3182,24 @@ public function getUserChatBoosts($chatId, $userId) ])); } + /** + * Use this method to get information about the connection of the bot with a business account. + * Returns a BusinessConnection object on success. + * + * @param string $businessConnectionId Unique identifier for the business connection + * + * @return BusinessConnection + * @throws Exception + * + * @author bernard-ng + */ + public function getBusinessConnection($businessConnectionId) + { + return BusinessConnection::fromResponse($this->call('getBusinessConnection', [ + 'business_connection_id' => $businessConnectionId + ])); + } + /** * Set an option for a cURL transfer * diff --git a/src/Types/Update.php b/src/Types/Update.php index 0294b71e..3e9044b0 100644 --- a/src/Types/Update.php +++ b/src/Types/Update.php @@ -50,6 +50,10 @@ class Update extends BaseType implements TypeInterface 'message_reaction_count' => MessageReactionCountUpdated::class, 'chat_boost' => ChatBoostUpdated::class, 'chat_boost_removed' => ChatBoostRemoved::class, + 'business_connection' => BusinessConnection::class, + 'business_message' => Message::class, + 'edited_business_message' => Message::class, + 'deleted_business_messages' => BusinessMessagesDeleted::class ]; /** @@ -198,6 +202,35 @@ class Update extends BaseType implements TypeInterface */ protected $chatBoostRemoved; + /** + * Optional. The bot was connected to or disconnected from a business account, + * or a user edited an existing connection with the bot + * + * @var BusinessConnection|null + */ + protected $businessConnection; + + /** + * Optional. New message from a connected business account + * + * @var Message|null + */ + protected $businessMessage; + + /** + * Optional. New version of a message from a connected business account + * + * @var Message|null + */ + protected $editedBusinessMessage; + + /** + * Optional. Messages were deleted from a connected business account + * + * @var BusinessMessagesDeleted|null + */ + protected $deletedBusinessMessages; + /** * @return int */ @@ -540,4 +573,72 @@ public function setChatBoostRemoved($chatBoostRemoved) { $this->chatBoostRemoved = $chatBoostRemoved; } + + /** + * @return BusinessConnection|null + */ + public function getBusinessConnection() + { + return $this->businessConnection; + } + + /** + * @param BusinessConnection|null $businessConnection + * @return void + */ + public function setBusinessConnection($businessConnection) + { + $this->businessConnection = $businessConnection; + } + + /** + * @return Message|null + */ + public function getBusinessMessage() + { + return $this->businessMessage; + } + + /** + * @param Message|null $businessMessage + * @return void + */ + public function setBusinessMessage($businessMessage) + { + $this->businessMessage = $businessMessage; + } + + /** + * @return Message|null + */ + public function getEditedBusinessMessage() + { + return $this->editedBusinessMessage; + } + + /** + * @param Message|null $editedBusinessMessage + * @return void + */ + public function setEditedBusinessMessage($editedBusinessMessage) + { + $this->editedBusinessMessage = $editedBusinessMessage; + } + + /** + * @return BusinessMessagesDeleted|null + */ + public function getDeletedBusinessMessages() + { + return $this->deletedBusinessMessages; + } + + /** + * @param BusinessMessagesDeleted|null $deletedBusinessMessages + * @return void + */ + public function setDeletedBusinessMessages($deletedBusinessMessages) + { + $this->deletedBusinessMessages = $deletedBusinessMessages; + } } diff --git a/tests/Types/BusinessConnectionTest.php b/tests/Types/BusinessConnectionTest.php new file mode 100644 index 00000000..caaecb6e --- /dev/null +++ b/tests/Types/BusinessConnectionTest.php @@ -0,0 +1,61 @@ + 1, + 'user' => UserTest::getMinResponse(), + 'user_chat_id' => 1, + 'date' => 1682343643, + 'can_reply' => true, + 'is_enabled' => true + ]; + } + + public static function getFullResponse() + { + return [ + 'id' => 1, + 'user' => UserTest::getMinResponse(), + 'user_chat_id' => 1, + 'date' => 1682343643, + 'can_reply' => true, + 'is_enabled' => true + ]; + } + + protected function assertMinItem($item) + { + $this->assertEquals(1, $item->getId()); + $this->assertEquals(UserTest::createMinInstance(), $item->getUser()); + $this->assertEquals(1, $item->getUserChatId()); + $this->assertEquals(1682343643, $item->getDate()); + $this->assertTrue($item->getCanReply()); + $this->assertTrue($item->getIsEnabled()); + } + + protected function assertFullItem($item) + { + $this->assertEquals(1, $item->getId()); + $this->assertEquals(UserTest::createMinInstance(), $item->getUser()); + $this->assertEquals(1, $item->getUserChatId()); + $this->assertEquals(1682343643, $item->getDate()); + $this->assertTrue($item->getCanReply()); + $this->assertTrue($item->getIsEnabled()); + } +} diff --git a/tests/Types/BusinessMessagesDeletedTest.php b/tests/Types/BusinessMessagesDeletedTest.php new file mode 100644 index 00000000..e20a6149 --- /dev/null +++ b/tests/Types/BusinessMessagesDeletedTest.php @@ -0,0 +1,48 @@ + 'id', + 'chat' => ChatTest::getMinResponse(), + 'message_ids' => [1, 2, 3], + ]; + } + + public static function getFullResponse() + { + return [ + 'business_connection_id' => 'id', + 'chat' => ChatTest::getMinResponse(), + 'message_ids' => [1, 2, 3], + ]; + } + + protected function assertMinItem($item) + { + $this->assertEquals('id', $item->getBusinessConnectionId()); + $this->assertEquals(ChatTest::createMinInstance(), $item->getChat()); + $this->assertEquals([1, 2, 3], $item->getMessageIds()); + } + + protected function assertFullItem($item) + { + $this->assertEquals('id', $item->getBusinessConnectionId()); + $this->assertEquals(ChatTest::createMinInstance(), $item->getChat()); + $this->assertEquals([1, 2, 3], $item->getMessageIds()); + } +} diff --git a/tests/Types/UpdateTest.php b/tests/Types/UpdateTest.php index fb730af2..e32644f1 100644 --- a/tests/Types/UpdateTest.php +++ b/tests/Types/UpdateTest.php @@ -3,6 +3,7 @@ namespace TelegramBot\Api\Test\Types; use TelegramBot\Api\Test\AbstractTypeTest; +use TelegramBot\Api\Types\BusinessMessagesDeleted; use TelegramBot\Api\Test\Types\Inline\ChosenInlineResultTest; use TelegramBot\Api\Test\Types\Inline\InlineQueryTest; use TelegramBot\Api\Test\Types\Payments\Query\PreCheckoutQueryTest; @@ -43,6 +44,10 @@ public static function getFullResponse() 'message_reaction_count' => MessageReactionCountUpdatedTest::getMinResponse(), 'chat_boost' => ChatBoostUpdatedTest::getMinResponse(), 'chat_boost_removed' => ChatBoostRemovedTest::getMinResponse(), + 'business_connection' => BusinessConnectionTest::getMinResponse(), + 'business_message' => MessageTest::getMinResponse(), + 'edited_business_message' => MessageTest::getMinResponse(), + 'deleted_business_messages' => BusinessMessagesDeletedTest::getMinResponse(), ]; } @@ -71,6 +76,10 @@ protected function assertMinItem($item) $this->assertNull($item->getMessageReactionCount()); $this->assertNull($item->getChatBoost()); $this->assertNull($item->getChatBoostRemoved()); + $this->assertNull($item->getBusinessConnection()); + $this->assertNull($item->getBusinessMessage()); + $this->assertNull($item->getEditedBusinessMessage()); + $this->assertNull($item->getDeletedBusinessMessages()); } /** @@ -96,5 +105,9 @@ protected function assertFullItem($item) $this->assertEquals(MessageReactionCountUpdatedTest::createMinInstance(), $item->getMessageReactionCount()); $this->assertEquals(ChatBoostUpdatedTest::createMinInstance(), $item->getChatBoost()); $this->assertEquals(ChatBoostRemovedTest::createMinInstance(), $item->getChatBoostRemoved()); + $this->assertEquals(BusinessConnectionTest::createMinInstance(), $item->getBusinessConnection()); + $this->assertEquals(MessageTest::createMinInstance(), $item->getBusinessMessage()); + $this->assertEquals(MessageTest::createMinInstance(), $item->getEditedBusinessMessage()); + $this->assertEquals(BusinessMessagesDeletedTest::createMinInstance(), $item->getDeletedBusinessMessages()); } }