From 5effcdf05ba54de0c20c6460e97b33e05c4ba952 Mon Sep 17 00:00:00 2001 From: mario Date: Wed, 26 Jun 2024 12:07:31 +0200 Subject: [PATCH] #498 implement sendMediaGroup method --- docs/12.features/7.attachments.md | 17 +++++++++++ docs/12.features/8.telegram-api-calls.md | 17 +++++++++++ docs/13.models/2.telegraph-chat.md | 17 +++++++++++ src/Concerns/SendsAttachments.php | 31 +++++++++++++++----- src/DTO/InlineQueryResultArticle.php | 2 +- src/DTO/InlineQueryResultContact.php | 2 +- src/DTO/InlineQueryResultLocation.php | 2 +- src/Facades/Telegraph.php | 1 + src/Models/TelegraphChat.php | 8 +++++ src/Telegraph.php | 1 + tests/TestCase.php | 1 - tests/Unit/Concerns/HasBotsAndChatsTest.php | 1 - tests/Unit/Concerns/SendsAttachmentsTest.php | 2 -- tests/Unit/Handlers/WebhookHandlerTest.php | 1 - tests/Unit/Models/TelegraphChatTest.php | 29 ++++++++++++++++++ 15 files changed, 116 insertions(+), 16 deletions(-) diff --git a/docs/12.features/7.attachments.md b/docs/12.features/7.attachments.md index 742cc753d..47255d4f9 100644 --- a/docs/12.features/7.attachments.md +++ b/docs/12.features/7.attachments.md @@ -213,3 +213,20 @@ Telegraph::document(Storage::path('brochure.pdf')) ->thumbnail(Storage::path('brochure_thumbnail.jpg')) ->send(); ``` + +### Media Group + +Group of photos, videos, documents or audios as an album can be sent through Telegraph `->mediaGroup()` method: + +```php +Telegraph::mediaGroup([ + [ + 'type' => 'photo', + 'media' => 'https://my-repository/photo1.jpg', + ], + [ + 'type' => 'photo', + 'media' => 'https://my-repository/photo2.jpg', + ] +])->send(); +``` diff --git a/docs/12.features/8.telegram-api-calls.md b/docs/12.features/8.telegram-api-calls.md index 613f706c9..3938ac06b 100644 --- a/docs/12.features/8.telegram-api-calls.md +++ b/docs/12.features/8.telegram-api-calls.md @@ -220,6 +220,23 @@ sends a photo Telegraph::photo($pathToPhotoFile)->send(); ``` +### Media Group + +sends a group of photos, videos, documents or audios as an album + +```php +Telegraph::mediaGroup([ + [ + 'type' => 'photo', + 'media' => 'https://my-repository/photo1.jpg', + ], + [ + 'type' => 'photo', + 'media' => 'https://my-repository/photo2.jpg', + ] +])->send(); +``` + ## registerBotCommands register commands in Telegram Bot in order to display them to the user when the "/" key is pressed diff --git a/docs/13.models/2.telegraph-chat.md b/docs/13.models/2.telegraph-chat.md index 3e9aedb4a..579b89426 100644 --- a/docs/13.models/2.telegraph-chat.md +++ b/docs/13.models/2.telegraph-chat.md @@ -598,3 +598,20 @@ $telegraphChat->setMenuButton()->default()->send(); //restore default $telegraphChat->setMenuButton()->commands()->send(); //show bot commands in menu button $telegraphChat->setMenuButton()->webApp("Web App", "https://my-web.app")->send(); //show start web app button ``` + +### `Media Group` + +Group of photos, videos, documents or audios as an album can be sent through Telegraph `->mediaGroup()` method: + +```php +$telegraphChat->mediaGroup([ + [ + 'type' => 'photo', + 'media' => 'https://my-repository/photo1.jpg', + ], + [ + 'type' => 'photo', + 'media' => 'https://my-repository/photo2.jpg', + ] +])->send(); +``` diff --git a/src/Concerns/SendsAttachments.php b/src/Concerns/SendsAttachments.php index adf7fbf05..358bbb98c 100644 --- a/src/Concerns/SendsAttachments.php +++ b/src/Concerns/SendsAttachments.php @@ -181,7 +181,7 @@ public function thumbnail(string $path): self $telegraph = clone $this; if (File::exists($path)) { - /* @phpstan-ignore-next-line */ + /* @phpstan-ignore-next-line */ $maxSizeKb = floatval(config('telegraph.attachments.thumbnail.max_size_kb', 200)); if (($size = $telegraph->fileSizeInKb($path)) > $maxSizeKb) { @@ -236,6 +236,22 @@ public function photo(string $path, string $filename = null): self return $telegraph; } + /** + * @param array> $mediaInputs + */ + public function mediaGroup(array $mediaInputs): self + { + $telegraph = clone $this; + + $telegraph->endpoint = self::ENDPOINT_SEND_MEDIA_GROUP; + + $telegraph->data['chat_id'] = $telegraph->getChatId(); + + $telegraph->data['media'] = $mediaInputs; + + return $telegraph; + } + private function imageHeight(string $path): int { return $this->imageDimensions($path)[1]; @@ -292,7 +308,7 @@ public function dice(string $emoji = null): self protected function attachPhoto(self $telegraph, string $path, ?string $filename): void { if (File::exists($path)) { - /* @phpstan-ignore-next-line */ + /* @phpstan-ignore-next-line */ $maxSizeInMb = floatval(config('telegraph.attachments.photo.max_size_mb', 10)); if (($size = $telegraph->fileSizeInMb($path)) > $maxSizeInMb) { @@ -310,7 +326,7 @@ protected function attachPhoto(self $telegraph, string $path, ?string $filename) throw FileException::invalidPhotoSize($totalLength, $heightWidthSumPx); } - /* @phpstan-ignore-next-line */ + /* @phpstan-ignore-next-line */ $maxRatio = floatval(config('telegraph.attachments.photo.max_ratio', 20)); if (($ratio = $height / $width) > $maxRatio || $ratio < (1 / $maxRatio)) { @@ -327,7 +343,7 @@ protected function attachPhoto(self $telegraph, string $path, ?string $filename) protected function attachAnimation(self $telegraph, string $path, ?string $filename): void { if (File::exists($path)) { - /* @phpstan-ignore-next-line */ + /* @phpstan-ignore-next-line */ $maxSizeMb = floatval(config('telegraph.attachments.animation.max_size_mb', 50)); if (($size = $telegraph->fileSizeInMb($path)) > $maxSizeMb) { @@ -347,7 +363,7 @@ protected function attachAnimation(self $telegraph, string $path, ?string $filen protected function attachVideo(self $telegraph, string $path, ?string $filename): void { if (File::exists($path)) { - /* @phpstan-ignore-next-line */ + /* @phpstan-ignore-next-line */ $maxSizeMb = floatval(config('telegraph.attachments.video.max_size_mb', 50)); if (($size = $telegraph->fileSizeInMb($path)) > $maxSizeMb) { @@ -374,7 +390,7 @@ protected function attachVideo(self $telegraph, string $path, ?string $filename) protected function attachAudio(self $telegraph, string $path, ?string $filename): void { if (File::exists($path)) { - /* @phpstan-ignore-next-line */ + /* @phpstan-ignore-next-line */ $maxSizeMb = floatval(config('telegraph.attachments.audio.max_size_mb', 50)); if (($size = $telegraph->fileSizeInMb($path)) > $maxSizeMb) { @@ -398,8 +414,7 @@ protected function attachAudio(self $telegraph, string $path, ?string $filename) protected function attachDocument(self $telegraph, string $path, ?string $filename): void { if (File::exists($path)) { - - /* @phpstan-ignore-next-line */ + /* @phpstan-ignore-next-line */ $maxSizeMb = floatval(config('telegraph.attachments.document.max_size_mb', 50)); if (($size = $telegraph->fileSizeInMb($path)) > $maxSizeMb) { diff --git a/src/DTO/InlineQueryResultArticle.php b/src/DTO/InlineQueryResultArticle.php index 2cacafeaa..8fe659022 100644 --- a/src/DTO/InlineQueryResultArticle.php +++ b/src/DTO/InlineQueryResultArticle.php @@ -108,7 +108,7 @@ public function data(): array 'thumb_height' => $this->thumbHeight, ]; - if($this->message !== null) { + if ($this->message !== null) { $data['input_message_content'] = [ 'message_text' => $this->message, 'parse_mode' => $this->parseMode ?? config('telegraph.default_parse_mode', Telegraph::PARSE_HTML), diff --git a/src/DTO/InlineQueryResultContact.php b/src/DTO/InlineQueryResultContact.php index b73110d86..007388f04 100644 --- a/src/DTO/InlineQueryResultContact.php +++ b/src/DTO/InlineQueryResultContact.php @@ -102,7 +102,7 @@ public function data(): array 'thumb_height' => $this->thumbHeight, ]; - if($this->message !== null) { + if ($this->message !== null) { $data['input_message_content'] = [ 'message_text' => $this->message, 'parse_mode' => $this->parseMode ?? config('telegraph.default_parse_mode', Telegraph::PARSE_HTML), diff --git a/src/DTO/InlineQueryResultLocation.php b/src/DTO/InlineQueryResultLocation.php index d84687fc1..3895ccf75 100644 --- a/src/DTO/InlineQueryResultLocation.php +++ b/src/DTO/InlineQueryResultLocation.php @@ -123,7 +123,7 @@ public function data(): array 'horizontal_accuracy' => $this->horizontalAccuracy, ]; - if($this->message !== null) { + if ($this->message !== null) { $data['input_message_content'] = [ 'message_text' => $this->message, 'parse_mode' => $this->parseMode ?? config('telegraph.default_parse_mode', Telegraph::PARSE_HTML), diff --git a/src/Facades/Telegraph.php b/src/Facades/Telegraph.php index 7c043185a..7cfede501 100644 --- a/src/Facades/Telegraph.php +++ b/src/Facades/Telegraph.php @@ -49,6 +49,7 @@ * @method static \DefStudio\Telegraph\Telegraph video(string $path, string $filename = null) * @method static \DefStudio\Telegraph\Telegraph audio(string $path, string $filename = null) * @method static \DefStudio\Telegraph\Telegraph dice() + * @method static \DefStudio\Telegraph\Telegraph mediaGroup(string $path, array $media) * @method static \DefStudio\Telegraph\Telegraph botUpdates() * @method static \DefStudio\Telegraph\Telegraph botInfo() * @method static \DefStudio\Telegraph\Telegraph setBaseUrl(string|null $url) diff --git a/src/Models/TelegraphChat.php b/src/Models/TelegraphChat.php index f5836c79e..8afd38fc0 100644 --- a/src/Models/TelegraphChat.php +++ b/src/Models/TelegraphChat.php @@ -223,6 +223,14 @@ public function photo(string $path, string $filename = null): Telegraph return TelegraphFacade::chat($this)->photo($path, $filename); } + /** + * @param array> $media + */ + public function mediaGroup(array $media): Telegraph + { + return TelegraphFacade::chat($this)->mediaGroup($media); + } + public function animation(string $path, string $filename = null): Telegraph { return TelegraphFacade::chat($this)->animation($path, $filename); diff --git a/src/Telegraph.php b/src/Telegraph.php index 2bc2d54b4..ff1604015 100755 --- a/src/Telegraph.php +++ b/src/Telegraph.php @@ -69,6 +69,7 @@ class Telegraph public const ENDPOINT_SEND_LOCATION = 'sendLocation'; public const ENDPOINT_SEND_ANIMATION = 'sendAnimation'; public const ENDPOINT_SEND_VOICE = 'sendVoice'; + public const ENDPOINT_SEND_MEDIA_GROUP = 'sendMediaGroup'; public const ENDPOINT_SEND_CHAT_ACTION = 'sendChatAction'; public const ENDPOINT_SEND_DOCUMENT = 'sendDocument'; public const ENDPOINT_SEND_PHOTO = 'sendPhoto'; diff --git a/tests/TestCase.php b/tests/TestCase.php index 88732d975..7bb6255fd 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -33,7 +33,6 @@ public function getEnvironmentSetUp($app): void $this->filesystemSetup($app['config']); $app['config']->set('database.default', 'testing'); - } protected function defineDatabaseMigrations(): void diff --git a/tests/Unit/Concerns/HasBotsAndChatsTest.php b/tests/Unit/Concerns/HasBotsAndChatsTest.php index 3fe5c3e79..274e73c0a 100644 --- a/tests/Unit/Concerns/HasBotsAndChatsTest.php +++ b/tests/Unit/Concerns/HasBotsAndChatsTest.php @@ -107,7 +107,6 @@ }); test('photo is validated', function (string $path, bool $valid, string $exceptionClass = null, string $exceptionMessage = null, array $customConfigs = []) { - foreach ($customConfigs as $key => $value) { Config::set($key, $value); } diff --git a/tests/Unit/Concerns/SendsAttachmentsTest.php b/tests/Unit/Concerns/SendsAttachmentsTest.php index cbf7f62af..fbf153d9e 100644 --- a/tests/Unit/Concerns/SendsAttachmentsTest.php +++ b/tests/Unit/Concerns/SendsAttachmentsTest.php @@ -85,7 +85,6 @@ }); test('documents are validated', function (string $path, bool $valid, string $exceptionClass = null, string $exceptionMessage = null, array $customConfigs = []) { - foreach ($customConfigs as $key => $value) { Config::set($key, $value); } @@ -337,7 +336,6 @@ }); test('photos are validated', function (string $path, bool $valid, string $exceptionClass = null, string $exceptionMessage = null, array $customConfigs = []) { - foreach ($customConfigs as $key => $value) { Config::set($key, $value); } diff --git a/tests/Unit/Handlers/WebhookHandlerTest.php b/tests/Unit/Handlers/WebhookHandlerTest.php index 2e129f8ba..b614983e3 100644 --- a/tests/Unit/Handlers/WebhookHandlerTest.php +++ b/tests/Unit/Handlers/WebhookHandlerTest.php @@ -358,7 +358,6 @@ }); it('does not crash on errors', function () { - $chat = chat(); Facade::fake(); diff --git a/tests/Unit/Models/TelegraphChatTest.php b/tests/Unit/Models/TelegraphChatTest.php index c825ff382..a5298b68c 100644 --- a/tests/Unit/Models/TelegraphChatTest.php +++ b/tests/Unit/Models/TelegraphChatTest.php @@ -749,3 +749,32 @@ 'caption' => 'test', ]); }); + +it('can send a mediaGroup from remote url', function () { + Telegraph::fake(); + $chat = make_chat(); + + $chat->mediaGroup([ + [ + 'type' => 'photo', + 'media' => 'https://test.dev/photo.jpg', + ], + [ + 'type' => 'photo', + 'media' => 'https://test.dev/photo.jpg', + ], + ])->send(); + + Telegraph::assertSentData(\DefStudio\Telegraph\Telegraph::ENDPOINT_SEND_MEDIA_GROUP, [ + 'media' => [ + [ + 'type' => 'photo', + 'media' => 'https://test.dev/photo.jpg', + ], + [ + 'type' => 'photo', + 'media' => 'https://test.dev/photo.jpg', + ], + ], + ]); +});