Webhook
Webhook is a mode where Telegram sends updates to your server via HTTP requests. This is the recommended method for production environments.
Advantages
Section titled “Advantages”- ✅ Instant delivery of updates
- ✅ Efficient resource usage
- ✅ Suitable for production
- ✅ Support for multiple bots
1. Configuration in config/hybridgram.php
Section titled “1. Configuration in config/hybridgram.php”'bots' => [ [ 'token' => env('BOT_TOKEN'), 'bot_id' => 'main', 'update_mode' => UpdateModeEnum::WEBHOOK, 'webhook_url' => env('TELEGRAM_WEBHOOK_URL'), 'secret_token' => env('TELEGRAM_SECRET_TOKEN'), // Recommended 'routes_file' => base_path('routes/telegram.php'), ],],2. Environment Variables
Section titled “2. Environment Variables”BOT_TOKEN=your_tokenBOT_ID=mainTELEGRAM_UPDATE_MODE=WEBHOOKTELEGRAM_WEBHOOK_URL=https://your-domain.com/telegram/bot/webhook/mainTELEGRAM_SECRET_TOKEN=your_secret_token3. Setting Webhook
Section titled “3. Setting Webhook”Via Artisan Command
Section titled “Via Artisan Command”php artisan hybridgram:set-webhook mainOr with additional parameters:
php artisan hybridgram:set-webhook main \ --url=https://your-domain.com/telegram/bot/webhook/main \ --secret-token=your_secret_token \ --drop-pendingVia Code
Section titled “Via Code”use HybridGram\Telegram\TelegramBotApi;
$telegram = app(TelegramBotApi::class, ['botId' => 'main']);$telegram->setWebhook( url: 'https://your-domain.com/telegram/bot/webhook/main', secretToken: 'your_secret_token');4. Webhook Route
Section titled “4. Webhook Route”The package automatically registers a route, but you can configure your own:
// routes/web.php or routes/api.phpRoute::post('/telegram/bot/webhook/{botId}', [\HybridGram\Http\Controllers\WebhookController::class, 'handle'])->name('telegram.bot.webhook');The {botId} URL parameter is automatically resolved to a BotConfig object. The handle method receives the resolved bot configuration, not a string:
use HybridGram\Core\Config\BotConfig;use HybridGram\Http\Controllers\WebhookController;
// handle method signature — second argument is BotConfig, not stringpublic function handle(\Illuminate\Http\Request $request, BotConfig $botConfig): \Illuminate\Http\Response{ // $botConfig — object with token, bot_id, routes_file and other bot settings // ...}Security
Section titled “Security”Secret Token
Section titled “Secret Token”Using a secret token to verify request authenticity is recommended:
-
Generate a random token:
Terminal window php artisan tinker>>> Str::random(32) -
Set in
.env:TELEGRAM_SECRET_TOKEN=your_generated_token -
Use when setting webhook:
Terminal window php artisan hybridgram:set-webhook main --secret-token=your_generated_token
The package automatically verifies the secret token in the X-Telegram-Bot-Api-Secret-Token header.
Telegram requires HTTPS for webhooks. Use:
- SSL certificate (Let’s Encrypt, Cloudflare, etc.)
- Local certificate for development (not recommended for production)
SSL Certificate
Section titled “SSL Certificate”If you have a self-signed certificate:
php artisan hybridgram:set-webhook main \ --certificate=/path/to/certificate.pemOr in config:
'certificate_path' => env('TELEGRAM_CERTIFICATE_PATH'),Webhook Verification
Section titled “Webhook Verification”Get Webhook Info
Section titled “Get Webhook Info”php artisan hybridgram:get-webhook-info mainOr via code:
$telegram = app(TelegramBotApi::class, ['botId' => 'main']);$info = $telegram->getWebhookInfo();Delete Webhook
Section titled “Delete Webhook”php artisan hybridgram:delete-webhook mainAllowed Updates
Section titled “Allowed Updates”You can limit the types of updates the bot receives:
'allowed_updates' => ['message', 'callback_query', 'inline_query'],Or in .env:
ALLOWED_TELEGRAM_UPDATES=message,callback_queryDropping Pending Updates
Section titled “Dropping Pending Updates”When setting webhook, you can drop all pending updates:
php artisan hybridgram:set-webhook main --drop-pendingOr in config:
'webhook_drop_pending_updates' => true,Go Handler tgook (optional)
Section titled “Go Handler tgook (optional)”The package supports a separate Go proxy tgook for maximum performance and async update processing.
'webhook_port' => env('TELEGRAM_WEBHOOK_PORT', 9070),In this mode, Telegram sends requests to Go service, which puts updates in Redis queue, and Laravel workers process them in background.
Detailed architecture and setup instructions — in section “Go proxy tgook” (/en/advanced/go-proxy-tgook/).
Debugging
Section titled “Debugging”Logging
Section titled “Logging”Enable logging in config/logging.php:
'channels' => [ 'telegram' => [ 'driver' => 'single', 'path' => storage_path('logs/telegram.log'), ],],Request Inspection
Section titled “Request Inspection”All incoming updates are logged in telegram_updates table (if migration is executed).
Common Issues
Section titled “Common Issues”429 Too Many Requests
Section titled “429 Too Many Requests”If you get 429 error:
- Check rate limiting settings
- Make sure you’re using queue mode
- Check for duplicate requests
Webhook Not Working
Section titled “Webhook Not Working”- Check that HTTPS is configured
- Make sure URL is accessible externally
- Check secret token
- Check server logs
Update Delays
Section titled “Update Delays”- Check webhook settings via
get-webhook-info - Make sure server is not overloaded
- Check queue size
Production Recommendations
Section titled “Production Recommendations”- ✅ Use HTTPS with valid certificate
- ✅ Configure secret token
- ✅ Use queue mode for sending
- ✅ Set up monitoring and alerts
- ✅ Regularly check webhook status
- ✅ Limit
allowed_updatesto only needed types
What’s Next?
Section titled “What’s Next?”- Polling — alternative mode for receiving updates
- Sending Messages — working with TelegramBotApi