devgoeth / tbot
PHP 微型框架,用于创建Telegram机器人。
Requires
- php: >=5.4
- telegram-bot/api: ^2.3
- yiisoft/yii2-httpclient: ^2.0
This package is not auto-updated.
Last update: 2024-09-27 09:30:31 UTC
README
cd /path/to/project
composer require devgoeth/tbot @dev
执行迁移
php yii migrate --migrationPath=./vendor/devgoeth/tbot/migrations --interactive=0
概述
迁移创建文件夹和文件
/frontend/components/tbot
/frontend/components/tbot/config/menu.php
/frontend/components/tbot/config/params.php
/frontend/components/tbot/controllers/DefaultController.php
示例
您可能需要查看具有迁移的示例。
1. 步骤
编辑 /frontend/components/tbot/config/params.php 并在参数数组中写入您的 apibot 令牌。
<?php return [ 'token' => '' ];
2. 步骤
为您的机器人设置Webhook,例如。
$url = 'https://' . $_SERVER['SERVER_NAME'] . '/test/web-hook'; $crt = './../../ssl/bundle.crt'; $bot = new \devgoeth\tbot\Base(); $bot->setWebHook($url, $crt);
现在Telegram将向您的webhook动作发送数据
3. 步骤
在Yii控制器动作中使用webhook。不要忘记为您的webhook动作禁用csrf验证
public function actionWebHook(){ $bot = new \devgoeth\tbot\Base(); $bot->webHook(); }
4. 步骤
编辑按钮的菜单数组
/frontend/components/tbot/config/menu.php
例如
<?php return [ 'noneMenuFunctions' => [ ['/start' => 'Default/start'], ['/other' => 'Default/start'], ], 'default' => [ ['The Button' => 'Default/button'], [ 'The Wizard' => 'Default/wizard', 'The Input' => 'Default/input' ], ] ];
“按钮标签”=》“controllerName/functionName”,所有不通过菜单执行的功能必须在“noneMenuFunction”数组中
菜单数组中必须包含tbot/controllers下的所有控制器
内联模式
您可以在消息中开启内联模式。在tbot控制器函数中。
public function start(){ return [ 'message' => 'Welcome to bot', 'keyboard' => [ [ ['text' => 'Label for button', 'callback_data' => 'command'] ] ], 'inline' => true ]; }
或直接链接
public function start(){ return [ 'message' => 'Welcome to bot', 'keyboard' => [ [ ['text' => 'Google', 'url' => 'https://google.com'] ] ], 'inline' => true ]; }
输入模式
在 tbot/controllers/DefaultController.php(不要忘记在 menu.php 中创建按钮 'The Input' => 'Default/input')中,您必须为您的函数添加前缀 Input,它将在主函数之后执行。
public function input(){ return [ 'message' => 'Input value, pls', 'keyboard' => 'default', ]; } public function inputInput(){ return [ 'message' => 'Your value ' . $this->params->message->text, 'keyboard' => 'default', ]; }
向导模式
您可以在tbot控制器中的函数中执行命令并创建逐步向导
public function wizard(){ return $this->base->executeCommand('Default/input'); }
基本
在控制器中,您可以使用包含所有基本参数以及 TelegramBot\Api 对象的基参数 https://github.com/TelegramBot/Api
public function myMessage(){ $keyboard = new \TelegramBot\Api\Types\ReplyKeyboardMarkup(array(array("one", "two", "three")), false); $message = 'It\'s awesome'; // $this->base->markUp = 'html' by default; $this->base->bot->sendMessage( $this->params->message->chat->id, $message, $this->base->markUp, false, null, $keyboard ); return [ 'message' => 'Input value, pls', 'keyboard' => 'default', ]; }
您可以访问前一个命令的参数。
$this->base->state->parameters;
您可以发送消息
$this->base->send($text);
您还可以消失键盘菜单。在tbot动作中使用,并且下一条消息将消失键盘
$this->base->visible = true;