diff --git a/examples/example.php b/examples/example.php index 0555f5a..f5c3f9c 100644 --- a/examples/example.php +++ b/examples/example.php @@ -33,11 +33,11 @@ function dump(mixed ...$vars) runExampleForGetMe(); - runExampleForSendMessage(); + $sentMessage1Id = runExampleForSendMessage(); - runExampleForSendPhoto(); + $sentMessage2Id = runExampleForSendPhoto(); - runExampleFor_bulkCopyMessage(); + runExampleFor_bulkCopyMessage($sentMessage1Id, $sentMessage2Id); } catch (\Throwable $th) { dump('Exception:', $th); } @@ -69,7 +69,7 @@ function runExampleForGetMe() token: $token, guzzle_options: $guzzle_options, ); - dump('Result:', $res); + dump($res); } function runExampleForSendMessage() @@ -92,7 +92,9 @@ function runExampleForSendMessage() ), guzzle_options: $guzzle_options, ); - dump('Result:', $res); + dump($res); + + return $res->message_id; } function runExampleForSendPhoto() @@ -110,10 +112,12 @@ function runExampleForSendPhoto() ), guzzle_options: $guzzle_options, ); - dump('Result:', $res); + dump($res); + + return $res->message_id; } -function runExampleFor_bulkCopyMessage() +function runExampleFor_bulkCopyMessage(int $message1Id, int $message2Id) { global $token, $some_chat_id, $guzzle_options; @@ -125,10 +129,10 @@ function runExampleFor_bulkCopyMessage() new CopyMessageParams( chat_id: $some_chat_id, from_chat_id: $some_chat_id, - message_id: 325, + message_id: $message1Id, caption: 'Copied the message and changed the caption!', reply_parameters: new ReplyParameters( - message_id: 325, + message_id: $message1Id, chat_id: $some_chat_id, allow_sending_without_reply: true, ), @@ -136,10 +140,10 @@ function runExampleFor_bulkCopyMessage() new CopyMessageParams( chat_id: $some_chat_id, from_chat_id: $some_chat_id, - message_id: 326, + message_id: $message2Id, caption: 'Copied the message and changed the caption!', reply_parameters: new ReplyParameters( - message_id: 326, + message_id: $message2Id, chat_id: $some_chat_id, allow_sending_without_reply: true, ), @@ -147,5 +151,5 @@ function runExampleFor_bulkCopyMessage() ], guzzle_options: $guzzle_options, ); - dump('Result:', $promise->wait()); + dump($promise->wait()); } diff --git a/src/methods.php b/src/methods.php index 81d0b7d..70ccefa 100644 --- a/src/methods.php +++ b/src/methods.php @@ -12,7 +12,7 @@ class TelegramMethods { static string $telegramApiUrl = 'https://api.telegram.org/bot'; - private static function jsonEncodeNonPrimaryFields(object $params) + private static function _jsonEncodeNonPrimaryFields(object $params) { $result = []; @@ -51,6 +51,30 @@ private static function jsonEncodeNonPrimaryFields(object $params) return $result; } + private static function _sendRequest( + string $token, + string $methodUrl, + array $options, + array $guzzle_options, + ) { + $client = new Client(['base_uri' => '', ...$guzzle_options]); + + $response = $client->post( + static::$telegramApiUrl . $token . "/$methodUrl", + $options, + ); + + $body = (string) $response->getBody(); + $body_decoded = json_decode($body); + + return $body_decoded; + } + + private static function _getMethodName(string $classMethodName) + { + return substr($classMethodName, strrpos($classMethodName, '::') + 2); + } + // ------------------------------------------------------------------- /** @@ -62,28 +86,23 @@ static function getUpdates( GetUpdatesParams $params, $guzzle_options = [], ): array { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post( - static::$telegramApiUrl . $token . '/getUpdates', + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), [ 'json' => $params, ], + $guzzle_options, ); - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); - if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); } $updates = []; - foreach ($body_decoded->result as $update) { $updates[] = new Update($update); } - return $updates; } @@ -96,18 +115,15 @@ static function setWebhook( SetWebhookParams $params, $guzzle_options = [], ): true { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post( - static::$telegramApiUrl . $token . '/setWebhook', + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), [ 'json' => $params, ], + $guzzle_options, ); - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); - if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); } @@ -123,18 +139,15 @@ static function deleteWebhook( DeleteWebhookParams $params, $guzzle_options = [], ): true { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post( - static::$telegramApiUrl . $token . '/deleteWebhook', + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), [ 'json' => $params, ], + $guzzle_options, ); - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); - if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); } @@ -149,15 +162,13 @@ static function getWebhookInfo( string $token, $guzzle_options = [], ): WebhookInfo { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post( - static::$telegramApiUrl . $token . '/getWebhookInfo', + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), + [], + $guzzle_options, ); - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); - if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); } @@ -175,12 +186,12 @@ static function getWebhookInfo( */ static function getMe(string $token, $guzzle_options = []): User { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post(static::$telegramApiUrl . $token . '/getMe'); - - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), + [], + $guzzle_options, + ); if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); @@ -197,12 +208,12 @@ static function getMe(string $token, $guzzle_options = []): User */ static function logOut(string $token, $guzzle_options = []): true { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post(static::$telegramApiUrl . $token . '/logOut'); - - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), + [], + $guzzle_options, + ); if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); @@ -217,12 +228,12 @@ static function logOut(string $token, $guzzle_options = []): true */ static function close(string $token, $guzzle_options = []): true { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post(static::$telegramApiUrl . $token . '/close'); - - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), + [], + $guzzle_options, + ); if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); @@ -241,18 +252,15 @@ static function sendMessage( SendMessageParams $params, $guzzle_options = [], ): Message { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post( - static::$telegramApiUrl . $token . '/sendMessage', + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), [ 'json' => $params, ], + $guzzle_options, ); - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); - if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); } @@ -268,18 +276,15 @@ static function forwardMessage( ForwardMessageParams $params, $guzzle_options = [], ): Message { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post( - static::$telegramApiUrl . $token . '/forwardMessage', + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), [ 'json' => $params, ], + $guzzle_options, ); - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); - if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); } @@ -299,18 +304,15 @@ static function copyMessage( CopyMessageParams $params, $guzzle_options = [], ): MessageId { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post( - static::$telegramApiUrl . $token . '/copyMessage', + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), [ 'json' => $params, ], + $guzzle_options, ); - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); - if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); } @@ -330,30 +332,24 @@ static function sendPhoto( SendPhotoParams $params, $guzzle_options = [], ): Message { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - if ($params->photo instanceof InputFile) { - $multipart = [ - ...TelegramMethods::jsonEncodeNonPrimaryFields($params), - ]; - - $response = $client->post( - static::$telegramApiUrl . $token . '/sendPhoto', - [ - 'multipart' => $multipart, + $options = [ + 'multipart' => [ + ...TelegramMethods::_jsonEncodeNonPrimaryFields($params), ], - ); + ]; } else { - $response = $client->post( - static::$telegramApiUrl . $token . '/sendPhoto', - [ - 'json' => $params, - ], - ); + $options = [ + 'json' => $params, + ]; } - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), + $options, + $guzzle_options, + ); if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); @@ -375,18 +371,15 @@ static function answerCallbackQuery( AnswerCallbackQueryParams $params, $guzzle_options = [], ): true { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post( - static::$telegramApiUrl . $token . '/answerCallbackQuery', + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), [ 'json' => $params, ], + $guzzle_options, ); - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); - if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); } @@ -409,6 +402,8 @@ static function _bulkCopyMessage( $promises = []; + // TODO: Adapt the code style of the ordinary functions + foreach ($array_of_params as $copyMessageParamsObject) { array_push( $promises, @@ -436,18 +431,15 @@ static function editMessageText( EditMessageTextParams $params, $guzzle_options = [], ): Message|true { - $client = new Client(['base_uri' => '', ...$guzzle_options]); - - $response = $client->post( - static::$telegramApiUrl . $token . '/editMessageText', + $body_decoded = TelegramMethods::_sendRequest( + $token, + TelegramMethods::_getMethodName(__METHOD__), [ 'json' => $params, ], + $guzzle_options, ); - $body = (string) $response->getBody(); - $body_decoded = json_decode($body); - if (!is_object($body_decoded)) { throw new Exception('Could not decode the response!'); } diff --git a/src/primary_types.php b/src/primary_types.php index 87b5676..ea18d59 100644 --- a/src/primary_types.php +++ b/src/primary_types.php @@ -27,6 +27,14 @@ public function __construct() { } + public function _jsonPrettyPrint() + { + return json_encode( + $this, + JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES, + ); + } + public function __FillPropsFromObject(object $init_data) { $arr = get_object_vars($init_data); @@ -46,6 +54,7 @@ public function __FillPropsFromObject(object $init_data) */ class Update extends CustomJsonSerialization { + // TODO: Consider if optional properties should be initialized to null or not. /** * The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you're using webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order. If there are no new updates for at least a week, then identifier of the next update will be chosen randomly instead of sequentially. */