2017 © Pedro Peláez
 

library tbot

PHP mini-framework for creating telegram bots.

image

devgoeth/tbot

PHP mini-framework for creating telegram bots.

  • Monday, May 21, 2018
  • by devgoeth
  • Repository
  • 1 Watchers
  • 0 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 6 % Grown

The README.md

Installation

``` bash cd /path/to/project composer require devgoeth/tbot @dev, (*1)


#### Execute migration ``` bash php yii migrate --migrationPath=./vendor/devgoeth/tbot/migrations --interactive=0

Overview

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

Examples

You might view example with migration., (*3)

1. Step

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)

3. Step

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)

Inline mode

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 ]; }

Input mode

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'); }

Base

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;

The Versions

21/05 2018

dev-master

9999999-dev

PHP mini-framework for creating telegram bots.

  Sources   Download

MIT

The Requires

 

framework telegram