Added \TgBotLib > Bot > setWebhook()

This commit is contained in:
Netkas 2023-02-14 21:00:59 -05:00
parent b2dbc01193
commit d1609e36bd

View file

@ -4,8 +4,7 @@
namespace TgBotLib; namespace TgBotLib;
use CurlHandle; use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Exceptions\RequestException;
use TgBotLib\Objects\Telegram\Update; use TgBotLib\Objects\Telegram\Update;
class Bot class Bot
@ -110,7 +109,7 @@
* @param string $method * @param string $method
* @param array $params * @param array $params
* @return array * @return array
* @throws RequestException * @throws TelegramException
*/ */
public function sendRequest(string $method, array $params = []): array public function sendRequest(string $method, array $params = []): array
{ {
@ -129,12 +128,12 @@
]); ]);
$response = curl_exec($ch); $response = curl_exec($ch);
if ($response === false) 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); curl_close($ch);
$parsed = json_decode($response, true); $parsed = json_decode($response, true);
if($parsed['ok'] === false) if($parsed['ok'] === false)
throw new RequestException($parsed['description'], $parsed['error_code']); throw new TelegramException($parsed['description'], $parsed['error_code']);
return $parsed['result']; return $parsed['result'];
} }
@ -145,7 +144,7 @@
* *
* @param array $params * @param array $params
* @return Update[] * @return Update[]
* @throws RequestException * @throws TelegramException
*/ */
public function getUpdates(array $params=[]): array public function getUpdates(array $params=[]): array
{ {
@ -163,4 +162,24 @@
return $results; 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;
}
} }