Event System Overview
Gogram is built on an event-driven architecture. Instead of polling Telegram manually, the library listens for updates and dispatches them to registered handler functions.Basic Usage
The primary way to handle updates is through theclient.On() method, which uses flexible pattern matching.
Handler Shortcuts
For cleaner code, use the builder-style shortcut methods available on the client.| Method | Description |
|---|---|
client.OnMessage(pattern, handler) | Registers a message handler builder. |
client.OnCommand(command, handler) | Registers a command handler builder. |
client.OnCallback(pattern, handler) | Registers a callback handler builder. |
Other Shortcuts
Gogram provides shortcuts for almost every event type:| Method | Description |
|---|---|
client.OnInlineQuery(pattern, handler) | Handle inline queries. |
client.OnInlineCallback(pattern, handler) | Handle inline button callbacks. |
client.OnEdit(pattern, handler) | Handle message edits. |
client.OnDelete(pattern, handler) | Handle message deletions. |
client.OnAlbum(handler) | Handle grouped media albums. |
client.OnChosenInline(handler) | Handle chosen inline results. |
client.OnParticipant(handler) | Handle chat participant changes. |
client.OnJoinRequest(handler) | Handle requests to join a chat. |
client.OnRaw(updateType, handler) | Handle raw TL updates. |
client.OnE2EMessage(handler) | Handle End-to-End encrypted messages. |
Middleware
Middleware allows you to wrap your handlers with cross-cutting concerns like logging, rate limiting, or authentication checks.Pattern Matching
Gogram supports powerful pattern matching for messages and callbacks.Command Patterns
Commands are automatically parsed handling standard prefixes (default/ and !).
cmd:startmatches/start,!start, and/start@yourbot.cmd:settingsmatches/settingswith arguments.
Regex and String Matching
You can use Regular Expressions for advanced text matching."hello": Matches the exact string “hello”."^Hello": Matches any message starting with “Hello” (Regex)."(?i)hi": Case-insensitive match for “hi” (Regex).
Callback Data
"btn_next": Exact match for callback data."^page_\d+": Matches “page_1”, “page_2”, etc.
Managing Handlers
Groups and Execution Order
Handlers are organized into Groups to control execution flow.- Group 0 (Default): Concurrent execution. All handlers run in parallel.
- Positive Groups (1, 2…): Sequential execution. Group 1 runs, then Group 2, etc.
- Group -1: System/Conversation group. Runs before everything else.
Priority
Within a group, Priority determines the order. Higher priority runs first.Stopping Propagation
To stop processing subsequent handlers in the current group, returntelegram.ErrEndGroup.
Specialized Handlers
For strict typing and specific use cases, use the specializedAdd...Handler methods. These return a Handle directly without the builder interface.
| Method | Signature | Description |
|---|---|---|
AddMessageHandler | func(*NewMessage) error | General messages with filters. |
AddCommandHandler | func(*NewMessage) error | Optimized for commands. |
AddEditHandler | func(*NewMessage) error | Edited text/media messages. |
AddDeleteHandler | func(*DeleteMessage) error | Deleted messages. |
AddCallbackHandler | func(*CallbackQuery) error | Inline button clicks. |
AddInlineHandler | func(*InlineQuery) error | Incoming inline queries. |
AddInlineSendHandler | func(*InlineSend) error | Result chosen from inline query. |
AddAlbumHandler | func(*Album) error | Grouped media (albums). |
AddParticipantHandler | func(*ParticipantUpdate) error | User join/leave events. |
AddJoinRequestHandler | func(*JoinRequestUpdate) error | Admin approval requests. |
AddActionHandler | func(*NewMessage) error | Service messages (pins, calls). |
AddRawHandler | func(Update, *Client) error | Low-level raw update handling. |
AddE2EHandler | func(Update, *Client) error | End-to-End encrypted chat updates. |
Channel Optimization
For high-traffic channels where real-time updates are critical, you can enable aggressive polling usingOpenChat. This periodically actively fetches updates for that specific channel.