diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index 0294ac4..f3f48be 100644 --- a/src/TgBotLib/Enums/Methods.php +++ b/src/TgBotLib/Enums/Methods.php @@ -12,10 +12,12 @@ use TgBotLib\Methods\ForwardMessages; use TgBotLib\Methods\GetMe; use TgBotLib\Methods\Logout; + use TgBotLib\Methods\SendAnimation; use TgBotLib\Methods\SendAudio; use TgBotLib\Methods\SendDocument; use TgBotLib\Methods\SendMessage; use TgBotLib\Methods\SendPhoto; + use TgBotLib\Methods\SendVideo; enum Methods : string { @@ -31,6 +33,7 @@ case SEND_AUDIO = 'sendAudio'; case SEND_DOCUMENT = 'sendDocument'; case SEND_VIDEO = 'sendVideo'; + case SEND_ANIMATION = 'sendAnimation'; /** * Executes a command on the provided bot with the given parameters. @@ -56,6 +59,7 @@ self::SEND_AUDIO => SendAudio::execute($bot, $parameters), self::SEND_DOCUMENT => SendDocument::execute($bot, $parameters), self::SEND_VIDEO => SendVideo::execute($bot, $parameters), + self::SEND_ANIMATION => SendAnimation::execute($bot, $parameters), }; } } diff --git a/src/TgBotLib/Methods/SendAnimation.php b/src/TgBotLib/Methods/SendAnimation.php new file mode 100644 index 0000000..bd01ca3 --- /dev/null +++ b/src/TgBotLib/Methods/SendAnimation.php @@ -0,0 +1,99 @@ +toArray(); + } + else + { + $entities[] = $entity; + } + } + $parameters['caption_entities'] = $entities; + } + + if(isset($parameters['reply_parameters']) && $parameters['reply_parameters'] instanceof ReplyParameters) + { + $parameters['reply_parameters'] = $parameters['reply_parameters']->toArray(); + } + + if(isset($parameters['reply_markup']) && $parameters['reply_markup'] instanceof ObjectTypeInterface) + { + $parameters['reply_markup'] = $parameters['reply_markup']->toArray(); + } + + // Handle different photo input types + if (isset($parameters['animation'])) + { + $animation = $parameters['animation']; + + // If photo is a file path and exists locally + if (is_string($animation) && file_exists($animation) && is_file($animation)) + { + return Message::fromArray(self::executeCurl(self::buildUpload($bot, Methods::SEND_ANIMATION->value, 'animation', $animation, array_diff_key($parameters, ['animation' => null])))); + } + } + + // If photo is a file_id or URL, use regular POST method + return Message::fromArray(self::executeCurl(self::buildPost($bot, Methods::SEND_ANIMATION->value, $parameters))); + } + + /** + * @inheritDoc + */ + public static function getRequiredParameters(): ?array + { + return [ + 'chat_id', + 'animation', + ]; + } + + /** + * @inheritDoc + */ + public static function getOptionalParameters(): ?array + { + return [ + 'business_connection_id', + 'message_thread_id', + 'duration', + 'width', + 'height', + 'thumbnail', + 'caption', + 'parse_mode', + 'caption_entities', + 'show_caption_above_media', + 'has_spoiler', + 'disable_notification', + 'protect_content', + 'message_effect_id', + 'reply_parameters', + 'reply_markup' + ]; + } + } \ No newline at end of file diff --git a/tests/TgBotLib/Methods/SendAnimationTest.php b/tests/TgBotLib/Methods/SendAnimationTest.php new file mode 100644 index 0000000..a1a3223 --- /dev/null +++ b/tests/TgBotLib/Methods/SendAnimationTest.php @@ -0,0 +1,38 @@ +sendAnimation( + chat_id: TEST_CHAT_ID, + animation: __DIR__ . DIRECTORY_SEPARATOR . 'sample' . DIRECTORY_SEPARATOR . 'sticker.gif', + ); + + $this->assertInstanceOf(Message::class, $result); + $this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId()); + } +} diff --git a/tests/TgBotLib/Methods/sample/sticker.gif b/tests/TgBotLib/Methods/sample/sticker.gif new file mode 100644 index 0000000..5301e9b Binary files /dev/null and b/tests/TgBotLib/Methods/sample/sticker.gif differ