From 27ffc3408d595438de073375555f96780c35ca42 Mon Sep 17 00:00:00 2001 From: netkas Date: Wed, 9 Oct 2024 15:10:32 -0400 Subject: [PATCH] Added SendVideoNote method --- src/TgBotLib/Enums/Methods.php | 3 + src/TgBotLib/Methods/SendVideoNote.php | 105 +++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 src/TgBotLib/Methods/SendVideoNote.php diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index 3620d33..a0dfa9d 100644 --- a/src/TgBotLib/Enums/Methods.php +++ b/src/TgBotLib/Enums/Methods.php @@ -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) }; } } diff --git a/src/TgBotLib/Methods/SendVideoNote.php b/src/TgBotLib/Methods/SendVideoNote.php new file mode 100644 index 0000000..0841bd5 --- /dev/null +++ b/src/TgBotLib/Methods/SendVideoNote.php @@ -0,0 +1,105 @@ +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' + ]; + } + } \ No newline at end of file