tigris / telegram-bot-api
轻量级PHP7客户端,用于Telegram机器人API
3.2.4
2017-07-27 08:30 UTC
Requires
- php: >=7.0
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- phpunit/phpunit: ~6
This package is not auto-updated.
Last update: 2024-09-15 04:26:06 UTC
README
轻量级PHP7客户端,用于Telegram机器人API
概述
当前API版本:3.2
需求
- PHP 7
- Guzzle 6
安装
Composer
安装此扩展的首选方法是通过Composer。
运行
php composer.phar require tigris/telegram-bot-api
或在您的composer.json
文件的require部分添加
"tigris/telegram-bot-api": "*"
用法
入门指南
首先您需要创建api client
实例
$httpClient = new \GuzzleHttp\Client(); $apiClient = new \Tigris\Telegram\ApiClient($httpClient); $apiClient->setApiToken('CHANGEME');
创建api wrapper
实例
$apiWrapper = new \Tigris\Telegram\ApiWrapper($apiClient);
使用直接映射到Telegram机器人API的api wrapper
方法
$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, ]);
类型映射
请注意,每个方法调用的响应都将被解析为相应的类型。我们为所有API对象提供PHP类。例如,sendMessage()
调用将返回一个\Tigris\Telegram\Types\Message
对象实例。对象、数组、嵌套数组、标量值都由类型解析器完全支持。
获取更新
// 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); }
处理错误
默认情况下,如果发生错误,每个映射的方法返回null
值。您可以通过向ApiWrapper实例添加callable错误处理器来更改此行为。
$apiWrapper->setErrorHandler(function (\Exception $e) use ($logger) { $logger->log($e); });
进一步探索
请随时研究源代码。所有API方法和类型都完全文档化。请在gitter聊天频道中联系我们:https://gitter.im/tigris-php/tigris
许可
MIT