Add EditMessageMedia method

This commit is contained in:
netkas 2024-11-14 00:19:26 -05:00
parent 0486a7f0ab
commit 6e6e207c54
2 changed files with 74 additions and 0 deletions

View file

@ -28,6 +28,7 @@
use TgBotLib\Methods\EditForumTopic;
use TgBotLib\Methods\EditGeneralForumTopic;
use TgBotLib\Methods\EditMessageCaption;
use TgBotLib\Methods\EditMessageMedia;
use TgBotLib\Methods\EditMessageText;
use TgBotLib\Methods\ExportChatInviteLink;
use TgBotLib\Methods\ForwardMessage;
@ -190,6 +191,7 @@
case GET_MY_DEFAULT_ADMINISTRATOR_RIGHTS = 'getMyDefaultAdministratorRights';
case EDIT_MESSAGE_TEXT = 'editMessageText';
case EDIT_MESSAGE_CAPTION = 'editMessageCaption';
case EDIT_MESSAGE_MEDIA = 'editMessageMedia';
/**
* Executes a command on the provided bot with the given parameters.
@ -294,6 +296,7 @@
self::GET_MY_DEFAULT_ADMINISTRATOR_RIGHTS => GetMyDefaultAdministratorRights::execute($bot, $parameters),
self::EDIT_MESSAGE_TEXT => EditMessageText::execute($bot, $parameters),
self::EDIT_MESSAGE_CAPTION => EditMessageCaption::execute($bot, $parameters),
self::EDIT_MESSAGE_MEDIA => EditMessageMedia::execute($bot, $parameters),
};
}
}

View file

@ -0,0 +1,71 @@
<?php
namespace TgBotLib\Methods;
use TgBotLib\Abstracts\Method;
use TgBotLib\Bot;
use TgBotLib\Enums\Methods;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Message;
class EditMessageMedia extends Method
{
/**
* @inheritDoc
*/
public static function execute(Bot $bot, array $parameters = []): mixed
{
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 different media input types
if (isset($parameters['media']))
{
$media = $parameters['media'];
// If media is a file path and exists locally
if (is_string($media) && file_exists($media) && is_file($media))
{
$curl = self::buildUpload($bot, Methods::EDIT_MESSAGE_MEDIA->value, 'media', $media, array_diff_key($parameters, ['media' => null]));
return Message::fromArray(self::executeCurl($curl));
}
}
return Message::fromArray(self::executeCurl(self::buildPost($bot, Methods::EDIT_MESSAGE_MEDIA->value, $parameters)));
}
/**
* @inheritDoc
*/
public static function getRequiredParameters(): ?array
{
return [
'media'
];
}
/**
* @inheritDoc
*/
public static function getOptionalParameters(): ?array
{
return [
'business_connection_id',
'chat_id',
'message_id',
'inline_message_id',
'reply_markup'
];
}
}