Basic Usage
The easiest way to start receiving updates:Configuration Options
Stopping the Bot
Handling Signals
Gracefully shut down on process termination:Allowed Updates
Control which updates your bot receives:If you don’t specify
allowed_updates, Telegram will send all update types except chat_member, message_reaction, and message_reaction_count.Available Update Types
message- New incoming messageedited_message- Message was editedchannel_post- New channel postedited_channel_post- Channel post was editedbusiness_connection- Business connection status changedbusiness_message- New business messageedited_business_message- Business message editeddeleted_business_messages- Business messages deletedinline_query- New inline querychosen_inline_result- Result of inline query chosencallback_query- Callback button pressedshipping_query- Shipping query for invoicepre_checkout_query- Pre-checkout query for invoicepurchased_paid_media- Paid media purchasedpoll- Poll state changedpoll_answer- User changed poll answermy_chat_member- Bot’s chat member status changedchat_member- Chat member status changedchat_join_request- User requested to join chatchat_boost- Chat boost addedremoved_chat_boost- Chat boost removedmessage_reaction- Message reaction changedmessage_reaction_count- Anonymous reactions changed
Error Handling
Startup Callback
Run code when the bot is ready:Drop Pending Updates
Skip old updates when starting:Long Polling vs Webhooks
- Long Polling
- Webhooks
Advantages:
- Simple setup, no server configuration
- Works behind firewalls and NAT
- No HTTPS certificate required
- Great for development
- Higher latency (up to 30 seconds)
- Keeps a connection open
- Doesn’t scale as well
- Not ideal for serverless
Sequential Update Processing
By default, grammY processes updates sequentially:Concurrent Processing
For better performance with independent updates, use therun helper:
Development Setup
Typical development configuration:Best Practices
Graceful Shutdown
Always handle SIGINT/SIGTERM to stop the bot gracefully
Use Webhooks in Production
Switch to webhooks for better performance in production
Set allowed_updates
Only receive update types you need to reduce bandwidth
Handle Errors
Install an error handler with
bot.catch() to prevent crashesTroubleshooting
Bot not receiving updates
Bot not receiving updates
- Check if another bot instance is running (only one can receive updates at a time)
- Verify your bot token is correct
- Make sure no webhook is set:
await bot.api.deleteWebhook() - Check if you’re filtering out updates with
allowed_updates
Bot stops after a while
Bot stops after a while
- Add error handling with
bot.catch() - Check for unhandled promise rejections
- Make sure your process doesn’t exit unexpectedly
- Monitor your server’s resources
Slow update processing
Slow update processing
- Consider using concurrent processing with
run() - Optimize slow operations
- Consider switching to webhooks
- Check your network connection