jaggman/telegram-bot-dialogs
Composer 安装命令:
composer require jaggman/telegram-bot-dialogs
包简介
Telegram Bot API PHP SDK extension that allows to implement dialogs in bots
README 文档
README
The extension for Telegram Bot API PHP SDK that allows to implement dialogs in bots
This library allows to make simple dialogs for your Telegram bots that based on the Telegram Bot SDK.
Installation
You can easy install the package using Composer:
composer require okaufmann/telegram-bot-dialogs
You can publish the config-file with:
php artisan vendor:publish --provider="BotDialogs\DialogsServiceProvider" --tag="config"
Each dialog should be implemented as class that extends basic Dialog as you can see in example bellow:
use BotDialogs\Dialog; class HelloDialog extends Dialog { // Array with methods that contains logic of dialog steps. // The order in this array defines the sequence of execution. protected $steps = ['hello', 'fine', 'bye']; public function hello() { $this->telegram->sendMessage([ 'chat_id' => $this->getChat()->getId(), 'text' => 'Hello! How are you?' ]); } public function bye() { $this->telegram->sendMessage([ 'chat_id' => $this->getChat()->getId(), 'text' => 'Bye!' ]); $this->jump('hello'); } public function fine() { $this->telegram->sendMessage([ 'chat_id' => $this->getChat()->getId(), 'text' => 'I\'m OK :)' ]); } }
For initiate new dialog you have to use Dialogs class instance to add new dialog implementation. And for execute the first and next steps you have to call Dialogs::proceed() method with update object as an argument. Also it is possible to use dialogs with Telegram commands and DI through type hinting.
use Telegram\Bot\Commands\Command; use BotDialogs\Dialogs; use App\Dialogs\HelloDialog; class HelloCommand extends Command { protected $name = 'hello'; protected $description = 'Just say "Hello" and ask few questions'; public function __construct(Dialogs $dialogs) { $this->dialogs = $dialogs; } public function handle($arguments) { $this->dialogs->add(new HelloDialog($this->update)); } }
And code in the webhook controller
use Telegram\Bot\Api; use BotDialogs\Dialogs; // ... public function __construct(Api $telegram, Dialogs $dialogs) { $this->telegram = $telegram; $this->dialogs = $dialogs; } // ... $update = $this->telegram->commandsHandler(true); if (!$this->dialogs->exists($update)) { // Do something if there are no existing dialogs } else { // Call the next step of the dialog $this->dialogs->proceed($update); }
For storing dialog information(also for the data that pushed by the Dialog::remember() method) using Redis.
Advanced definition of the dialog steps
You can define default text answers for your dialog steps. For this you have to define the step as an array with name and response fields.
class HelloDialog extends Dialog { protected $steps = [ [ 'name' => 'hello', 'response' => 'Hello my friend!' ], 'fine', 'bye' ]; // ... }
In this case, if you don't need any logic inside the step handler - you can don't define it. Just put the response inside the step definition. It works good for welcome messages, messages with tips/advices and so on. If you want format response with markdown, just set markdown field to true.
Also, you can control dialog direction in step by defining jump and end fields. jump acts as jump() method - dialog jumps to particular step. end field, is set to true, ends dialog after current step.
Also, you can use is_dich (is it a dichotomous question) option of the step. If this option set to true, you can use yes and no fields of the Dialog instance to check user answer. For example:
class HelloDialog extends Dialog { protected $steps = [ [ 'name' => 'hello', 'response' => 'Hello my friend! Are you OK?' ], [ 'name' => 'answer', 'is_dich' => true ], 'bye' ]; public function answer() { if ($this->yes) { // Send message "I am fine, thank you!" } elseif ($this->no) { // Send message "No, I am got a sick :(" } } }
In the config/dialogs.php you can modify aliases for yes/no meanings.
Often in dichotomous question you only need to send response and jump to another step. In this case, you can define steps with responses and set their names as values of 'yes', 'no' or 'default' keys of dichotomous step. For example:
class HelloDialog extends Dialog { protected $steps = [ [ 'name' => 'hello', 'response' => 'Hello my friend! Are you OK?' ], [ 'name' => 'answer', 'is_dich' => true, 'yes' => 'fine', 'no' => 'sick', 'default' => 'bye' ], [ 'name' => 'fine', 'response' => 'I am fine, thank you!', 'jump' => 'bye', ], [ 'name' => 'sick', 'response' => 'No, I am got a sick :(', ], 'bye' ]; }
Access control with in dialogs
You can inherit AuthorizedDialog class and put Telegram usernames into $allowedUsers property. After that just for users in the list will be allowed to start the dialog.
Available methods of the Dialog class
start()- Start the dialog from the first stepproceed()- Proceed the dialog to the next stepend()- End dialogjump($step)- Jump to the particular stepremember($value)- Remember some information for the next step usage (For now just a "short" memory works, just for one step)isEnd()- Check the end of the dialog
Available methods of the Dialogs class
add(Dialog $dialog)- Add the new dialogget(Telegram\Bot\Objects\Update $update)- Returns the dialog object for the existing dialogproceed(Telegram\Bot\Objects\Update $update)- Run the next step handler for the existing dialogexists(Telegram\Bot\Objects\Update $update)- Check for existing dialog
Steps configuration in separate files
You can define dialog configuration in separate yaml or php files. To do this, set scenarios in dialogs configuration file, using dialog class name as key and path to config file as value, for example:
'scenarios' => [ HelloDialog::class => base_path('config/dialogs/hello.yml') ],
Configuration from files in production environment stored in default cache instance. Because of this, you shall add php artisan cache:clear to your deployment script.
jaggman/telegram-bot-dialogs 适用场景与选型建议
jaggman/telegram-bot-dialogs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jaggman/telegram-bot-dialogs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jaggman/telegram-bot-dialogs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-03