Added SendAnimation method

This commit is contained in:
netkas 2024-10-09 13:29:09 -04:00
parent 02d9fe69d9
commit 4935373cda
4 changed files with 141 additions and 0 deletions

View file

@ -12,10 +12,12 @@
use TgBotLib\Methods\ForwardMessages; use TgBotLib\Methods\ForwardMessages;
use TgBotLib\Methods\GetMe; use TgBotLib\Methods\GetMe;
use TgBotLib\Methods\Logout; use TgBotLib\Methods\Logout;
use TgBotLib\Methods\SendAnimation;
use TgBotLib\Methods\SendAudio; use TgBotLib\Methods\SendAudio;
use TgBotLib\Methods\SendDocument; use TgBotLib\Methods\SendDocument;
use TgBotLib\Methods\SendMessage; use TgBotLib\Methods\SendMessage;
use TgBotLib\Methods\SendPhoto; use TgBotLib\Methods\SendPhoto;
use TgBotLib\Methods\SendVideo;
enum Methods : string enum Methods : string
{ {
@ -31,6 +33,7 @@
case SEND_AUDIO = 'sendAudio'; case SEND_AUDIO = 'sendAudio';
case SEND_DOCUMENT = 'sendDocument'; case SEND_DOCUMENT = 'sendDocument';
case SEND_VIDEO = 'sendVideo'; case SEND_VIDEO = 'sendVideo';
case SEND_ANIMATION = 'sendAnimation';
/** /**
* Executes a command on the provided bot with the given parameters. * Executes a command on the provided bot with the given parameters.
@ -56,6 +59,7 @@
self::SEND_AUDIO => SendAudio::execute($bot, $parameters), self::SEND_AUDIO => SendAudio::execute($bot, $parameters),
self::SEND_DOCUMENT => SendDocument::execute($bot, $parameters), self::SEND_DOCUMENT => SendDocument::execute($bot, $parameters),
self::SEND_VIDEO => SendVideo::execute($bot, $parameters), self::SEND_VIDEO => SendVideo::execute($bot, $parameters),
self::SEND_ANIMATION => SendAnimation::execute($bot, $parameters),
}; };
} }
} }

View file

@ -0,0 +1,99 @@
<?php
namespace TgBotLib\Methods;
use TgBotLib\Abstracts\Method;
use TgBotLib\Bot;
use TgBotLib\Enums\Methods;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Message;
use TgBotLib\Objects\MessageEntity;
use TgBotLib\Objects\ReplyParameters;
class SendAnimation extends Method
{
/**
* @inheritDoc
*/
public static function execute(Bot $bot, array $parameters = []): Message
{
// Handle object conversions
if(isset($parameters['caption_entities']) && $parameters['caption_entities'])
{
$entities = [];
foreach($parameters['caption_entities'] as $entity)
{
if($entity instanceof MessageEntity)
{
$entities[] = $entity->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'
];
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace TgBotLib\Methods;
use PHPUnit\Framework\TestCase;
use TgBotLib\Bot;
use TgBotLib\Enums\Types\ParseMode;
use TgBotLib\Objects\LinkPreviewOptions;
use TgBotLib\Objects\Message;
class SendAnimationTest extends TestCase
{
private static Bot $bot;
/**
* @return void
*/
public static function setUpBeforeClass(): void
{
self::$bot = new Bot(BOT_TOKEN);
}
/**
* Tests sending an animation file to a specified chat.
*
* @return void
*/
public function testSendAnimation(): void
{
$result = self::$bot->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());
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB