From 1189fd47f87bb1898cba1d42ed4df310b8a9a76b Mon Sep 17 00:00:00 2001 From: bernard-ng Date: Fri, 11 Oct 2024 22:21:34 +0200 Subject: [PATCH 1/3] 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()); } } From 6d0d893a7787736e08a98ff771042641001068b1 Mon Sep 17 00:00:00 2001 From: bernard-ng Date: Fri, 11 Oct 2024 23:37:10 +0200 Subject: [PATCH 2/3] feat: Working on Behalf of Business Accounts --- CHANGELOG.md | 1 + src/BotApi.php | 110 ++++++++++++++++++------- tests/Types/BusinessConnectionTest.php | 1 - tests/Types/UpdateTest.php | 1 - 4 files changed, 80 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e24fba9c..1e8472e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file ## 3.0.0 - YYYY-MM-DD +- Add Added the parameter `business_connection_id` to `\TelegramBot\Api\BotApi` send methods - Add `\TelegramBot\Api\BotApi::getBusinessConnection` api method - Add `\TelegramBot\Api\Types\Update::$deletedBusinessMessages` field - Add `\TelegramBot\Api\Types\Update::$editedBusinessMessage` field diff --git a/src/BotApi.php b/src/BotApi.php index 4fd9f96f..53a1ce07 100644 --- a/src/BotApi.php +++ b/src/BotApi.php @@ -344,6 +344,7 @@ public static function jsonValidate($jsonString, $asArray) * @param bool|null $allowSendingWithoutReply * @param ReplyParameters|null $replyParameters Description of the message to reply to. * @param LinkPreviewOptions|null $linkPreviewOptions Link preview generation options for the message. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws InvalidArgumentException @@ -361,7 +362,8 @@ public function sendMessage( $protectContent = null, $allowSendingWithoutReply = null, $replyParameters = null, - $linkPreviewOptions = null + $linkPreviewOptions = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -394,7 +396,8 @@ public function sendMessage( 'disable_notification' => (bool) $disableNotification, 'protect_content' => (bool) $protectContent, 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), - 'link_preview_options' => is_null($linkPreviewOptions) ? $linkPreviewOptions : $linkPreviewOptions->toJson() + 'link_preview_options' => is_null($linkPreviewOptions) ? $linkPreviewOptions : $linkPreviewOptions->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -475,6 +478,7 @@ public function copyMessage( * @param bool|null $protectContent * @param bool|null $allowSendingWithoutReply * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws Exception @@ -490,7 +494,8 @@ public function sendContact( $messageThreadId = null, $protectContent = null, $allowSendingWithoutReply = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -514,7 +519,8 @@ public function sendContact( 'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(), 'disable_notification' => (bool) $disableNotification, 'protect_content' => (bool) $protectContent, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -532,15 +538,17 @@ public function sendContact( * * @param int $chatId * @param string $action + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return bool * @throws Exception */ - public function sendChatAction($chatId, $action) + public function sendChatAction($chatId, $action, $businessConnectionId = null) { return $this->call('sendChatAction', [ 'chat_id' => $chatId, 'action' => $action, + 'business_connection_id' => $businessConnectionId ]); } @@ -695,6 +703,7 @@ public function getUpdates($offset = 0, $limit = 100, $timeout = 0) * @param bool|null $protectContent * @param bool|null $allowSendingWithoutReply * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @@ -711,7 +720,8 @@ public function sendLocation( $messageThreadId = null, $protectContent = null, $allowSendingWithoutReply = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -735,7 +745,8 @@ public function sendLocation( 'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(), 'disable_notification' => (bool) $disableNotification, 'protect_content' => (bool) $protectContent, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -826,6 +837,7 @@ public function stopMessageLiveLocation( * @param bool|null $protectContent * @param bool|null $allowSendingWithoutReply * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws Exception @@ -843,7 +855,8 @@ public function sendVenue( $messageThreadId = null, $protectContent = null, $allowSendingWithoutReply = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -869,7 +882,8 @@ public function sendVenue( 'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(), 'disable_notification' => (bool) $disableNotification, 'protect_content' => (bool) $protectContent, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -885,6 +899,7 @@ public function sendVenue( * @param bool $allowSendingWithoutReply Pass True if the message should be sent even if the specified replied-to message is not found * @param string|null $messageThreadId * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws InvalidArgumentException @@ -899,7 +914,8 @@ public function sendSticker( $protectContent = false, $allowSendingWithoutReply = false, $messageThreadId = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -921,7 +937,8 @@ public function sendSticker( 'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(), 'disable_notification' => (bool) $disableNotification, 'protect_content' => (bool) $protectContent, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -1161,6 +1178,7 @@ public function setStickerSetThumb($name, $userId, $thumb = null) * @param bool|null $allowSendingWithoutReply * @param \CURLFile|\CURLStringFile|string|null $thumbnail * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws InvalidArgumentException @@ -1180,7 +1198,8 @@ public function sendVideo( $protectContent = null, $allowSendingWithoutReply = null, $thumbnail = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -1207,7 +1226,8 @@ public function sendVideo( 'parse_mode' => $parseMode, 'protect_content' => (bool) $protectContent, 'thumbnail' => $thumbnail, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -1229,6 +1249,7 @@ public function sendVideo( * @param bool|null $allowSendingWithoutReply * @param \CURLFile|\CURLStringFile|string|null $thumbnail * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws InvalidArgumentException @@ -1247,7 +1268,8 @@ public function sendAnimation( $protectContent = null, $allowSendingWithoutReply = null, $thumbnail = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -1273,7 +1295,8 @@ public function sendAnimation( 'parse_mode' => $parseMode, 'protect_content' => (bool) $protectContent, 'thumbnail' => $thumbnail, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -1298,6 +1321,7 @@ public function sendAnimation( * @param int|null $messageThreadId * @param bool|null $protectContent * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws InvalidArgumentException @@ -1315,7 +1339,8 @@ public function sendVoice( $parseMode = null, $messageThreadId = null, $protectContent = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -1340,7 +1365,8 @@ public function sendVoice( 'disable_notification' => (bool) $disableNotification, 'parse_mode' => $parseMode, 'protect_content' => (bool) $protectContent, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -1403,6 +1429,7 @@ public function forwardMessage( * @param bool|null $allowSendingWithoutReply * @param \CURLFile|\CURLStringFile|string|null $thumbnail * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws InvalidArgumentException @@ -1425,7 +1452,8 @@ public function sendAudio( $protectContent = null, $allowSendingWithoutReply = null, $thumbnail = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -1451,7 +1479,8 @@ public function sendAudio( 'parse_mode' => $parseMode, 'protect_content' => (bool) $protectContent, 'thumbnail' => $thumbnail, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -1469,6 +1498,7 @@ public function sendAudio( * @param bool|null $protectContent * @param bool|null $allowSendingWithoutReply * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws InvalidArgumentException @@ -1485,7 +1515,8 @@ public function sendPhoto( $messageThreadId = null, $protectContent = null, $allowSendingWithoutReply = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -1509,7 +1540,8 @@ public function sendPhoto( 'disable_notification' => (bool) $disableNotification, 'parse_mode' => $parseMode, 'protect_content' => (bool) $protectContent, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -1529,6 +1561,7 @@ public function sendPhoto( * @param bool|null $allowSendingWithoutReply * @param \CURLFile|\CURLStringFile|string|null $thumbnail * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws InvalidArgumentException @@ -1546,7 +1579,8 @@ public function sendDocument( $protectContent = null, $allowSendingWithoutReply = null, $thumbnail = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -1571,7 +1605,8 @@ public function sendDocument( 'parse_mode' => $parseMode, 'protect_content' => (bool) $protectContent, 'thumbnail' => $thumbnail, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -2581,6 +2616,7 @@ public function getChatAdministrators($chatId) * @param bool|null $allowSendingWithoutReply * @param \CURLFile|\CURLStringFile|string|null $thumbnail * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws InvalidArgumentException @@ -2598,7 +2634,8 @@ public function sendVideoNote( $protectContent = null, $allowSendingWithoutReply = null, $thumbnail = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -2623,7 +2660,8 @@ public function sendVideoNote( 'disable_notification' => (bool) $disableNotification, 'protect_content' => (bool) $protectContent, 'thumbnail' => $thumbnail, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -2640,6 +2678,7 @@ public function sendVideoNote( * @param bool|null $allowSendingWithoutReply * @param array $attachments Attachments to use in attach:// * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message[] * @throws Exception @@ -2653,7 +2692,8 @@ public function sendMediaGroup( $protectContent = null, $allowSendingWithoutReply = null, $attachments = [], - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -2674,7 +2714,8 @@ public function sendMediaGroup( 'message_thread_id' => $messageThreadId, 'disable_notification' => (bool) $disableNotification, 'protect_content' => (bool) $protectContent, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ] + $attachments)); } @@ -2701,6 +2742,7 @@ public function sendMediaGroup( * @param bool|null $protectContent * @param bool|null $allowSendingWithoutReply * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws Exception @@ -2722,7 +2764,8 @@ public function sendPoll( $messageThreadId = null, $protectContent = null, $allowSendingWithoutReply = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -2750,7 +2793,8 @@ public function sendPoll( 'message_thread_id' => $messageThreadId, 'reply_markup' => $replyMarkup === null ? $replyMarkup : $replyMarkup->toJson(), 'protect_content' => (bool) $protectContent, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -2774,6 +2818,7 @@ public function sendPoll( * @param int|null $messageThreadId * @param bool|null $protectContent * @param ReplyParameters|null $replyParameters Description of the message to reply to. + * @param string|null $businessConnectionId Unique identifier of the business connection on behalf of which the message will be sent * * @return Message * @throws Exception @@ -2789,7 +2834,8 @@ public function sendDice( $replyMarkup = null, $messageThreadId = null, $protectContent = null, - $replyParameters = null + $replyParameters = null, + $businessConnectionId = null ) { if (null !== $replyToMessageId || null !== $allowSendingWithoutReply) { @trigger_error( @@ -2811,7 +2857,8 @@ public function sendDice( 'message_thread_id' => $messageThreadId, 'reply_markup' => $replyMarkup === null ? $replyMarkup : $replyMarkup->toJson(), 'protect_content' => (bool) $protectContent, - 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson() + 'reply_parameters' => is_null($replyParameters) ? $replyParameters : $replyParameters->toJson(), + 'business_connection_id' => $businessConnectionId ])); } @@ -3191,6 +3238,7 @@ public function getUserChatBoosts($chatId, $userId) * @return BusinessConnection * @throws Exception * + * @since Bot API 7.2 * @author bernard-ng */ public function getBusinessConnection($businessConnectionId) diff --git a/tests/Types/BusinessConnectionTest.php b/tests/Types/BusinessConnectionTest.php index caaecb6e..f45dae8b 100644 --- a/tests/Types/BusinessConnectionTest.php +++ b/tests/Types/BusinessConnectionTest.php @@ -9,7 +9,6 @@ class BusinessConnectionTest extends AbstractTypeTest { - protected static function getType() { return BusinessConnection::class; diff --git a/tests/Types/UpdateTest.php b/tests/Types/UpdateTest.php index e32644f1..66c0cec9 100644 --- a/tests/Types/UpdateTest.php +++ b/tests/Types/UpdateTest.php @@ -3,7 +3,6 @@ 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; From cfde76a4e69e0f0d6d8bbabf8d7d6935b3eb7686 Mon Sep 17 00:00:00 2001 From: bernard-ng Date: Sat, 12 Oct 2024 03:04:33 +0200 Subject: [PATCH 3/3] Other Changes --- CHANGELOG.md | 1 + src/BotApi.php | 28 +++++ src/Types/InputSticker.php | 164 +++++++++++++++++++++++++ tests/Types/BusinessConnectionTest.php | 2 - tests/Types/InputStickerTest.php | 50 ++++++++ 5 files changed, 243 insertions(+), 2 deletions(-) create mode 100644 src/Types/InputSticker.php create mode 100644 tests/Types/InputStickerTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e8472e5..739458d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file ## 3.0.0 - YYYY-MM-DD +- Add `\TelegramBot\Api\BotApi::replaceStickerInSet` api method - Add Added the parameter `business_connection_id` to `\TelegramBot\Api\BotApi` send methods - Add `\TelegramBot\Api\BotApi::getBusinessConnection` api method - Add `\TelegramBot\Api\Types\Update::$deletedBusinessMessages` field diff --git a/src/BotApi.php b/src/BotApi.php index 53a1ce07..3174e953 100644 --- a/src/BotApi.php +++ b/src/BotApi.php @@ -2,6 +2,7 @@ namespace TelegramBot\Api; +use TelegramBot\Api\Types\InputSticker; use TelegramBot\Api\Http\CurlHttpClient; use TelegramBot\Api\Types\UserChatBoosts; use TelegramBot\Api\Types\ReplyParameters; @@ -3248,6 +3249,33 @@ public function getBusinessConnection($businessConnectionId) ])); } + /** + * Use this method to replace an existing sticker in a sticker set with a new one. + * The method is equivalent to calling deleteStickerFromSet, then addStickerToSet, then setStickerPositionInSet. + * Returns True on success. + * + * @param int $userId User identifier of the sticker set owner + * @param string $name Sticker set name + * @param string $oldSticker File identifier of the replaced sticker + * @param InputSticker $sticker A JSON-serialized object with information about the added sticker. + * If exactly the same sticker had already been added to the set, then the set remains unchanged. + * + * @return bool + * @throws Exception + * + * @since Bot API 7.2 + * @auther bernard-ng + */ + public function replaceStickerInSet($userId, $name, $oldSticker, $sticker) + { + return $this->call('replaceStickerInSet', [ + 'user_id' => $userId, + 'name' => $name, + 'old_sticker' => $oldSticker, + 'sticker' => $sticker + ]); + } + /** * Set an option for a cURL transfer * diff --git a/src/Types/InputSticker.php b/src/Types/InputSticker.php new file mode 100644 index 00000000..d46b3fcb --- /dev/null +++ b/src/Types/InputSticker.php @@ -0,0 +1,164 @@ + + */ +class InputSticker extends BaseType implements TypeInterface +{ + /** + * {@inheritdoc} + * + * @var array + */ + protected static $requiredParams = ['sticker', 'format', 'emoji_list']; + + /** + * {@inheritdoc} + * + * @var array + */ + protected static $map = [ + 'sticker' => true, + 'format' => true, + 'emoji_list' => true, + 'mask_position' => MaskPosition::class, + 'keywords' => true, + ]; + + /** + * The added sticker. + * Pass a file_id as a String to send a file that already exists on the Telegram servers, + * pass an HTTP URL as a String for Telegram to get a file from the Internet, + * upload a new one using multipart/form-data, or pass “attach://” to upload a new one using multipart/form-data under name. + * Animated and video stickers can't be uploaded via HTTP URL. + * + * @var string + * @see https://core.telegram.org/bots/api#sending-files + */ + protected string $sticker; + + /** + * Format of the added sticker, must be one of “static” + * for a .WEBP or .PNG image, “animated” for a .TGS animation, “video” for a WEBM video + * + * @var string + */ + protected string $format; + + /** + * List of 1-20 emoji associated with the sticker + * + * @var string[] + */ + protected array $emojiList; + + /** + * Optional. Position where the mask should be placed on faces. For “mask” stickers only. + * + * @var MaskPosition|null + */ + protected $maskPosition; + + /** + * Optional. List of 0-20 search keywords for the sticker with total length of up to 64 characters. + * For “regular” and “custom_emoji” stickers only. + * + * @var string[] + */ + protected $keywords; + + /** + * @return string + */ + public function getSticker() + { + return $this->sticker; + } + + /** + * @param string $sticker + * @return void + */ + public function setSticker(string $sticker) + { + $this->sticker = $sticker; + } + + /** + * @return string + */ + public function getFormat() + { + return $this->format; + } + + /** + * @param string $format + * @return void + */ + public function setFormat(string $format) + { + $this->format = $format; + } + + /** + * @return string[] + */ + public function getEmojiList() + { + return $this->emojiList; + } + + /** + * @param string[] $emojiList + * @return void + */ + public function setEmojiList(array $emojiList) + { + $this->emojiList = $emojiList; + } + + /** + * @return MaskPosition|null + */ + public function getMaskPosition() + { + return $this->maskPosition; + } + + /** + * @param MaskPosition|null $maskPosition + * @return void + */ + public function setMaskPosition($maskPosition) + { + $this->maskPosition = $maskPosition; + } + + /** + * @return string[] + */ + public function getKeywords() + { + return $this->keywords; + } + + /** + * @param string[] $keywords + * @return void + */ + public function setKeywords(array $keywords) + { + $this->keywords = $keywords; + } +} diff --git a/tests/Types/BusinessConnectionTest.php b/tests/Types/BusinessConnectionTest.php index f45dae8b..9caa52a8 100644 --- a/tests/Types/BusinessConnectionTest.php +++ b/tests/Types/BusinessConnectionTest.php @@ -1,7 +1,5 @@ 'file', + 'format' => 'static', + 'emoji_list' => ['😀'] + ]; + } + + public static function getFullResponse() + { + return [ + 'sticker' => 'file', + 'format' => 'static', + 'emoji_list' => ['😀'], + 'mask_position' => MaskPositionTest::getMinResponse(), + 'keywords' => ['keywords'] + ]; + } + + protected function assertMinItem($item) + { + $this->assertEquals('file', $item->getSticker()); + $this->assertEquals('static', $item->getFormat()); + $this->assertEquals(['😀'], $item->getEmojiList()); + } + + protected function assertFullItem($item) + { + $this->assertEquals('file', $item->getSticker()); + $this->assertEquals('static', $item->getFormat()); + $this->assertEquals(['😀'], $item->getEmojiList()); + $this->assertEquals(MaskPositionTest::createMinInstance(), $item->getMaskPosition()); + $this->assertEquals(['keywords'], $item->getKeywords()); + } +}