Module tbot::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 Message or Pinnable. These traits share common methods between contexts, e.g. send_message_in_reply you have seen above.

Modules

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

Structs

The context for animation handlers.
The context for any_update handlers.
The context for audio handlers.
The context for chat_member handlers.
The context for chosen_inline handlers.
The context for text handlers.
The context for connected_website handlers.
The context for contact handlers.
The context for created_group handlers.
The context for deleted_chat_photo handlers.
The context for dice handlers.
The context for document handlers.
The context for edited_animation handlers.
The context for edited_audio handlers.
The context for text handlers.
The context for edited_document handlers.
The context for edited_location handlers.
The context for edited_photo handlers.
The context for edited_text handlers.
The context for edited_video handlers.
The context for ended_voice_chat handlers.
The context for game handlers.
The context for inline handlers.
The context for invoice handlers.
The context for left_member handlers.
The context for location handlers.
The context for migration handlers.
The context for my_chat_member handlers.
The context for new_chat_photo handlers.
The context for new_chat_title handlers.
The context for new_members handlers.
The context for passport handlers.
The context for payment handlers.
The context for photo handlers.
The context for pinned_message handlers.
The context for poll handlers.
The context for poll_answer handlers.
The context for pre_checkout handlers.
The context for proximity_alert handlers.
The context for scheduled_voice_chat handlers.
The context for shipping handlers.
The context for started_voice_chat handlers.
The context for sticker handlers.
The context for text handlers.
The context for unhandled handlers.
The context for updated_poll handlers.
The context for venue handlers.
The context for video handlers.
The context for video_note handlers.
The context for voice handlers.