Skip to main content

Context

Struct Context 

Source
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: BotClient

The API client — call any Bot API method directly.

Implementations§

Source§

impl Context

Source

pub fn new(update: Update, bot: BotClient) -> Self

Creates a new Context.

Source

pub fn update_id(&self) -> i64

Returns the update ID.

Source

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.

Source

pub fn chat_id(&self) -> Option<ChatId>

Returns the chat ID from the current update as a ChatId, if available.

Source

pub fn from_id(&self) -> Option<i64>

Returns the sender’s user ID, if available.

Source

pub fn callback_query(&self) -> Option<&CallbackQuery>

Returns the CallbackQuery if this is a callback query update.

Source

pub fn inline_query(&self) -> Option<&InlineQuery>

Returns the InlineQuery if this is an inline query update.

Source

pub fn text(&self) -> Option<&str>

Returns the effective text of the message — Message::text if present, falling back to Message::caption.

Source

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").

Source

pub fn is_ephemeral(&self) -> bool

Returns true if the current update’s message is an ephemeral message.

Source

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.

Source

pub fn reply(&self, text: impl Into<String>) -> Option<SendMessage>

Sends a text reply to the current chat, automatically setting reply_to_message_id to the incoming message.

Returns None when the update has no associated chat.

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more