Skip to main content
Every handler in Telebot receives a single 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

All handlers have this signature. Return nil on success, or a non-nil error to trigger the bot’s OnError callback.

What Context wraps

Internally, the native context stores:
  • b API — the bot instance (or any mock that satisfies API)
  • u Update — the raw Telegram update
  • A thread-safe map[string]interface{} for arbitrary per-request values
You receive a Context in every handler; you can also construct one manually for testing:

Update accessors

These methods extract the relevant payload from the wrapped Update. 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

Example — reading a command argument:

Specialised update accessors

Sending helpers

All send helpers target the current recipient (from c.Recipient()) automatically.

Send and Reply

Forward

Edit

EditOrSend and EditOrReply are especially useful for handlers that can be reached from both commands and callback buttons.

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.
Example — middleware sets a value, handler reads it:
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.