Added SendVideoNote method

This commit is contained in:
netkas 2024-10-09 15:10:32 -04:00
parent 5d1b07c4c4
commit 27ffc3408d
2 changed files with 108 additions and 0 deletions

View file

@ -18,6 +18,7 @@
use TgBotLib\Methods\SendMessage;
use TgBotLib\Methods\SendPhoto;
use TgBotLib\Methods\SendVideo;
use TgBotLib\Methods\SendVideoNote;
use TgBotLib\Methods\SendVoice;
enum Methods : string
@ -36,6 +37,7 @@
case SEND_VIDEO = 'sendVideo';
case SEND_ANIMATION = 'sendAnimation';
case SEND_VOICE = 'sendVoice';
case SEND_VIDEO_NOTE = 'sendVideoNote';
/**
* Executes a command on the provided bot with the given parameters.
@ -63,6 +65,7 @@
self::SEND_VIDEO => SendVideo::execute($bot, $parameters),
self::SEND_ANIMATION => SendAnimation::execute($bot, $parameters),
self::SEND_VOICE => SendVoice::execute($bot, $parameters),
self::SEND_VIDEO_NOTE => SendVideoNote::execute($bot, $parameters)
};
}
}

View file

@ -0,0 +1,105 @@
<?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\ReplyParameters;
class SendVideoNote extends Method
{
/**
* @inheritDoc
*/
public static function execute(Bot $bot, array $parameters = []): Message
{
if (isset($parameters['reply_parameters']) && $parameters['reply_parameters'] instanceof ReplyParameters)
{
$parameters['reply_parameters'] = $parameters['reply_parameters']->toArray();
}
if (isset($parameters['reply_markup']))
{
if ($parameters['reply_markup'] instanceof ObjectTypeInterface)
{
$parameters['reply_markup'] = json_encode($parameters['reply_markup']->toArray());
}
elseif (is_array($parameters['reply_markup']))
{
$parameters['reply_markup'] = json_encode($parameters['reply_markup']);
}
}
// Handle file uploads
$hasLocalVideo = isset($parameters['video_note']) && is_string($parameters['video_note']) && file_exists($parameters['video_note']) && is_file($parameters['video_note']);
$hasLocalThumb = isset($parameters['thumbnail']) && is_string($parameters['thumbnail']) && file_exists($parameters['thumbnail']) && is_file($parameters['thumbnail']);
if ($hasLocalVideo || $hasLocalThumb)
{
$files = [];
if ($hasLocalVideo)
{
$files['video_note'] = $parameters['video_note'];
unset($parameters['video_note']);
}
if ($hasLocalThumb)
{
$files['thumbnail'] = $parameters['thumbnail'];
unset($parameters['thumbnail']);
}
if (count($files) > 1)
{
// Multiple files to upload
$curl = self::buildMultiUpload($bot, Methods::SEND_VIDEO_NOTE->value, $files, $parameters);
}
else
{
// Single file to upload
$fileParam = array_key_first($files);
$curl = self::buildUpload($bot, Methods::SEND_VIDEO_NOTE->value, $fileParam, $files[$fileParam], $parameters);
}
return Message::fromArray(self::executeCurl($curl));
}
// If no local files to upload, use regular POST method
return Message::fromArray(self::executeCurl(self::buildPost($bot, Methods::SEND_VIDEO->value, $parameters)));
}
/**
* @inheritDoc
*/
public static function getRequiredParameters(): ?array
{
return [
'chat_id',
'video_note'
];
}
/**
* @inheritDoc
*/
public static function getOptionalParameters(): ?array
{
return [
'business_connection_id',
'message_thread_id',
'duration',
'length',
'thumbnail',
'disable_notification',
'protect_content',
'message_effect_id',
'reply_parameters',
'reply_markup'
];
}
}