Working with Multiple Bots
The package supports working with multiple bots simultaneously. Each bot has its own configuration and routes.
Multiple Bot Configuration
Section titled “Multiple Bot Configuration”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'), ],],Environment Variables
Section titled “Environment Variables”# First botBOT_TOKEN_1=first_bot_tokenTELEGRAM_WEBHOOK_URL_1=https://your-domain.com/telegram/bot/webhook/main
# Second botBOT_TOKEN_2=second_bot_tokenTELEGRAM_WEBHOOK_URL_2=https://your-domain.com/telegram/bot/webhook/support
# Third botBOT_TOKEN_3=third_bot_tokenRouting for Specific Bot
Section titled “Routing for Specific Bot”Using forBot()
Section titled “Using forBot()”use HybridGram\Facades\TelegramRouter;
TelegramRouter::forBot('main')->onCommand('/start', function(CommandData $data) { // Handle only for 'main' bot});
// routes/telegram-support.phpTelegramRouter::forBot('support')->onCommand('/start', function(CommandData $data) { // Handle only for 'support' bot});Common Routes for All Bots
Section titled “Common Routes for All Bots”// Route for all botsTelegramRouter::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');Grouping Routes by Bots
Section titled “Grouping Routes by Bots”TelegramRouter::group([ 'botId' => 'main',], function($router) { $router->onCommand('/start', function(CommandData $data) { // ... });
$router->onCommand('/menu', function(CommandData $data) { // ... });});Different Operation Modes
Section titled “Different Operation Modes”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 ],],Webhook Management
Section titled “Webhook Management”Setting Webhook for Specific Bot
Section titled “Setting Webhook for Specific Bot”php artisan hybridgram:set-webhook mainphp artisan hybridgram:set-webhook supportGetting Webhook Info
Section titled “Getting Webhook Info”php artisan hybridgram:get-webhook-info mainphp artisan hybridgram:get-webhook-info supportRunning Polling for Multiple Bots
Section titled “Running Polling for Multiple Bots”Run separate process for each bot:
# Terminal 1php artisan hybridgram:polling main
# Terminal 2php artisan hybridgram:polling supportOr use Supervisor:
[program:telegram-main]command=php /path/to/artisan hybridgram:polling main
[program:telegram-support]command=php /path/to/artisan hybridgram:polling supportData Isolation
Section titled “Data Isolation”Each bot has:
- Its own routes
- Its own rate limit (separate counter)
- Its own states (isolated)
- Its own config
Sharing Code Between Bots
Section titled “Sharing Code Between Bots”You can extract common logic into services:
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); }}
// UsageTelegramRouter::forBot('main')->onCommand('/start', function(CommandData $data) { app(CommonService::class)->handleWelcome($data, 'main');});Recommendations
Section titled “Recommendations”- ✅ Use unique
bot_idfor each bot - ✅ Separate routes into files for better organization
- ✅ Use prefixes in environment variable names
- ✅ Document the purpose of each bot
- ✅ Monitor rate limits for each bot separately
What’s Next?
Section titled “What’s Next?”- Configuration — detailed configuration
- Webhook — setting up webhooks for multiple bots