From eab15b2afce61aa9b920f1fd1b81c9a893be1d21 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 23 Jun 2024 19:04:11 +0300 Subject: [PATCH] Add `sendAnimation` method --- CHANGELOG.md | 1 + src/Method/SendAnimation.php | 106 ++++++++++++++++++++++++++++ src/TelegramBotApi.php | 52 ++++++++++++++ tests/Method/SendAnimationTest.php | 107 +++++++++++++++++++++++++++++ tests/TelegramBotApiTest.php | 20 ++++++ 5 files changed, 286 insertions(+) create mode 100644 src/Method/SendAnimation.php create mode 100644 tests/Method/SendAnimationTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 382eeb5..9d9bc8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - New #23: Add `logOut` and `close` methods. - New #25: Add `forwardMessage` and `forwardMessages` methods, and `MessageId` type. - New #26: Add `copyMessage` and `copyMessages` methods. +- New #27: Add `sendAnimation` method. - Chg #24: Move update methods to `Vjik\TelegramBot\Api\Method\Update` namespace, and update types to `Vjik\TelegramBot\Api\Type\Update` namespace. diff --git a/src/Method/SendAnimation.php b/src/Method/SendAnimation.php new file mode 100644 index 0000000..6148fab --- /dev/null +++ b/src/Method/SendAnimation.php @@ -0,0 +1,106 @@ + $this->businessConnectionId, + 'chat_id' => $this->chatId, + 'message_thread_id' => $this->messageThreadId, + 'animation' => is_string($this->animation) ? $this->animation : null, + 'duration' => $this->duration, + 'width' => $this->width, + 'height' => $this->height, + 'thumbnail' => is_string($this->thumbnail) ? $this->thumbnail : null, + 'caption' => $this->caption, + 'parse_mode' => $this->parseMode, + 'caption_entities' => $this->captionEntities === null ? null : array_map( + static fn(MessageEntity $entity) => $entity->toRequestArray(), + $this->captionEntities, + ), + 'show_caption_above_media' => $this->showCaptionAboveMedia, + 'has_spoiler' => $this->hasSpoiler, + 'disable_notification' => $this->disableNotification, + 'protect_content' => $this->protectContent, + 'message_effect_id' => $this->messageEffectId, + 'reply_parameters' => $this->replyParameters?->toRequestArray(), + 'reply_markup' => $this->replyMarkup?->toRequestArray(), + ], + static fn(mixed $value): bool => $value !== null, + ); + } + + public function getFiles(): array + { + return array_filter( + [ + 'animation' => $this->animation instanceof InputFile ? $this->animation : null, + 'thumbnail' => $this->thumbnail instanceof InputFile ? $this->thumbnail : null, + ], + static fn(mixed $value): bool => $value !== null, + ); + } + + public function prepareResult(mixed $result): Message + { + return Message::fromTelegramResult($result); + } +} diff --git a/src/TelegramBotApi.php b/src/TelegramBotApi.php index 60b8234..dcfec28 100644 --- a/src/TelegramBotApi.php +++ b/src/TelegramBotApi.php @@ -24,6 +24,7 @@ use Vjik\TelegramBot\Api\Method\GetUserProfilePhotos; use Vjik\TelegramBot\Api\Method\LogOut; use Vjik\TelegramBot\Api\Method\Payments\GetStarTransactions; +use Vjik\TelegramBot\Api\Method\SendAnimation; use Vjik\TelegramBot\Api\Method\SendAudio; use Vjik\TelegramBot\Api\Method\SendChatAction; use Vjik\TelegramBot\Api\Method\SendContact; @@ -410,6 +411,57 @@ public function logOut(): FailResult|true return $this->send(new LogOut()); } + /** + * @param MessageEntity[]|null $captionEntities + * + * @see https://core.telegram.org/bots/api#sendanimation + * + * @psalm-suppress MixedInferredReturnType,MixedReturnStatement + */ + public function sendAnimation( + int|string $chatId, + InputFile|string $animation, + ?string $businessConnectionId = null, + ?int $messageThreadId = null, + ?int $duration = null, + ?int $width = null, + ?int $height = null, + InputFile|string|null $thumbnail = null, + ?string $caption = null, + ?string $parseMode = null, + ?array $captionEntities = null, + ?bool $showCaptionAboveMedia = null, + ?bool $hasSpoiler = null, + ?bool $disableNotification = null, + ?bool $protectContent = null, + ?string $messageEffectId = null, + ?ReplyParameters $replyParameters = null, + InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply|null $replyMarkup = null, + ): FailResult|Message { + return $this->send( + new SendAnimation( + $chatId, + $animation, + $businessConnectionId, + $messageThreadId, + $duration, + $width, + $height, + $thumbnail, + $caption, + $parseMode, + $captionEntities, + $showCaptionAboveMedia, + $hasSpoiler, + $disableNotification, + $protectContent, + $messageEffectId, + $replyParameters, + $replyMarkup, + ) + ); + } + /** * @param MessageEntity[]|null $captionEntities * diff --git a/tests/Method/SendAnimationTest.php b/tests/Method/SendAnimationTest.php new file mode 100644 index 0000000..556cbf4 --- /dev/null +++ b/tests/Method/SendAnimationTest.php @@ -0,0 +1,107 @@ +assertSame(HttpMethod::POST, $method->getHttpMethod()); + $this->assertSame('sendAnimation', $method->getApiMethod()); + $this->assertSame( + [ + 'chat_id' => 12, + 'animation' => 'https://example.com/anime.gif', + ], + $method->getData(), + ); + $this->assertSame([], $method->getFiles()); + } + + public function testFull(): void + { + $animation = new InputFile((new StreamFactory())->createStream('test'), 'test.gif'); + $thumbnail = new InputFile((new StreamFactory())->createStream('test'), 'test.jpg'); + $entity = new MessageEntity('bold', 0, 5); + $replyParameters = new ReplyParameters(23); + $replyMarkup = new ForceReply(); + $method = new SendAnimation( + 12, + $animation, + 'bcid1', + 99, + 100, + 240, + 320, + $thumbnail, + 'Caption', + 'HTML', + [$entity], + true, + false, + false, + true, + 'meID', + $replyParameters, + $replyMarkup, + ); + + $this->assertSame( + [ + 'business_connection_id' => 'bcid1', + 'chat_id' => 12, + 'message_thread_id' => 99, + 'duration' => 100, + 'width' => 240, + 'height' => 320, + 'caption' => 'Caption', + 'parse_mode' => 'HTML', + 'caption_entities' => [$entity->toRequestArray()], + 'show_caption_above_media' => true, + 'has_spoiler' => false, + 'disable_notification' => false, + 'protect_content' => true, + 'message_effect_id' => 'meID', + 'reply_parameters' => $replyParameters->toRequestArray(), + 'reply_markup' => $replyMarkup->toRequestArray(), + ], + $method->getData(), + ); + $this->assertSame( + [ + 'animation' => $animation, + 'thumbnail' => $thumbnail, + ], + $method->getFiles(), + ); + } + + public function testPrepareResult(): void + { + $method = new SendAnimation(12, 'https://example.com/anime.gif'); + + $preparedResult = $method->prepareResult([ + 'message_id' => 7, + 'date' => 1620000000, + 'chat' => [ + 'id' => 1, + 'type' => 'private', + ], + ]); + + $this->assertSame(7, $preparedResult->messageId); + } +} diff --git a/tests/TelegramBotApiTest.php b/tests/TelegramBotApiTest.php index 2681d97..d5d1b40 100644 --- a/tests/TelegramBotApiTest.php +++ b/tests/TelegramBotApiTest.php @@ -437,6 +437,26 @@ public function testLogOut(): void $this->assertTrue($result); } + public function testSendAnimation(): void + { + $api = $this->createApi([ + 'ok' => true, + 'result' => [ + 'message_id' => 7, + 'date' => 1620000000, + 'chat' => [ + 'id' => 1, + 'type' => 'private', + ], + ], + ]); + + $result = $api->sendAnimation(12, 'https://example.com/anime.gif'); + + $this->assertInstanceOf(Message::class, $result); + $this->assertSame(7, $result->messageId); + } + public function testSendAudio(): void { $api = $this->createApi([