Skip to main content

CallbackContext

Struct CallbackContext 

Source
pub struct CallbackContext {
    pub matches: Option<Vec<String>>,
    pub named_matches: Option<HashMap<String, String>>,
    pub args: Option<Vec<String>>,
    pub error: Option<Arc<dyn Error + Sync + Send>>,
    /* private fields */
}
Expand description

A context object passed to handler callbacks.

Fields§

§matches: Option<Vec<String>>

Positional regex match results (populated by regex-based handlers).

§named_matches: Option<HashMap<String, String>>

Named regex capture groups (populated by regex-based handlers when the pattern contains at least one named group).

Mirrors Python’s context.matches which exposes the full re.Match object including match.groupdict().

§args: Option<Vec<String>>

Arguments to a command (populated by CommandHandler).

§error: Option<Arc<dyn Error + Sync + Send>>

The error that was raised. Only present in error handler contexts.

Implementations§

Source§

impl CallbackContext

Source

pub fn new( bot: Arc<ExtBot>, chat_id: Option<i64>, user_id: Option<i64>, user_data_store: Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>, chat_data_store: Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>, bot_data: Arc<RwLock<HashMap<String, Value>>>, ) -> CallbackContext

Creates a new CallbackContext.

Source

pub fn from_update( update: &Update, bot: Arc<ExtBot>, user_data_store: Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>, chat_data_store: Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>, bot_data: Arc<RwLock<HashMap<String, Value>>>, ) -> CallbackContext

Constructs a context from a typed Update.

Source

pub fn from_error( update: Option<&Update>, error: Arc<dyn Error + Sync + Send>, bot: Arc<ExtBot>, user_data_store: Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>, chat_data_store: Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>, bot_data: Arc<RwLock<HashMap<String, Value>>>, ) -> CallbackContext

Constructs a context for an error handler.

Source

pub fn bot(&self) -> &Arc<ExtBot>

Returns a reference to the bot associated with this context.

Source

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

Returns the chat ID extracted from the update, if available.

Source

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

Returns the user ID extracted from the update, if available.

Source

pub async fn bot_data(&self) -> DataReadGuard<'_>

Acquire a read lock on the bot-wide data store, returning a typed guard.

Source

pub async fn bot_data_mut(&self) -> DataWriteGuard<'_>

Acquire a write lock on the bot-wide data store, returning a typed guard.

Source

pub async fn user_data(&self) -> Option<HashMap<String, Value>>

Returns a cloned snapshot of the current user’s data, if a user ID is set.

Source

pub async fn chat_data(&self) -> Option<HashMap<String, Value>>

Returns a cloned snapshot of the current chat’s data, if a chat ID is set.

Source

pub async fn set_user_data(&self, key: String, value: Value) -> bool

Insert a key-value pair into the current user’s data store. Returns false if no user ID.

Source

pub async fn set_chat_data(&self, key: String, value: Value) -> bool

Insert a key-value pair into the current chat’s data store. Returns false if no chat ID.

Source

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

Returns the first positional regex match, if available.

Source

pub fn extra(&self) -> Option<&HashMap<String, Value>>

Returns a reference to the extra data map, if any data has been set.

Source

pub fn extra_mut(&mut self) -> &mut HashMap<String, Value>

Returns a mutable reference to the extra data map, creating it if needed.

Source

pub fn set_extra(&mut self, key: String, value: Value)

Insert a key-value pair into the extra data map.

Source

pub fn get_extra(&self, key: &str) -> Option<&Value>

Get a value from the extra data map by key.

Source

pub async fn drop_callback_data( &self, callback_query_id: &str, ) -> Result<(), InvalidCallbackData>

Drop the cached callback data for a given callback query ID.

Source

pub async fn reply_text( &self, update: &Update, text: &str, ) -> Result<Message, TelegramError>

Send a text reply to the chat associated with the given update.

This is a convenience method that mirrors python-telegram-bot’s update.message.reply_text(text) / context.bot.send_message(...).

§Errors

Returns TelegramError if the chat cannot be determined from the update or if the Telegram API call fails.

Source

pub async fn reply_html( &self, update: &Update, text: &str, ) -> Result<Message, TelegramError>

Send an HTML-formatted text reply to the chat associated with the given update.

Equivalent to reply_text with parse_mode("HTML").

§Errors

Returns TelegramError if the chat cannot be determined from the update or if the Telegram API call fails.

Source

pub async fn reply_markdown_v2( &self, update: &Update, text: &str, ) -> Result<Message, TelegramError>

Send a MarkdownV2-formatted text reply to the chat associated with the given update.

Equivalent to reply_text with parse_mode("MarkdownV2").

§Errors

Returns TelegramError if the chat cannot be determined from the update or if the Telegram API call fails.

Source

pub async fn reply_photo( &self, update: &Update, photo: InputFile, ) -> Result<Message, TelegramError>

Send a photo reply to the chat associated with the given update.

§Errors

Returns TelegramError if the chat cannot be determined from the update or if the Telegram API call fails.

Source

pub async fn reply_document( &self, update: &Update, document: InputFile, ) -> Result<Message, TelegramError>

Send a document reply to the chat associated with the given update.

§Errors

Returns TelegramError if the chat cannot be determined from the update or if the Telegram API call fails.

Source

pub async fn reply_sticker( &self, update: &Update, sticker: InputFile, ) -> Result<Message, TelegramError>

Send a sticker reply to the chat associated with the given update.

§Errors

Returns TelegramError if the chat cannot be determined from the update or if the Telegram API call fails.

Source

pub async fn reply_location( &self, update: &Update, latitude: f64, longitude: f64, ) -> Result<Message, TelegramError>

Send a location reply to the chat associated with the given update.

§Errors

Returns TelegramError if the chat cannot be determined from the update or if the Telegram API call fails.

Source

pub async fn answer_callback_query( &self, update: &Update, ) -> Result<bool, TelegramError>

Answer the callback query from the given update.

Automatically extracts the callback query ID from the update. This is a convenience shortcut that eliminates the common boilerplate of extracting update.callback_query.id manually.

§Errors

Returns TelegramError if the update does not contain a callback query or if the Telegram API call fails.

Source

pub async fn edit_callback_message_text( &self, update: &Update, text: &str, ) -> Result<MessageOrBool, TelegramError>

Edit the text of the message that originated the callback query.

Automatically determines whether to use chat_id + message_id or inline_message_id based on the callback query contents.

§Errors

Returns TelegramError if the update does not contain a callback query, the callback query has no associated message, or the Telegram API call fails.

Trait Implementations§

Source§

impl Clone for CallbackContext

Source§

fn clone(&self) -> CallbackContext

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for CallbackContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. 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> 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: 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: 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