Add SetWebhook method and integrate with Methods enum

This commit is contained in:
netkas 2024-11-01 13:51:52 -04:00
parent f2f26998ad
commit f8e5b0f632
2 changed files with 66 additions and 0 deletions

View file

@ -29,10 +29,12 @@
use TgBotLib\Methods\SendVideo;
use TgBotLib\Methods\SendVideoNote;
use TgBotLib\Methods\SendVoice;
use TgBotLib\Methods\SetWebhook;
enum Methods : string
{
case GET_UPDATES = 'getUpdates';
case SET_WEBHOOK = 'setWebhook';
case GET_ME = 'getMe';
case LOGOUT = 'logOut';
case CLOSE = 'close';
@ -70,6 +72,7 @@
return match($this)
{
self::GET_UPDATES => GetUpdates::execute($bot, $parameters),
self::SET_WEBHOOK => SetWebhook::execute($bot, $parameters),
self::GET_ME => GetMe::execute($bot, $parameters),
self::LOGOUT => LogOut::execute($bot, $parameters),
self::CLOSE => Close::execute($bot, $parameters),

View file

@ -0,0 +1,63 @@
<?php
namespace TgBotLib\Methods;
use TgBotLib\Abstracts\Method;
use TgBotLib\Bot;
use TgBotLib\Enums\Methods;
use TgBotLib\Enums\Types\ParseMode;
use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Message;
use TgBotLib\Objects\MessageEntity;
use TgBotLib\Objects\ReplyParameters;
class SetWebhook extends Method
{
/**
* @inheritDoc
*/
public static function execute(Bot $bot, array $parameters=[]): bool
{
// Handle different photo input types
if (isset($parameters['certificate']))
{
$certificate = $parameters['certificate'];
// If certificate is a file path and exists locally
if (is_string($certificate) && file_exists($certificate) && is_file($certificate))
{
$curl = self::buildUpload($bot, Methods::SET_WEBHOOK->value, 'certificate', $certificate, array_diff_key($parameters, ['certificate' => null]));
return self::executeCurl($curl);
}
}
// If no local files to upload, use regular POST method
return self::executeCurl(self::buildPost($bot, Methods::SET_WEBHOOK->value, $parameters));
}
/**
* @inheritDoc
*/
public static function getRequiredParameters(): ?array
{
return [
'url',
];
}
/**
* @inheritDoc
*/
public static function getOptionalParameters(): ?array
{
return [
'certificate',
'ip_address',
'max_connections',
'allowed_updates',
'drop_pending_updates',
'secret_token'
];
}
}