From d1609e36bd84f7cfdb7a3a2f0d4837e40e698a64 Mon Sep 17 00:00:00 2001 From: Netkas Date: Tue, 14 Feb 2023 21:00:59 -0500 Subject: [PATCH] Added \TgBotLib > Bot > setWebhook() --- src/TgBotLib/Bot.php | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 0341c80..81d8b82 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -4,8 +4,7 @@ namespace TgBotLib; - use CurlHandle; - use TgBotLib\Exceptions\RequestException; + use TgBotLib\Exceptions\TelegramException; use TgBotLib\Objects\Telegram\Update; class Bot @@ -110,7 +109,7 @@ * @param string $method * @param array $params * @return array - * @throws RequestException + * @throws TelegramException */ public function sendRequest(string $method, array $params = []): array { @@ -129,12 +128,12 @@ ]); $response = curl_exec($ch); if ($response === false) - throw new RequestException('Curl error: ' . curl_error($ch), curl_errno($ch)); + throw new TelegramException('Curl error: ' . curl_error($ch), curl_errno($ch)); curl_close($ch); $parsed = json_decode($response, true); if($parsed['ok'] === false) - throw new RequestException($parsed['description'], $parsed['error_code']); + throw new TelegramException($parsed['description'], $parsed['error_code']); return $parsed['result']; } @@ -145,7 +144,7 @@ * * @param array $params * @return Update[] - * @throws RequestException + * @throws TelegramException */ public function getUpdates(array $params=[]): array { @@ -163,4 +162,24 @@ return $results; } + /** + * Use this method to specify a URL and receive incoming updates via an outgoing webhook. Whenever there is an + * update for the bot, we will send an HTTPS POST request to the specified URL, containing a JSON-serialized + * Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. + * Returns True on success. + * + * If you'd like to make sure that the webhook was set by you, you can specify secret data in the parameter + * secret_token. If specified, the request will contain a header “X-Telegram-Bot-Api-Secret-Token” with the + * secret token as content. + * + * @param array $params + * @return bool + * @throws TelegramException + */ + public function setWebhook(array $params=[]): bool + { + $this->sendRequest('setWebhook', $params); + return true; + } + } \ No newline at end of file