cd /path/to/project
composer require devgoeth/tbot @devphp yii migrate --migrationPath=./vendor/devgoeth/tbot/migrations --interactive=0Migration create Folders and Files:
/frontend/components/tbot
/frontend/components/tbot/config/menu.php
/frontend/components/tbot/config/params.php
/frontend/components/tbot/controllers/DefaultController.php
You might view example with migration.
Edit /frontend/components/tbot/config/params.php and write your apibot token in array of params.
<?php
return [
'token' => ''
];Set Webhook for your bot, for example.
$url = 'https://' . $_SERVER['SERVER_NAME'] . '/test/web-hook';
$crt = './../../ssl/bundle.crt';
$bot = new \devgoeth\tbot\Base();
$bot->setWebHook($url, $crt);And now telegram will be send data to your web-hook action
Use web-hook in Yii controller action. Don't forget to disable csrf validation for your web-hook action
public function actionWebHook(){
$bot = new \devgoeth\tbot\Base();
$bot->webHook();
}Edit menu array for your buttons
/frontend/components/tbot/config/menu.php
For Example:
<?php
return [
'noneMenuFunctions' => [
['/start' => 'Default/start'],
['/other' => 'Default/start'],
],
'default' => [
['The Button' => 'Default/button'],
[
'The Wizard' => 'Default/wizard',
'The Input' => 'Default/input'
],
]
];Where is 'Label for Button' => 'controllerName/functionName', all function which execute whithout menu must be in 'noneMenuFunction' array
All controllers for menu array must be in tbot/controllers
Your can turn on inline mode in message. In tbot Controller function.
public function start(){
return [
'message' => 'Welcome to bot',
'keyboard' => [
[
['text' => 'Label for button', 'callback_data' => 'command']
]
],
'inline' => true
];
}or just link
public function start(){
return [
'message' => 'Welcome to bot',
'keyboard' => [
[
['text' => 'Google', 'url' => 'https://google.com']
]
],
'inline' => true
];
}In tbot/controllers/DefaultController.php (Don't forget to create button 'The Input' => 'Default/input' in menu.php) You must add prefix Input for your function and it will be execute after main function.
public function input(){
return [
'message' => 'Input value, pls',
'keyboard' => 'default',
];
}
public function inputInput(){
return [
'message' => 'Your value ' . $this->params->message->text,
'keyboard' => 'default',
];
}You can execute command from function in tbot Controllers and create step by step wizards
public function wizard(){
return $this->base->executeCommand('Default/input');
}In Controller you can use base parameter which contain all base parameters include object of 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',
];
}You can access previus comand's parameters.
$this->base->state->parameters;You can send message
$this->base->send($text);Also you can disappear keyboard menu. In tbot action use and next message will disappear keyboard
$this->base->visible = true;