PHP code example of zarincheg / telegram-bot-dialogs
1. Go to this page and download the library: Download zarincheg/telegram-bot-dialogs library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use BotDialogs\Dialog;
/**
* Class HelloDialog
*/
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 :)'
]);
}
}
use Telegram\Bot\Commands\Command;
use BotDialogs\Dialogs;
use App\Dialogs\HelloDialog;
class HelloCommand extends Command
{
/**
* @var string Command name
*/
protected $name = 'hello';
protected $description = 'Just say "Hello" and ask few questions';
/**
* @param Dialogs $dialogs
*/
public function __construct(Dialogs $dialogs)
{
$this->dialogs = $dialogs;
}
public function handle($arguments)
{
$this->dialogs->add(new HelloDialog($this->update));
}
}
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);
}