botasis / telegram-client
又一个PHP Telegram机器人客户端
1.0.1
2024-09-15 10:58 UTC
Requires
- php: ^8.2
- nyholm/psr7: ^1.8
- php-http/multipart-stream-builder: ^1.3
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0||^2.0
- psr/log: ^1.0.0||^2.0.0||^3.0.0
- yiisoft/friendly-exception: ^1.1
Requires (Dev)
- maglnet/composer-require-checker: ^4.5
- phpunit/phpunit: ^10.1
- roave/infection-static-analysis-plugin: ^1.16
- symfony/console: ^6.2
- symfony/http-client: ^6.2
- vimeo/psalm: ^5.4
Suggests
- httpsoft/http-message: ^1.0.10
- symfony/http-client: ^6.0
- yiisoft/yii-console: ^1.0
README
概览
Botasis 提供了一个轻量级且严格遵守PSR规范的 Telegram机器人API 实现。它专为寻求强大且高级机器人SDK的开发者设计,Botasis 简化了Telegram机器人的创建和管理。
在我们的高级SDK botasis/runtime 中探索,并在 botasis/bot-template 中访问一个现成的应用程序模板。
快速开始
确保您已安装 Composer 并从 BotFather 获取了机器人令牌。
设置
-
安装包
composer require botasis/telegram-client
-
安装PSR HTTP客户端和消息工厂:如果您没有偏好,使用
composer require httpsoft/http-message php-http/socket-client
-
创建API客户端实例:要么使用您的DI容器,要么手动实例化
$streamFactory = new \HttpSoft\Message\StreamFactory(); $client = new \Botasis\Client\Telegram\Client\ClientPsr( getenv('BOT_TOKEN'), new \Http\Client\Socket\Client(), new \HttpSoft\Message\RequestFactory(), $streamFactory, new \Http\Message\MultipartStream\MultipartStreamBuilder($streamFactory), );
-
发送请求:发送“Hello”消息的示例
$response = $client->send(new \Botasis\Client\Telegram\Request\TelegramRequest( 'sendMessage', // any telegram bot api method you like // data to send [ 'text' => 'Hello Botasis!', 'chat_id' => $chatId, ] ));
$response
将包含Telegram机器人API的响应数据。有关详细信息,请参阅 API规范。
功能亮点
方法特定的请求类
使用严格类型的类进行API请求。例如
$requestTyped = new \Botasis\Client\Telegram\Request\Message\Message( 'Hello *Botasis*\!', // be sure you correctly escape special characters when use Markdown message format \Botasis\Client\Telegram\Request\Message\MessageFormat::MARKDOWN, $chatId, ); // This is equal, but not strictly typed request: $requestGeneric = new \Botasis\Client\Telegram\Request\TelegramRequest( 'sendMessage', [ 'text' => 'Hello *Botasis*\!', 'chat_id' => $chatId, 'parse_mode' => 'MarkdownV2', ] )
扩展功能
缺少特定方法的请求类?扩展库
- 贡献:提交带有您实现的PR。
- 回退:使用通用的
TelegramRequest
类。
这种方法确保了立即适应任何Telegram API更新。
文件上传
文档正在编制中 - 请继续关注!
错误处理
文档正在编制中 - 请继续关注!