From 5703ce998700b96fa6d486ac1ba7b87e1b32bd2c Mon Sep 17 00:00:00 2001 From: netkas Date: Sun, 6 Oct 2024 18:50:26 -0400 Subject: [PATCH] Minor Correction --- src/TgBotLib/Abstracts/Method.php | 208 +++++++++++++-------------- src/TgBotLib/Methods/Close.php | 26 ++-- src/TgBotLib/Methods/GetMe.php | 28 ++-- src/TgBotLib/Methods/Logout.php | 20 +-- src/TgBotLib/Methods/SendMessage.php | 44 +++--- 5 files changed, 163 insertions(+), 163 deletions(-) diff --git a/src/TgBotLib/Abstracts/Method.php b/src/TgBotLib/Abstracts/Method.php index 16be0da..58054e8 100644 --- a/src/TgBotLib/Abstracts/Method.php +++ b/src/TgBotLib/Abstracts/Method.php @@ -1,124 +1,124 @@ getEndpoint(), $bot->getToken(), $method)); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + /** + * Executes a bot command with the given parameters. + * + * @param Bot $bot The bot instance on which the command is to be executed. + * @param array $parameters The parameters required for the bot command. + * @return ObjectTypeInterface|ObjectTypeInterface[]|mixed The result of the bot command. + * @throws TelegramException if the response from the bot command is not valid. + * @throws InvalidArgumentException if the required parameters are not provided. + */ + public abstract static function execute(Bot $bot, array $parameters=[]): mixed; - if($parameters === null) + /** + * Builds a cURL handle for making a POST request to a bot's endpoint. + * + * @param Bot $bot The bot object containing the endpoint information. + * @param string $method + * @param array|null $parameters An array of parameters to be sent in the POST request. + * @return CurlHandle The configured cURL handle ready for execution. + */ + protected static function buildPost(Bot $bot, string $method, ?array $parameters=null): CurlHandle { - curl_setopt($curl, CURLOPT_POST, false); + $curl = curl_init(sprintf('%s/bot%s/%s', $bot->getEndpoint(), $bot->getToken(), $method)); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + + if($parameters === null) + { + curl_setopt($curl, CURLOPT_POST, false); + } + else + { + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); + } + + return $curl; } - else + + /** + * Builds a cURL handle for uploading a file to the Telegram API. + * + * @param Bot $bot The bot instance used to get the endpoint. + * @param string $file_param The parameter name for the file to be uploaded. + * @param string $file_path The file path of the file to be uploaded. + * @param array $parameters Additional parameters to be included in the request. + * @return CurlHandle The cURL handle configured for the file upload. + */ + protected static function buildUpload(Bot $bot, string $method, string $file_param, string $file_path, array $parameters): CurlHandle { + $curl = curl_init(sprintf('%s/%s?%s', $bot->getEndpoint(), $method, http_build_query($parameters))); + curl_setopt($curl, CURLOPT_POST, true); - curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); - curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']); + curl_setopt($curl, CURLOPT_POSTFIELDS, [ + $file_param => new CURLFile($file_path) + ]); + + return $curl; } - return $curl; - } - - /** - * Builds a cURL handle for uploading a file to the Telegram API. - * - * @param Bot $bot The bot instance used to get the endpoint. - * @param string $file_param The parameter name for the file to be uploaded. - * @param string $file_path The file path of the file to be uploaded. - * @param array $parameters Additional parameters to be included in the request. - * @return CurlHandle The cURL handle configured for the file upload. - */ - protected static function buildUpload(Bot $bot, string $method, string $file_param, string $file_path, array $parameters): CurlHandle - { - $curl = curl_init(sprintf('%s/%s?%s', $bot->getEndpoint(), $method, http_build_query($parameters))); - - curl_setopt($curl, CURLOPT_POST, true); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']); - curl_setopt($curl, CURLOPT_POSTFIELDS, [ - $file_param => new CURLFile($file_path) - ]); - - return $curl; - } - - protected static function buildMultiUpload(Bot $bot, string $method, array $files, array $parameters): CurlHandle - { - $curl = curl_init(sprintf('%s/%s?%s', $bot->getEndpoint(), $method, http_build_query($parameters))); - - curl_setopt($curl, CURLOPT_POST, true); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']); - - $post_fields = []; - foreach($files as $file_param => $file_path) + protected static function buildMultiUpload(Bot $bot, string $method, array $files, array $parameters): CurlHandle { - $post_fields[$file_param] = new CURLFile($file_path); + $curl = curl_init(sprintf('%s/%s?%s', $bot->getEndpoint(), $method, http_build_query($parameters))); + + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']); + + $post_fields = []; + foreach($files as $file_param => $file_path) + { + $post_fields[$file_param] = new CURLFile($file_path); + } + + curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields); + return $curl; } - curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields); - return $curl; - } - - /** - * Executes a cURL request and processes the response. - * - * @param CurlHandle $curl The cURL handle to be executed. - * @return array The decoded response from the cURL request. - * @throws TelegramException if the response is not a valid array, - * or if the 'ok' field is not set or is false. - */ - protected static function executeCurl(CurlHandle $curl): mixed - { - $response = curl_exec($curl); - curl_close($curl); - $result = json_decode($response, true); - - if(!is_array($result)) + /** + * Executes a cURL request and processes the response. + * + * @param CurlHandle $curl The cURL handle to be executed. + * @return array The decoded response from the cURL request. + * @throws TelegramException if the response is not a valid array, + * or if the 'ok' field is not set or is false. + */ + protected static function executeCurl(CurlHandle $curl): mixed { - throw new TelegramException('Invalid response from Telegram API'); - } + $response = curl_exec($curl); + curl_close($curl); + $result = json_decode($response, true); - if(!isset($result['ok'])) - { - throw new TelegramException('Invalid response from Telegram API'); - } + if(!is_array($result)) + { + throw new TelegramException('Invalid response from Telegram API'); + } - if($result['ok'] === false) - { - throw new TelegramException($result['description'], (int)$result['error_code']); - } + if(!isset($result['ok'])) + { + throw new TelegramException('Invalid response from Telegram API'); + } - return $result['result']; - } + if($result['ok'] === false) + { + throw new TelegramException($result['description'], (int)$result['error_code']); + } + + return $result['result']; + } } \ No newline at end of file diff --git a/src/TgBotLib/Methods/Close.php b/src/TgBotLib/Methods/Close.php index ecac1c4..085da19 100644 --- a/src/TgBotLib/Methods/Close.php +++ b/src/TgBotLib/Methods/Close.php @@ -1,18 +1,18 @@ value)); - } -} \ No newline at end of file + /** + * @inheritDoc + */ + public static function execute(Bot $bot, array $parameters = []): bool + { + return (bool) self::executeCurl(self::buildPost($bot, Methods::CLOSE->value)); + } + } \ No newline at end of file diff --git a/src/TgBotLib/Methods/GetMe.php b/src/TgBotLib/Methods/GetMe.php index 2ec4158..a499c32 100644 --- a/src/TgBotLib/Methods/GetMe.php +++ b/src/TgBotLib/Methods/GetMe.php @@ -1,19 +1,19 @@ value))); - } -} \ No newline at end of file + /** + * @inheritDoc + */ + public static function execute(Bot $bot, array $parameters=[]): User + { + return User::fromArray(self::executeCurl(self::buildPost($bot, Methods::GET_ME->value))); + } + } \ No newline at end of file diff --git a/src/TgBotLib/Methods/Logout.php b/src/TgBotLib/Methods/Logout.php index e0f873b..9f3b42b 100644 --- a/src/TgBotLib/Methods/Logout.php +++ b/src/TgBotLib/Methods/Logout.php @@ -1,15 +1,15 @@ value)); - } -} \ No newline at end of file + public static function execute(Bot $bot, array $parameters=[]): bool + { + return (bool) self::executeCurl(self::buildPost($bot, Methods::LOGOUT->value)); + } + } \ No newline at end of file diff --git a/src/TgBotLib/Methods/SendMessage.php b/src/TgBotLib/Methods/SendMessage.php index f9f0575..4e79a18 100644 --- a/src/TgBotLib/Methods/SendMessage.php +++ b/src/TgBotLib/Methods/SendMessage.php @@ -1,30 +1,30 @@ value, $parameters))); - } -} \ No newline at end of file + return Message::fromArray(self::executeCurl(self::buildPost($bot, Methods::SEND_MESSAGE->value, $parameters))); + } + } \ No newline at end of file