Lightweight PHP7 client for a Telegram Bot API
Current API version: 3.2
Requirements:
- PHP 7
- Guzzle 6
The preferred way to install this extension is through Composer.
Either run
php composer.phar require tigris/telegram-bot-api
or add
"tigris/telegram-bot-api": "*"
to the require section of your composer.json
First you need to create api client instance
$httpClient = new \GuzzleHttp\Client();
$apiClient = new \Tigris\Telegram\ApiClient($httpClient);
$apiClient->setApiToken('CHANGEME');
Create api wrapper instance
$apiWrapper = new \Tigris\Telegram\ApiWrapper($apiClient);
Use api wrapper methods mapped directly to the Telegram Bot API
$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,
]);
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.
// 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);
}
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.
$apiWrapper->setErrorHandler(function (\Exception $e) use ($logger) {
$logger->log($e);
});
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
MIT