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. - Chosen
Inline - The context for
chosen_inline
handlers. - Command
- A wrapping context for commands.
- Connected
Website - The context for
connected_website
handlers. - Contact
- The context for
contact
handlers. - Created
Group - The context for
created_group
handlers. - Data
Callback - Context for the
data_callback
handler. - Deleted
Chat Photo - The context for
deleted_chat_photo
handlers. - Dice
- The context for
dice
handlers. - Document
- The context for
document
handlers. - Edited
Animation - The context for
edited_animation
handlers. - Edited
Audio - The context for
edited_audio
handlers. - Edited
Document - The context for
edited_document
handlers. - Edited
Location - The context for
edited_location
handlers. - Edited
Photo - The context for
edited_photo
handlers. - Edited
Text - The context for
edited_text
handlers. - Edited
Video - The context for
edited_video
handlers. - Game
- The context for
game
handlers. - Game
Callback - Context for the
game_callback
handler. - Inline
- The context for
inline
handlers. - Invoice
- The context for
invoice
handlers. - Left
Member - The context for
left_member
handlers. - Location
- The context for
location
handlers. - Migration
- The context for
migration
handlers. - NewChat
Photo - The context for
new_chat_photo
handlers. - NewChat
Title - 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. - Pinned
Message - The context for
pinned_message
handlers. - Poll
- The context for
poll
handlers. - Poll
Answer - 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
andafter_update
handlers. - Updated
Poll - The context for
updated_poll
handlers. - Venue
- The context for
venue
handlers. - Video
- The context for
video
handlers. - Video
Note - The context for
video_note
handlers. - Voice
- The context for
voice
handlers.