From 6d0d893a7787736e08a98ff771042641001068b1 Mon Sep 17 00:00:00 2001 From: bernard-ng Date: Fri, 11 Oct 2024 23:37:10 +0200 Subject: [PATCH] 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;