From 891bd6f183c68f1a7fa2a6c430c8e05de5fbb984 Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 10 Oct 2024 12:15:42 -0400 Subject: [PATCH] Add SendMediaGroup method to send media albums --- src/TgBotLib/Enums/Methods.php | 3 + src/TgBotLib/Methods/SendMediaGroup.php | 115 ++++++++++++++++++ tests/TgBotLib/Methods/SendMediaGroupTest.php | 49 ++++++++ 3 files changed, 167 insertions(+) create mode 100644 src/TgBotLib/Methods/SendMediaGroup.php create mode 100644 tests/TgBotLib/Methods/SendMediaGroupTest.php diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index 7f2046a..52552cf 100644 --- a/src/TgBotLib/Enums/Methods.php +++ b/src/TgBotLib/Enums/Methods.php @@ -15,6 +15,7 @@ use TgBotLib\Methods\SendAnimation; use TgBotLib\Methods\SendAudio; use TgBotLib\Methods\SendDocument; + use TgBotLib\Methods\SendMediaGroup; use TgBotLib\Methods\SendMessage; use TgBotLib\Methods\SendPaidMedia; use TgBotLib\Methods\SendPhoto; @@ -40,6 +41,7 @@ case SEND_VOICE = 'sendVoice'; case SEND_VIDEO_NOTE = 'sendVideoNote'; case SEND_PAID_MEDIA = 'sendPaidMedia'; + case SEND_MEDIA_GROUP = 'sendMediaGroup'; /** * Executes a command on the provided bot with the given parameters. @@ -69,6 +71,7 @@ self::SEND_VOICE => SendVoice::execute($bot, $parameters), self::SEND_VIDEO_NOTE => SendVideoNote::execute($bot, $parameters), self::SEND_PAID_MEDIA => SendPaidMedia::execute($bot, $parameters), + self::SEND_MEDIA_GROUP => SendMediaGroup::execute($bot, $parameters), }; } } diff --git a/src/TgBotLib/Methods/SendMediaGroup.php b/src/TgBotLib/Methods/SendMediaGroup.php new file mode 100644 index 0000000..c5fd7a2 --- /dev/null +++ b/src/TgBotLib/Methods/SendMediaGroup.php @@ -0,0 +1,115 @@ + 10) + { + throw new TelegramException('Media array must include 2-10 items'); + } + + $hasLocalFiles = false; + $attachments = []; + $processedMedia = []; + + foreach ($parameters['media'] as $index => $mediaItem) + { + if (!($mediaItem instanceof InputMedia)) + { + throw new TelegramException('Invalid media item type'); + } + + $mediaArray = $mediaItem->toArray(); + + // Check for local files in the media + if (isset($mediaArray['media']) && is_string($mediaArray['media']) && file_exists($mediaArray['media']) && is_file($mediaArray['media'])) + { + $hasLocalFiles = true; + $attachmentKey = "file{$index}"; + $attachments[$attachmentKey] = $mediaArray['media']; + $mediaArray['media'] = "attach://{$attachmentKey}"; + } + + // Check for local thumbnail + if (isset($mediaArray['thumbnail']) && is_string($mediaArray['thumbnail']) && file_exists($mediaArray['thumbnail']) && is_file($mediaArray['thumbnail'])) + { + $hasLocalFiles = true; + $thumbKey = "thumb{$index}"; + $attachments[$thumbKey] = $mediaArray['thumbnail']; + $mediaArray['thumbnail'] = "attach://{$thumbKey}"; + } + + $processedMedia[] = $mediaArray; + } + + // Update parameters with processed media + $parameters['media'] = json_encode($processedMedia); + + // Handle reply parameters + if (isset($parameters['reply_parameters']) && $parameters['reply_parameters'] instanceof ReplyParameters) + { + $parameters['reply_parameters'] = $parameters['reply_parameters']->toArray(); + } + + $curl = $hasLocalFiles + ? self::buildMultiUpload($bot, Methods::SEND_MEDIA_GROUP->value, $attachments, $parameters) + : self::buildPost($bot, Methods::SEND_MEDIA_GROUP->value, $parameters); + + $result = self::executeCurl($curl); + + return array_map(function ($messageData) {return Message::fromArray($messageData);}, $result); + } + + /** + * @inheritDoc + */ + public static function getRequiredParameters(): ?array + { + return [ + 'chat_id', + 'media' + ]; + } + + /** + * @inheritDoc + */ + public static function getOptionalParameters(): ?array + { + return [ + 'business_connection_id', + 'message_thread_id', + 'disable_notification', + 'protect_content', + 'message_effect_id', + 'reply_parameters' + ]; + } + } \ No newline at end of file diff --git a/tests/TgBotLib/Methods/SendMediaGroupTest.php b/tests/TgBotLib/Methods/SendMediaGroupTest.php new file mode 100644 index 0000000..cf28dba --- /dev/null +++ b/tests/TgBotLib/Methods/SendMediaGroupTest.php @@ -0,0 +1,49 @@ +setAutoRetry(true); + } + + /** + * Tests the `sendMessage` function of the bot instance. + * + * @return void + */ + public function testSendMediaGroup(): void + { + $result = self::$bot->sendMediaGroup( + chat_id: TEST_CHAT_ID, + media: [ + (new InputMediaPhoto())->setMedia(self::TEST_IMAGE_PATH), + (new InputMediaPhoto())->setMedia(self::TEST_IMAGE_PATH) + ] + ); + + $this->assertIsArray($result); + + foreach ($result as $message) + { + $this->assertInstanceOf(Message::class, $message); + } + } + +}