dev-master
9999999-devPHP mini-framework for creating telegram bots.
MIT
The Requires
framework telegram
PHP mini-framework for creating telegram bots.
``` bash cd /path/to/project composer require devgoeth/tbot @dev, (*1)
#### Execute migration ``` bash php yii migrate --migrationPath=./vendor/devgoeth/tbot/migrations --interactive=0
Migration create Folders and Files:, (*2)
/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., (*3)
Edit /frontend/components/tbot/config/params.php and write your apibot token in array of params., (*4)
``` php <?php return [ 'token' => '' ];, (*5)
#### 2. Step Set Webhook for your bot, for example. ``` php $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, (*6)
Use web-hook in Yii controller action. Don't forget to disable csrf validation for your web-hook action ``` php public function actionWebHook(){ $bot = new \devgoeth\tbot\Base(); $bot->webHook(); }, (*7)
#### 4. Step Edit menu array for your buttons /frontend/components/tbot/config/menu.php For Example: ``` php <?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, (*8)
All controllers for menu array must be in tbot/controllers, (*9)
Your can turn on inline mode in message. In tbot Controller function., (*10)
``` php public function start(){ return [ 'message' => 'Welcome to bot', 'keyboard' => [ [ ['text' => 'Label for button', 'callback_data' => 'command'] ] ], 'inline' => true ]; }, (*11)
or just link ``` php 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., (*12)
``` php public function input(){ return [ 'message' => 'Input value, pls', 'keyboard' => 'default', ]; }, (*13)
public function inputInput(){ return [ 'message' => 'Your value ' . $this->params->message->text, 'keyboard' => 'default', ]; }, (*14)
### Wizard Mode You can execute command from function in tbot Controllers and create step by step wizards ``` php 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, (*15)
``` php public function myMessage(){ $keyboard = new \TelegramBot\Api\Types\ReplyKeyboardMarkup(array(array("one", "two", "three")), false); $message = 'It\'s awesome';, (*16)
// $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', ];
}, (*17)
You can access previus comand's parameters. ``` php $this->base->state->parameters;
You can send message, (*18)
``` php $this->base->send($text);, (*19)
Also you can disappear keyboard menu. In tbot action use and next message will disappear keyboard ``` php $this->base->visible = true;
PHP mini-framework for creating telegram bots.
MIT
framework telegram