Context argument. It wraps the raw Update together with a reference to the bot, and provides a rich set of accessor and send helpers so you rarely need to touch the underlying Update struct directly.
HandlerFunc
nil on success, or a non-nil error to trigger the bot’s OnError callback.
What Context wraps
b API— the bot instance (or any mock that satisfiesAPI)u Update— the raw Telegram update- A thread-safe
map[string]interface{}for arbitrary per-request values
Context in every handler; you can also construct one manually for testing:
Update accessors
These methods extract the relevant payload from the wrappedUpdate. They return nil (or zero values) when the update does not contain that type of data.
Bot()
Returns the
API instance the context is bound to.Update()
Returns the raw
Update struct.Message()
Returns the associated
*Message. Works for plain messages, callback messages, channel posts, and edited messages.Callback()
Returns
*Callback when the update is a button press.Query()
Returns
*Query for inline queries.Sender()
Returns the
*User who triggered the update, regardless of update type. Returns nil for anonymous updates.Chat()
Returns the
*Chat associated with the current update.Recipient()
Combines
Chat() and Sender(). Returns Chat if available, otherwise Sender. Never returns nil for native contexts.Text and data
Specialised update accessors
Sending helpers
All send helpers target the current recipient (fromc.Recipient()) automatically.
Send and Reply
Forward
Edit
Delete
Callbacks and inline queries
Payments
Notify
Storing custom values: Get / Set
Context has a built-in thread-safe key/value store. Use it to pass data between middleware and handlers without global state.
Get returns nil (not a panic) when the key does not exist. Always perform a nil check or type assertion guard before using the value.