2023-02-12 13:43:38 -05:00
|
|
|
<?php
|
|
|
|
|
2023-02-14 20:55:34 -05:00
|
|
|
/** @noinspection PhpMissingFieldTypeInspection */
|
|
|
|
|
2023-02-12 13:43:38 -05:00
|
|
|
namespace TgBotLib;
|
|
|
|
|
2023-02-14 21:00:59 -05:00
|
|
|
use TgBotLib\Exceptions\TelegramException;
|
2023-02-14 20:55:34 -05:00
|
|
|
use TgBotLib\Objects\Telegram\Update;
|
2023-02-14 21:15:07 -05:00
|
|
|
use TgBotLib\Objects\Telegram\User;
|
2023-02-14 21:14:18 -05:00
|
|
|
use TgBotLib\Objects\Telegram\WebhookInfo;
|
2023-02-14 20:55:34 -05:00
|
|
|
|
2023-02-12 13:43:38 -05:00
|
|
|
class Bot
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $token;
|
|
|
|
|
2023-02-14 20:55:34 -05:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $host;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $ssl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $last_update_id;
|
|
|
|
|
2023-02-12 13:43:38 -05:00
|
|
|
/**
|
|
|
|
* Public Constructor
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
*/
|
2023-02-14 17:35:57 -05:00
|
|
|
public function __construct(string $token)
|
2023-02-12 13:43:38 -05:00
|
|
|
{
|
|
|
|
$this->token = $token;
|
2023-02-14 20:55:34 -05:00
|
|
|
$this->host = 'api.telegram.org';
|
|
|
|
$this->ssl = true;
|
|
|
|
$this->last_update_id = 0;
|
2023-02-12 13:43:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-14 20:55:34 -05:00
|
|
|
* Returns the bot's token
|
|
|
|
*
|
2023-02-12 13:43:38 -05:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getToken(): string
|
|
|
|
{
|
|
|
|
return $this->token;
|
|
|
|
}
|
2023-02-14 20:55:34 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the host the library is using to send requests to
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getHost(): string
|
|
|
|
{
|
|
|
|
return $this->host;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the host the library will use to send requests to
|
|
|
|
*
|
|
|
|
* @param string $host
|
|
|
|
*/
|
|
|
|
public function setHost(string $host): void
|
|
|
|
{
|
|
|
|
$this->host = $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the library is using SSL to send requests
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isSsl(): bool
|
|
|
|
{
|
|
|
|
return $this->ssl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether the library will use SSL to send requests
|
|
|
|
*
|
|
|
|
* @param bool $ssl
|
|
|
|
*/
|
|
|
|
public function setSsl(bool $ssl): void
|
|
|
|
{
|
|
|
|
$this->ssl = $ssl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URL for the specified method using the current host and SSL settings
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getMethodUrl(string $method): string
|
|
|
|
{
|
|
|
|
return ($this->ssl ? 'https://' : 'http://') . $this->host . '/bot' . $this->token . '/' . $method;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a request to the Telegram API and returns the result as an array (unparsed)
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param array $params
|
|
|
|
* @return array
|
2023-02-14 21:00:59 -05:00
|
|
|
* @throws TelegramException
|
2023-02-14 20:55:34 -05:00
|
|
|
*/
|
|
|
|
public function sendRequest(string $method, array $params = []): array
|
|
|
|
{
|
|
|
|
$ch = curl_init();
|
|
|
|
|
|
|
|
curl_setopt_array($ch, [
|
|
|
|
CURLOPT_URL => $this->getMethodUrl($method),
|
|
|
|
CURLOPT_POST => true,
|
|
|
|
CURLOPT_POSTFIELDS => http_build_query($params),
|
|
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
|
|
CURLOPT_AUTOREFERER => true,
|
|
|
|
CURLOPT_HEADER => false,
|
|
|
|
CURLOPT_CONNECTTIMEOUT => 10,
|
|
|
|
CURLOPT_TIMEOUT => 30,
|
|
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
|
|
if ($response === false)
|
2023-02-14 21:00:59 -05:00
|
|
|
throw new TelegramException('Curl error: ' . curl_error($ch), curl_errno($ch));
|
2023-02-14 20:55:34 -05:00
|
|
|
|
|
|
|
curl_close($ch);
|
|
|
|
$parsed = json_decode($response, true);
|
|
|
|
if($parsed['ok'] === false)
|
2023-02-14 21:00:59 -05:00
|
|
|
throw new TelegramException($parsed['description'], $parsed['error_code']);
|
2023-02-14 20:55:34 -05:00
|
|
|
|
|
|
|
return $parsed['result'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use this method to receive incoming updates using long polling (wiki). Returns an Array of Update objects.
|
|
|
|
*
|
2023-02-14 21:02:37 -05:00
|
|
|
* @param array $options
|
2023-02-14 20:55:34 -05:00
|
|
|
* @return Update[]
|
2023-02-14 21:00:59 -05:00
|
|
|
* @throws TelegramException
|
2023-02-14 20:55:34 -05:00
|
|
|
*/
|
2023-02-14 21:02:37 -05:00
|
|
|
public function getUpdates(array $options=[]): array
|
2023-02-14 20:55:34 -05:00
|
|
|
{
|
2023-02-14 21:02:37 -05:00
|
|
|
if(!isset($options['offset']))
|
|
|
|
$options['offset'] = $this->last_update_id + 1;
|
2023-02-14 20:55:34 -05:00
|
|
|
|
|
|
|
|
|
|
|
$results = array_map(function ($update) {
|
|
|
|
return Update::fromArray($update);
|
2023-02-14 21:02:37 -05:00
|
|
|
}, $this->sendRequest('getUpdates', $options));
|
2023-02-14 20:55:34 -05:00
|
|
|
|
|
|
|
if(count($results) > 0)
|
|
|
|
$this->last_update_id = $results[count($results) - 1]->getUpdateId();
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2023-02-14 21:00:59 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2023-02-14 21:02:37 -05:00
|
|
|
* @param string $url HTTPS URL to send updates to. Use an empty string to remove webhook integration
|
|
|
|
* @param array $options
|
2023-02-14 21:00:59 -05:00
|
|
|
* @return bool
|
|
|
|
* @throws TelegramException
|
|
|
|
*/
|
2023-02-14 21:02:37 -05:00
|
|
|
public function setWebhook(string $url, array $options=[]): bool
|
2023-02-14 21:00:59 -05:00
|
|
|
{
|
2023-02-14 21:03:13 -05:00
|
|
|
$this->sendRequest('setWebhook', array_merge($options, [
|
|
|
|
'url' => $url
|
|
|
|
]));
|
2023-02-14 21:00:59 -05:00
|
|
|
return true;
|
|
|
|
}
|
2023-02-14 21:04:16 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Use this method to remove webhook integration if you decide to switch back to getUpdates.
|
|
|
|
* Returns True on success.
|
|
|
|
*
|
|
|
|
* @param bool $drop_pending_updates
|
|
|
|
* @return bool
|
|
|
|
* @throws TelegramException
|
|
|
|
*/
|
|
|
|
public function deleteWebhook(bool $drop_pending_updates=false): bool
|
|
|
|
{
|
|
|
|
$this->sendRequest('deleteWebhook', [
|
|
|
|
'drop_pending_updates' => $drop_pending_updates
|
|
|
|
]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-02-14 21:14:18 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Use this method to get current webhook status. Requires no parameters. On success, returns a WebhookInfo
|
|
|
|
* object. If the bot is using getUpdates, will return an object with the url field empty.
|
|
|
|
*
|
|
|
|
* @return WebhookInfo
|
|
|
|
* @throws TelegramException
|
|
|
|
*/
|
|
|
|
public function getWebhookInfo(): WebHookInfo
|
|
|
|
{
|
|
|
|
return WebhookInfo::fromArray($this->sendRequest('getWebhookInfo'));
|
|
|
|
}
|
2023-02-14 21:15:07 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple method for testing your bot's authentication token. Requires no parameters. Returns basic
|
|
|
|
* information about the bot in form of a User object.
|
|
|
|
*
|
|
|
|
* @return User
|
|
|
|
* @throws TelegramException
|
|
|
|
*/
|
|
|
|
public function getMe(): User
|
|
|
|
{
|
|
|
|
return User::fromArray($this->sendRequest('getMe'));
|
|
|
|
}
|
2023-02-12 13:43:38 -05:00
|
|
|
}
|