PHP code example of koot-labs / telegram-bot-dialogs
1. Go to this page and download the library: Download koot-labs/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/ */
koot-labs / telegram-bot-dialogs example snippets
use KootLabs\TelegramBotDialogs\Dialog;
use Telegram\Bot\Objects\Update;
final class HelloDialog extends Dialog
{
/** @var list<string> List of method to execute. The order defines the sequence */
protected array $steps = ['sayHello', 'sayOk',];
public function sayHello(Update $update): void
{
$this->bot->sendMessage([
'chat_id' => $this->getChatId(),
'text' => 'Hello! How are you?',
]);
}
public function sayOk(Update $update): void
{
$this->bot->sendMessage([
'chat_id' => $this->getChatId(),
'text' => 'I’m also OK :)',
]);
$this->nextStep('sayHello');
}
}
use App\Dialogs\HelloExampleDialog;
use KootLabs\TelegramBotDialogs\Laravel\Facades\Dialogs;
use Telegram\Bot\Commands\Command;
final class HelloCommand extends Command
{
/** @var string Command name */
protected $name = 'hello';
/** @var string Command description */
protected $description = 'Just say "Hello" and ask few questions in a dialog mode.';
public function handle(): void
{
Dialogs::activate(new HelloExampleDialog($this->update->getChat()->id));
}
}
use Telegram\Bot\BotsManager;
use KootLabs\TelegramBotDialogs\DialogManager;
final class TelegramWebhookController
{
public function handle(DialogManager $dialogs, BotsManager $botsManager): void
{
$update = $bot->commandsHandler(true);
$dialogs->exists($update)
? $dialogs->proceed($update) // proceed an active dialog (activated in HelloCommand)
: $botsManager->sendMessage([ // fallback message
'chat_id' => $update->getChat()->id,
'text' => 'There is no active dialog at this moment. Type /hello to start a new dialog.',
]);
}
}