pub struct Context {
pub update: Arc<Update>,
pub bot: BotClient,
}Expand description
The context object passed to every handler.
Contains the incoming Update, the BotClient (ready to make API
calls), and a reference to any shared bot-level state.
The context object passed to every handler.
Context bundles the incoming Update with the BotClient so
handlers have everything they need in a single value.
§Accessing the update
async fn handler(ctx: Context) -> BotResult<()> {
// Convenience accessors
let text = ctx.text(); // message text or caption
let cmd = ctx.command(); // "/start" → Some("start")
let chat_id = ctx.chat_id();
let user_id = ctx.from_id();
// Raw update for anything not covered by the helpers
let update = &ctx.update;
Ok(())
}§Sending replies
ctx.reply(text) is a shortcut that sends a message to the current chat
and automatically sets reply_to_message_id. It returns None when the
update has no associated chat (e.g. inline queries).
if let Some(reply) = ctx.reply("Got it!") {
reply.parse_mode(ParseMode::HTML).await?;
}Fields§
§update: Arc<Update>The incoming update that triggered this handler.
bot: BotClientThe API client — call any Bot API method directly.
Implementations§
Source§impl Context
impl Context
Sourcepub fn message(&self) -> Option<&Message>
pub fn message(&self) -> Option<&Message>
Returns the Message from a message, edited message, channel post,
or callback query update. Returns None for all other update types.
Sourcepub fn chat_id(&self) -> Option<ChatId>
pub fn chat_id(&self) -> Option<ChatId>
Returns the chat ID from the current update as a ChatId, if available.
Sourcepub fn callback_query(&self) -> Option<&CallbackQuery>
pub fn callback_query(&self) -> Option<&CallbackQuery>
Returns the CallbackQuery if this is a callback query update.
Sourcepub fn inline_query(&self) -> Option<&InlineQuery>
pub fn inline_query(&self) -> Option<&InlineQuery>
Returns the InlineQuery if this is an inline query update.
Sourcepub fn text(&self) -> Option<&str>
pub fn text(&self) -> Option<&str>
Returns the effective text of the message — Message::text if present,
falling back to Message::caption.
Sourcepub fn command(&self) -> Option<&str>
pub fn command(&self) -> Option<&str>
Returns the command name if the message starts with a bot command entity.
The leading / and optional @BotName suffix are stripped automatically.
/start@mybot returns Some("start").
Sourcepub fn is_ephemeral(&self) -> bool
pub fn is_ephemeral(&self) -> bool
Returns true if the current update’s message is an ephemeral message.
Sourcepub fn ephemeral_message_id(&self) -> Option<i64>
pub fn ephemeral_message_id(&self) -> Option<i64>
Returns the ephemeral message identifier, if the current update’s message is an ephemeral message.
Note that this is unrelated to Context::reply — replying to an
ephemeral message still requires calling BotClient::send_message
directly with .receiver_user_id(...) (and, within 15 seconds of the
triggering action, .callback_query_id(...) or
ReplyParameters::reply_to_ephemeral),
since reply()’s signature has no room for the extra targeting
parameters without a breaking change.