Telegram Bot API PHP SDK extension that allows to implement dialogs in bots
koot-labs/telegram-bot-dialogs is a Laravel package for telegram bot api php sdk extension that allows to implement dialogs in bots.
It currently has 14 GitHub stars and 2.901 downloads on Packagist (latest version 1.2.2).
Install it with composer require koot-labs/telegram-bot-dialogs.
Discover more Laravel packages by koot-labs
or browse all Laravel packages to compare alternatives.
Last updated

A powerful extension for Telegram Bot API PHP SDK v3.1+ that enables dialog-based interactions in your Telegram bots.
This package is a maintained fork of the original Telegram Bot Dialogs package, updated to support Telegram Bot API PHP SDK v3, PHP 8+, and modern Laravel features. Our focus is on stability, developer experience, and code readability.
The Original package is not maintained anymore and does not support Telegram Bot API PHP SDK v3. The goal of the fork is to maintain the package compatible with the latest Telegram Bot API PHP SDK, PHP 8+ and Laravel features, focus on stability, better DX and readability.
Any bot app basically listens to Updates from Telegram API (using your webhook endpoint or by pulling these updates on any trigger, like cron) and sends messages back.
This package helps to implement a dialog mode for your bot: for a given Update, check whether the Update belongs to an already activated Dialog and if there is, run the next step of the Dialog.
This package doesn't solve the task to activate Dialogs for a given Update—you need to implement this logic in your app. Different apps may have different strategies to activate Dialogs (e.g. by commands, by message content, by message type, by user_id, etc.). The package provides an API to activate Dialogs and run the next step for the active Dialog.
Install via Composer:
composer require koot-labs/telegram-bot-dialogs
The package automatically registers \KootLabs\TelegramBotDialogs\Laravel\DialogsServiceProvider
Publish the configuration:
php artisan vendor:publish --tag="telegram-config"
This creates config/telegramdialogs.php with these environment variables:
TELEGRAM_DIALOGS_CACHE_DRIVER (default: database)TELEGRAM_DIALOGS_CACHE_PREFIX (default: tg_dialog_)For non-Laravel applications, see our framework-agnostic guide.
Create a dialog class extending Dialog:
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');
}
}
In this example, the Dialog is activated by a command. You can also activate dialogs based on other triggers (like an Update/Message type, or a work inside a Message).
Create a command to activate your dialog (Laravel example):
use App\Dialogs\HelloDialog;
use KootLabs\TelegramBotDialogs\Laravel\Facades\Dialogs;
use Telegram\Bot\Commands\Command;
final class HelloCommand extends Command
{
protected $name = 'hello';
protected $description = 'Start a hello dialog';
public function handle(): void
{
Dialogs::activate(new HelloDialog($this->update->getChat()->id));
}
}
Handle webhook updates in your controller:
use Telegram\Bot\BotsManager;
use KootLabs\TelegramBotDialogs\DialogManager;
final class TelegramWebhookHandler
{
public function handle(DialogManager $dialogs, BotsManager $botsManager): void
{
// Find a \Telegram\Bot\Commands\Command instance for the Update and execute it
// for /hello command, it should call HelloCommand that will activate HelloDialog
$update = $bot->commandsHandler(true);
$dialogs->hasActiveDialog($update)
? $dialogs->processUpdate($update) // Run the next step of the active dialog
: $botsManager->sendMessage([ // send a fallback message
'chat_id' => $update->getChat()->id,
'text' => 'No active dialog. Type /hello to start.',
]);
}
}
abstract class Dialog
{
// Navigation
public function nextStep(string $stepName): void;
public function switch(string $stepName): void;
public function complete(): void;
// State Management
public function isAtStart(): bool;
public function isLastStep(): bool;
public function isCompleted(): bool;
// Lifecycle Hooks
protected function beforeEveryStep(Update $update, int $stepIndex): void;
protected function afterEveryStep(Update $update, int $stepIndex): void;
protected function beforeFirstStep(Update $update): void;
protected function afterLastStep(Update $update): void;
// Properties Access
public function getChatId(): int;
public function getUserId(): ?int;
public function getTtl(): ?int;
}
The DialogManager handles:
Laravel users can use the Dialogs facade:
use KootLabs\TelegramBotDialogs\Laravel\Facades\Dialogs;
// Activate a dialog
Dialogs::activate($dialog);
// Process an update
Dialogs::processUpdate($update);
// Check for active dialog
Dialogs::hasActiveDialog($update);
// Set custom bot instance
Dialogs::setBot($bot);
Contributions are welcome! Please see our Contributing Guide for details.
Run the test suite:
composer test
This package is open-sourced software licensed under the MIT license.
Tasks planned for v1.0:
We follow Semver 2.0. Breaking changes are only introduced in major versions.
Note:
@experimental or @internal are not covered by BC promise