Tigris: Telegram Bot API Client
, (*1)
Lightweight PHP7 client for a Telegram Bot API, (*2)
Overview
Current API version: 3.2, (*3)
Requirements:
* PHP 7
* Guzzle 6, (*4)
Installation
Composer
The preferred way to install this extension is through Composer., (*5)
Either run, (*6)
php composer.phar require tigris/telegram-bot-api
or add, (*7)
"tigris/telegram-bot-api": "*"
to the require section of your composer.json
, (*8)
Usage
Getting started
First you need to create api client instance, (*9)
$httpClient = new \GuzzleHttp\Client();
$apiClient = new \Tigris\Telegram\ApiClient($httpClient);
$apiClient->setApiToken('CHANGEME');
Create api wrapper instance, (*10)
$apiWrapper = new \Tigris\Telegram\ApiWrapper($apiClient);
Use api wrapper methods mapped directly to the Telegram Bot API, (*11)
$apiWrapper->sendChatAction([
'chat_id' => 123,
'action' => \Tigris\Telegram\Helpers\ChatAction::TYPING,
]);
$apiWrapper->sendMessage([
'chat_id' => 123
'text' => 'Hello, World!',
'parse_mode' => \Tigris\Telegram\Helpers\ParseMode::HTML,
]);
Type mapping
Please note that every method call response call is being parsed into the
corresponding type. We offer PHP classes for all of the API objects.
For example, sendMessage()
call would return an instance of
\Tigris\Telegram\Types\Message
objects. Objects, arrays, nested arrays, scalar values
are fully supported by the type parser., (*12)
Getting updates
// Get array of the \Tigris\Telegram\Types\Updates\Update
$updates = $apiWrapper->getUpdates([
'offset' => $this->offset,
]);
// Process received updates
foreach ($updates as $update) {
$this->offset = $update->update_id + 1;
$this->setLastOffset($this->offset);
$this->processUpdate($update);
}
Handling errors
By default every mapped method returns null
value if an error occurs.
You can change this behavior by adding callable error handler to your ApiWrapper instance., (*13)
$apiWrapper->setErrorHandler(function (\Exception $e) use ($logger) {
$logger->log($e);
});
Going further
Please feel free to investigate the source code. All the api methods and types are
fully documented. Please contact us in the gitter chat channel:
https://gitter.im/tigris-php/tigris, (*14)
License
MIT, (*15)