Module contexts

Source
Expand description

Contexts for update handlers.

A context is a struct that is passed to update handlers, contains data about the update, and provides methods that infer certain data from the update. For example:

use tbot::prelude::*;

let mut bot = tbot::from_env!("BOT_TOKEN").event_loop();

bot.text(|context| async move {
    let reversed: String = context.text.value.chars().rev().collect();
    context.send_message_in_reply(&reversed).call().await.unwrap();
});

Here, we set a text handler for our bot. Whenever we get a text message, the handler is called with a reference to a Text context that contains data about the incoming data, e.g. the text of the message. Then we call the send_message_in_reply method on the context, which does what its name says: sends a message in the same chat in reply to the incoming message, inferring your bot’s token and IDs of the chat and the message.

All contexts have one common field named bot. Through this field, you can call any method using a Bot:

use tbot::types::chat;
const ADMIN_CHAT: chat::Id = chat::Id(0);

bot.text(|context| async move {
    context
        .bot
        .send_message(ADMIN_CHAT, "New message!")
        .call()
        .await
        .unwrap();
});

Most contexts implement certain traits, such as ChatMethods or Pinnable. These traits share common methods between contexts, e.g. send_message_in_reply you have seen above.

Modules§

fields
Traits for common context fields.
methods
Traits for calling methods inferring as much data as possible from the context.

Structs§

Animation
The context for animation handlers.
Audio
The context for audio handlers.
ChosenInline
The context for chosen_inline handlers.
Command
A wrapping context for commands.
ConnectedWebsite
The context for connected_website handlers.
Contact
The context for contact handlers.
CreatedGroup
The context for created_group handlers.
DataCallback
Context for the data_callback handler.
DeletedChatPhoto
The context for deleted_chat_photo handlers.
Dice
The context for dice handlers.
Document
The context for document handlers.
EditedAnimation
The context for edited_animation handlers.
EditedAudio
The context for edited_audio handlers.
EditedDocument
The context for edited_document handlers.
EditedLocation
The context for edited_location handlers.
EditedPhoto
The context for edited_photo handlers.
EditedText
The context for edited_text handlers.
EditedVideo
The context for edited_video handlers.
Game
The context for game handlers.
GameCallback
Context for the game_callback handler.
Inline
The context for inline handlers.
Invoice
The context for invoice handlers.
LeftMember
The context for left_member handlers.
Location
The context for location handlers.
Migration
The context for migration handlers.
NewChatPhoto
The context for new_chat_photo handlers.
NewChatTitle
The context for new_chat_title handlers.
NewMembers
The context for new_members handlers.
Passport
The context for passport handlers.
Payment
The context for payment handlers.
Photo
The context for photo handlers.
PinnedMessage
The context for pinned_message handlers.
Poll
The context for poll handlers.
PollAnswer
The context for poll_answer handlers.
PreCheckout
The context for pre_checkout handlers.
Shipping
The context for shipping handlers.
Sticker
The context for sticker handlers.
Text
The context for text handlers.
Unhandled
The context for unhandled handlers.
Update
The context for before_update and after_update handlers.
UpdatedPoll
The context for updated_poll handlers.
Venue
The context for venue handlers.
Video
The context for video handlers.
VideoNote
The context for video_note handlers.
Voice
The context for voice handlers.