Context interface wraps an incoming Update and exposes the full set of accessors and actions your handler needs. Every handler function receives exactly one Context argument.
HandlerFunc
b.OnError.
NewContext
Context backed by the given bot API and update. Useful for testing or when manually dispatching updates.
The bot instance (implements
API).The update to wrap.
Update Accessors
Methods that surface the raw update payload.Bot
Update
Update struct.
Message
Update.MessageUpdate.Callback.Message(for callback queries)Update.EditedMessageUpdate.ChannelPost(or itsPinnedMessage)Update.EditedChannelPost
nil if none are present.
Callback
Update.Callback, or nil.
Query
Update.Query (inline query), or nil.
InlineResult
Update.InlineResult, or nil.
ShippingQuery
Update.ShippingQuery, or nil.
PreCheckoutQuery
Update.PreCheckoutQuery, or nil.
Payment
Update.Message.Payment, or nil if there is no message.
Poll
Update.Poll, or nil.
PollAnswer
Update.PollAnswer, or nil.
ChatMember
Update.ChatMember if present, falling back to Update.MyChatMember.
ChatJoinRequest
Update.ChatJoinRequest, or nil.
Topic
TopicCreated, TopicReopened, and TopicEdited in that order.
Boost
Update.Boost, or nil.
BoostRemoved
Update.BoostRemoved, or nil.
PurchasedPaidMedia
Update.PurchasedPaidMedia, or nil.
Migration
(migrateFrom, migrateTo) chat IDs for group→supergroup migrations. Both values are taken from Update.Message. Returns (0, 0) if the message is absent.
Convenience Accessors
Higher-level helpers that unify several update types into a single call.Sender
nil if no user is found.
Chat
nil if unavailable.
Recipient
Chat() and Sender(): returns the chat if present, otherwise the sender. Useful when you want to reply to either a private user or a group without special-casing.
Text
ThreadID
Message.ThreadID (forum thread), or 0.
Entities
CaptionEntities when present, otherwise returns Entities. Returns nil when there is no message.
Data
| Update type | Returned value |
|---|---|
| Message with payment | Payment.Payload |
| Plain message | Message.Payload (command arguments) |
| Callback | Callback.Data |
| Inline query | Query.Text |
| Inline result | InlineResult.Query |
| Shipping query | ShippingQuery.Payload |
| Pre-checkout query | PreCheckoutQuery.Payload |
Args
- Message commands: payload split by whitespace
- Payment messages: payload split by
| - Callbacks:
Callback.Datasplit by| - Inline queries / results: query text split by space
Action Methods
Send
what to the current recipient. Thread-aware: automatically injects ThreadID unless IgnoreThread is passed.
SendAlbum
Reply
ErrBadContext if there is no message in context.
Forward
msg to the current recipient.
ForwardTo
to. Returns ErrBadContext if there is no message in context.
Edit
ErrBadContext otherwise.
EditCaption
EditOrSend
Send on ErrBadContext.
EditOrReply
Reply on ErrBadContext.
Delete
ErrBadContext if there is no message.
DeleteAfter
d. The returned *time.Timer can be cancelled with its Stop method. Errors are forwarded to b.OnError.
Notify
Typing, UploadingPhoto) to the current recipient.
Respond
Update.Callback is nil.
RespondText
RespondAlert
Answer
Update.Query is nil.
Ship
Update.ShippingQuery is nil.
Accept
Update.PreCheckoutQuery is nil.
State
Get
nil if the key does not exist. Thread-safe.
Set
val under key in the context’s key-value store. Thread-safe.
The context store is local to a single update — data does not persist across requests. Use a database or the bot’s state management for persistence.
Option Flags
Option flags can be passed toSend, Reply, and other action methods as shorthand for SendOptions fields.
| Constant | Effect |
|---|---|
NoPreview | Disable web page link preview |
Silent | Send without notification sound |
AllowWithoutReply | Send even if the replied-to message is deleted |
Protected | Protect message content from forwarding/saving |
ForceReply | Request a reply interface from the user |
OneTimeKeyboard | Hide reply keyboard after one use |
RemoveKeyboard | Remove the reply keyboard |
IgnoreThread | Do not inherit thread ID from context |