Overview
Gogram provides a powerful event-driven architecture to handle updates from Telegram. You can listen to various event types and respond to them using handler functions. The recommended way to add handlers is using theclient.On() method.
Basic Usage
Handling Messages
Handle NewMessage updates:Handling Commands
Handling Callbacks
Handle CallbackQuery updates from inline button clicks:Handling Inline Queries
Handle InlineQuery updates when users type@yourbot query:
Event Types
Message Updates
NewMessage
New incoming messages
EditMessage
Edited messages
DeleteMessage
Deleted messages
Album
Media groups/albums
Callback & Inline Updates
CallbackQuery
Inline button callbacks
InlineQuery
Inline queries (@yourbot query)
InlineCallbackQuery
Inline result button clicks
InlineSend
Chosen inline results
Other Updates
ParticipantUpdate
Chat participant changes
ActionMessage
Service messages
Raw Updates
Raw Telegram updates
Handling Different Update Types
New Messages
Handle NewMessage updates with full access to message data:Edited Messages
Deleted Messages
Albums (Media Groups)
Handle Album updates for grouped media:Callback Queries
Handle CallbackQuery updates from inline button clicks:Inline Queries
Handle InlineQuery updates:Chosen Inline Results
Participant Updates
Action Messages
Handle service messages (user joined, pinned message, etc.):Raw Updates
Handle raw Telegram updates for advanced use cases:Available Events
Filtering Updates
Apply filters to handle specific updates:Custom Filters
Create custom filter functions:Pattern Matching
Command Patterns
String Patterns
Callback Patterns
Handler Groups
Handler groups control the order and flow of handler execution. By default, handlers run in Group 0 concurrently.How Groups Work
- Conversation Group (-1): Runs first, sequentially, used internally for conversations
- Group 0 (default): Execute concurrently (parallel)
- Positive Groups (1, 2, 3…): Execute in order, sequentially
Setting Groups
Handler Priority
Within the same group, handlers execute based on priority (higher = first):Stopping Propagation
Returntelegram.EndGroup to stop handler execution in the current group:
Pattern Matching
String Patterns
Command Patterns
Callback Patterns
Removing Handlers
Advanced Patterns
Using Conversations
Use the built-in conversation feature from NewMessage: More details here.Alternative Handler Methods
Whileclient.On() is recommended, you can also use specific methods:
Complete Example
Best Practices
Use client.On()
Recommended method for adding handlers - clean and simple
Return Errors
Always return errors from handlers for proper error handling
Use Filters
Filter updates early to avoid unnecessary processing with filters
Handle Errors
Check errors and handle them gracefully - see Error Handling
Use Groups Wisely
Use groups for sequential processing, default group for parallel
Stop Propagation
Return EndGroup when you want to stop further handlers
Set Priorities
Use priorities within groups to control execution order
Close Conversations
Always defer conv.Close() when using manual conversations
Check Context
Verify message context (chat type, sender, etc.) before processing
Avoid Blocking
Don’t block handlers with long operations - use goroutines