Skip to content

Working with Multiple Bots

The package supports working with multiple bots simultaneously. Each bot has its own configuration and routes.

In the config/hybridgram.php file:

'bots' => [
[
'token' => env('BOT_TOKEN_1'),
'bot_id' => 'main',
'update_mode' => UpdateModeEnum::WEBHOOK,
'webhook_url' => env('TELEGRAM_WEBHOOK_URL_1'),
'routes_file' => base_path('routes/telegram-main.php'),
],
[
'token' => env('BOT_TOKEN_2'),
'bot_id' => 'support',
'update_mode' => UpdateModeEnum::WEBHOOK,
'webhook_url' => env('TELEGRAM_WEBHOOK_URL_2'),
'routes_file' => base_path('routes/telegram-support.php'),
],
[
'token' => env('BOT_TOKEN_3'),
'bot_id' => 'admin',
'update_mode' => UpdateModeEnum::POLLING,
'routes_file' => base_path('routes/telegram-admin.php'),
],
],
# First bot
BOT_TOKEN_1=first_bot_token
TELEGRAM_WEBHOOK_URL_1=https://your-domain.com/telegram/bot/webhook/main
# Second bot
BOT_TOKEN_2=second_bot_token
TELEGRAM_WEBHOOK_URL_2=https://your-domain.com/telegram/bot/webhook/support
# Third bot
BOT_TOKEN_3=third_bot_token
routes/telegram-main.php
use HybridGram\Facades\TelegramRouter;
TelegramRouter::forBot('main')->onCommand('/start', function(CommandData $data) {
// Handle only for 'main' bot
});
// routes/telegram-support.php
TelegramRouter::forBot('support')->onCommand('/start', function(CommandData $data) {
// Handle only for 'support' bot
});
// Route for all bots
TelegramRouter::onCommand('/help', function(CommandData $data) {
// $data->botId contains ID of the bot for which route triggered
$botId = $data->botId;
// Different logic depending on bot
match($botId) {
'main' => $this->handleMainHelp($data),
'support' => $this->handleSupportHelp($data),
default => $this->handleDefaultHelp($data),
};
});

Getting TelegramBotApi Instance for Specific Bot

Section titled “Getting TelegramBotApi Instance for Specific Bot”
// For specific bot
$telegram = app(\HybridGram\Telegram\TelegramBotApi::class, ['botId' => 'main']);
$telegram->sendMessage($chatId, 'Message from main bot');
$telegram = app(\HybridGram\Telegram\TelegramBotApi::class, ['botId' => 'support']);
$telegram->sendMessage($chatId, 'Message from support bot');
TelegramRouter::group([
'botId' => 'main',
], function($router) {
$router->onCommand('/start', function(CommandData $data) {
// ...
});
$router->onCommand('/menu', function(CommandData $data) {
// ...
});
});

Each bot can work in its own mode:

'bots' => [
[
'bot_id' => 'main',
'update_mode' => UpdateModeEnum::WEBHOOK, // Production bot
],
[
'bot_id' => 'dev',
'update_mode' => UpdateModeEnum::POLLING, // Dev bot
],
],
Terminal window
php artisan hybridgram:set-webhook main
php artisan hybridgram:set-webhook support
Terminal window
php artisan hybridgram:get-webhook-info main
php artisan hybridgram:get-webhook-info support

Run separate process for each bot:

Terminal window
# Terminal 1
php artisan hybridgram:polling main
# Terminal 2
php artisan hybridgram:polling support

Or use Supervisor:

[program:telegram-main]
command=php /path/to/artisan hybridgram:polling main
[program:telegram-support]
command=php /path/to/artisan hybridgram:polling support

Each bot has:

  • Its own routes
  • Its own rate limit (separate counter)
  • Its own states (isolated)
  • Its own config

You can extract common logic into services:

app/Telegram/Services/CommonService.php
class CommonService
{
public function handleWelcome(CommandData $data, string $botId): void
{
$telegram = app(\HybridGram\Telegram\TelegramBotApi::class, ['botId' => $botId]);
$message = match($botId) {
'main' => 'Welcome to the main bot!',
'support' => 'Welcome to support!',
default => 'Welcome!',
};
$telegram->sendMessage($data->getChat()->id, $message);
}
}
// Usage
TelegramRouter::forBot('main')->onCommand('/start', function(CommandData $data) {
app(CommonService::class)->handleWelcome($data, 'main');
});
  1. ✅ Use unique bot_id for each bot
  2. ✅ Separate routes into files for better organization
  3. ✅ Use prefixes in environment variable names
  4. ✅ Document the purpose of each bot
  5. ✅ Monitor rate limits for each bot separately