Priorities & Queues
The package supports asynchronous message sending through Laravel queues with a priority system, ensuring fast processing of responses to incoming updates.
Operation Modes
Section titled “Operation Modes”Sync Mode (default)
Section titled “Sync Mode (default)”In sync mode, all requests are sent synchronously:
TELEGRAM_QUEUE_ENABLED=falseRequests execute immediately but without rate limiting. Suitable for development and small projects.
Queue Mode
Section titled “Queue Mode”In queue mode, requests are placed in queues:
TELEGRAM_QUEUE_ENABLED=trueTo run queues, you need to start workers:
# Process all queuesphp artisan queue:work --queue=telegram-high,telegram-low
# Or separately by priorityphp artisan queue:work --queue=telegram-highphp artisan queue:work --queue=telegram-lowPriorities
Section titled “Priorities”The package uses two priority levels:
HIGH (default)
Section titled “HIGH (default)”- Responses to incoming updates
- Critical messages
- All requests have this priority by default
use HybridGram\Telegram\Priority;
// Explicit specification (not required, this is default)$telegram->withPriority(Priority::HIGH) ->sendMessage($chatId, 'Fast response');- Broadcasts
- Background tasks
- Non-critical messages
$telegram->withPriority(Priority::LOW) ->sendMessage($chatId, 'Broadcast');Configuration
Section titled “Configuration”In config/hybridgram.php:
'sending' => [ 'queue_enabled' => env('TELEGRAM_QUEUE_ENABLED', false),
// Rate limit per minute per bot (default 1800 ≈ 30/sec) 'rate_limit_per_minute' => env('TELEGRAM_RATE_LIMIT_PER_MINUTE', 1800),
// Reserved slots for HIGH priority 'reserve_high_per_minute' => env('TELEGRAM_RESERVE_HIGH_PER_MINUTE', 300),
// Queue names 'queues' => [ 'high' => env('TELEGRAM_QUEUE_HIGH', 'telegram-high'), 'low' => env('TELEGRAM_QUEUE_LOW', 'telegram-low'), ],],HIGH Priority Reserve
Section titled “HIGH Priority Reserve”The reserve_high_per_minute parameter guarantees that a certain number of slots are always available for HIGH priority. LOW priority cannot use these slots.
Example:
rate_limit_per_minute = 1800reserve_high_per_minute = 300
This means:
- Up to 1800 requests per minute
- At least 300 slots always available for HIGH
- LOW can use up to 1500 slots per minute
In Route Handlers
Section titled “In Route Handlers”In route handlers, HIGH priority is used automatically:
TelegramRouter::onCommand('/start', function(CommandData $data) { $telegram = app(TelegramBotApi::class);
// Automatically HIGH priority $telegram->sendMessage($data->getChat()->id, 'Hello!');});For Broadcasts
Section titled “For Broadcasts”Explicitly specify LOW priority for broadcasts:
// Broadcast to all users$users = User::all();
foreach ($users as $user) { $telegram->withPriority(Priority::LOW) ->sendMessage($user->telegram_id, 'News!');}Mixed Usage
Section titled “Mixed Usage”use HybridGram\Core\Routing\RouteData\TextMessageData;
TelegramRouter::onTextMessage(function(TextMessageData $data) { $telegram = app(TelegramBotApi::class);
// High-priority response $telegram->sendMessage( $data->getChat()->id, 'Your message received!' );
// Low-priority task (e.g., logging) $telegram->withPriority(Priority::LOW) ->sendMessage( config('telegram.admin_chat_id'), "New message from {$data->getUser()->id}" );});Running Workers
Section titled “Running Workers”Basic Run
Section titled “Basic Run”php artisan queue:work --queue=telegram-high,telegram-lowWith Settings
Section titled “With Settings”php artisan queue:work \ --queue=telegram-high,telegram-low \ --tries=3 \ --timeout=60 \ --max-jobs=1000 \ --max-time=3600Using Supervisor (recommended)
Section titled “Using Supervisor (recommended)”Create configuration /etc/supervisor/conf.d/telegram-worker.conf:
[program:telegram-worker]process_name=%(program_name)s_%(process_num)02dcommand=php /path/to/artisan queue:work --queue=telegram-high,telegram-low --sleep=3 --tries=3 --max-time=3600autostart=trueautorestart=truestopasgroup=truekillasgroup=trueuser=www-datanumprocs=2redirect_stderr=truestdout_logfile=/path/to/storage/logs/worker.logstopwaitsecs=3600Then:
sudo supervisorctl rereadsudo supervisorctl updatesudo supervisorctl start telegram-worker:*Rate Limiting in Queue Mode
Section titled “Rate Limiting in Queue Mode”In queue mode, rate limiting is applied on workers:
- Job enters queue
- Worker takes job from queue
- Rate limit is checked
- If limit exceeded, job is returned to queue (
release()) - Worker is not blocked by
sleep(), can process other jobs
This ensures:
- Non-blocking processing
- Efficient resource usage
- Compliance with Telegram limits
Queue Monitoring
Section titled “Queue Monitoring”Checking Queue Sizes
Section titled “Checking Queue Sizes”use Illuminate\Support\Facades\Queue;
$highQueueSize = Queue::size('telegram-high');$lowQueueSize = Queue::size('telegram-low');Monitoring via Laravel Horizon
Section titled “Monitoring via Laravel Horizon”If using Laravel Horizon:
php artisan horizonThe web interface will show telegram-high and telegram-low queues.
Important Notes
Section titled “Important Notes”-
InputFile with resource not supported in queue mode. Use file paths or base64.
-
Service methods (getUpdates, setWebhook, getMe) always execute synchronously.
-
Default priority: HIGH is automatically used in route handlers.
-
Fallback: If queue is not configured, requests execute synchronously.
What’s Next?
Section titled “What’s Next?”- Rate Limiting — rate limiting details
- Operation Modes — configuring webhook and polling