[−][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::bot!("BOT_TOKEN").event_loop(); bot.text(|context| { let reversed: String = context.text.value.chars().rev().collect(); let reply = context .send_message_in_reply(&reversed) .into_future() .map_err(|err| { dbg!(err); }); tbot::spawn(reply); });
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 the 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 API method you can call using a Bot
:
use tbot::types::chat; const ADMIN_CHAT: chat::Id = chat::Id(0); bot.text(|context| { let notification = context .bot .send_message(ADMIN_CHAT, "New message!") .into_future() .map_err(|err| { dbg!(err); }); tbot::spawn(notification); });
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
traits | Contains traits for updates that infer some data from them, simplifying calling several methods. |
Structs
Animation | The context for |
Audio | The context for |
ChosenInline | The context for |
ConnectedWebsite | The context for |
Contact | The context for |
CreatedGroup | The context for |
DataCallback | Context for the |
DeletedChatPhoto | 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 |
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 |