[−][src]Module tbot::contexts
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 |
Audio | The context for |
ChosenInline | The context for |
Command | A wrapping context for commands. |
ConnectedWebsite | The context for |
Contact | The context for |
CreatedGroup | The context for |
DataCallback | Context for the |
DeletedChatPhoto | The context for |
Dice | The context for |
Document | The context for |
EditedAnimation | The context for |
EditedAudio | The context for |
EditedDocument | The context for |
EditedLocation | The context for |
EditedPhoto | The context for |
EditedText | The context for |
EditedVideo | The context for |
Game | The context for |
GameCallback | Context for the |
Inline | The context for |
Invoice | The context for |
LeftMember | The context for |
Location | The context for |
Migration | The context for |
NewChatPhoto | The context for |
NewChatTitle | The context for |
NewMembers | The context for |
Passport | The context for |
Payment | The context for |
Photo | The context for |
PinnedMessage | The context for |
Poll | The context for |
PollAnswer | The context for |
PreCheckout | The context for |
Shipping | The context for |
Sticker | The context for |
Text | The context for |
Unhandled | The context for |
Update | The context for |
UpdatedPoll | The context for |
Venue | The context for |
Video | The context for |
VideoNote | The context for |
Voice | The context for |