webpajooh/telebot
Composer 安装命令:
composer require webpajooh/telebot
包简介
A minimal library to develop your new Telegram bot
README 文档
README
TeleBot
A minimal library to develop your new Telegram bot
Installation
composer require webpajooh/telebot
How to use
Start point
We start by creating an instance of TeleBot class:
try { $tg = new TeleBot('YOUR_BOT_TOKEN'); } catch (Throwable $th) {...}
Get the update object
There are short ways to access the update object and some important fields. I recommend you to read the official documentation to understand these objects well.
$tg->update
$tg->message
$tg->chat
$tg->user
You also can use the hasCallbackQuery() method, when you want to check if the update object has a callback_query field.
Methods
Thanks to magic methods, we can use API methods without implementing them, and just call them by name and pass an array as parameter:
$tg->editMessageText([...])
Router
You may define some routes to your bot features; define them by the listen() method:
$tg->listen('/start', function () use ($tg) { $tg->sendMessage([ 'chat_id' => $tg->user->id, 'text' => 'Hello, world!', ]); }, false);
The third parameter that is true by default, makes you able to terminate the script after running a command. In the previous example we passed false so script continues.
You can also get parameters and use them as variables:
$tg->listen('set_age_%d', function ($age) use ($tg) { // TODO });
TeleBot translates them to regex, so it will be good to take a look at this table to know how to use them properly:
| Type | TeleBot | Regex |
|---|---|---|
| Digits | %d | (\d+) |
| String (Anything but a whitespace) | %s | (\S+) |
| Character | %c | (\S) |
| Everything including an empty string | %p | (.*) |
Logger
You may need to log something into a log.txt file:
Logger::log($tg->user->id); tl($tg->user->id); // Does the same thing
Keyboard
TeleBot includes two classes for making keyboards; InlineKeyboard and ReplyKeyboard. Here you see an example:
$keyboard = (new InlineKeyboard()) ->addCallbackButton('📕 Help', 'help_callback') ->addUrlButton('📱 Share', 'https://t.me/share/url?url=https://t.me/your_awesome_bot&text=Some text') ->chunk(1) ->rightToLeft() ->get();
Then you can use it as bellow:
$tg->sendMessage([ // Other parameters 'reply_markup' => $keyboard, ]);
Consider that the chunk() method supports more complex orders, just pass an array like [1, 3, 2] to build such a keyboard:
[ 1 ] [ 2 ] [ 3️ ] [ 4 ] [ 5 ] [ 6 ]
Default parameters
Sometimes you do not want to repeat yourself by passing a parameter everywhere, so you can define default parameters for each method. Here are three example that makes it clear how to use it:
$tg->setDefaults('sendMessage', ['parse_mode' => 'html']); // You will not need passing parse_mode anymore $tg->setDefaults(['sendMessage', 'banChatMember'], ['chat_id' => $chatId]); $tg->setDefaults('*', ['chat_id' => $chatId]); // a default parameter for all methods
Extend it!
You may want to add some methods to TeleBot class to improve your code readability and avoid duplication. Look at this simple example as an inspiration:
TeleBot::extend('isReply', function () { return property_exists($this->message, 'reply_to_message'); }); if ($tg->isReply()) { ... }
Have you seen a problem?
Create an issue and explain your problem!
Made with TeleBot
webpajooh/telebot 适用场景与选型建议
webpajooh/telebot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 212 次下载、GitHub Stars 达 54, 最近一次更新时间为 2020 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「telegram」 「telegram bot」 「telegram bot api」 「telegram sdk」 「telegram php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webpajooh/telebot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webpajooh/telebot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webpajooh/telebot 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dialogflow php sdk
Laravel 5.6 Monolog custom telegram channel
A PSR-7 compatible library for making CRUD API endpoints
PHP Wrapper for Telegram Bot API
VK driver for botman
Telegram bot integration with laravel
统计信息
- 总下载量: 212
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 55
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-05-24