Module tbot::contexts[][src]

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

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.

AnyUpdate

The context for any_update handlers.

Audio

The context for audio handlers.

ChangedAutoDeleteTimer

The context for changed_auto_delete_timer handlers.

ChatMember

The context for chat_member handlers.

ChosenInline

The context for chosen_inline handlers.

Command

The context for text handlers.

ConnectedWebsite

The context for connected_website handlers.

Contact

The context for contact handlers.

CreatedGroup

The context for created_group handlers.

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.

EditedCommand

The context for text 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.

EndedVoiceChat

The context for ended_voice_chat handlers.

Game

The context for game handlers.

Inline

The context for inline handlers.

InlineDataCallback

Context for the inline_data_callback handler.

InlineGameCallback

Context for the inline_game_callback handler.

InvitedVoiceChatParticipants

The context for invited_voice_chat_participants handlers.

Invoice

The context for invoice handlers.

LeftMember

The context for left_member handlers.

Location

The context for location handlers.

MessageDataCallback

Context for the message_data_callback handler.

MessageGameCallback

Context for the message_game_callback handler.

Migration

The context for migration handlers.

MyChatMember

The context for my_chat_member 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.

ProximityAlert

The context for proximity_alert handlers.

ScheduledVoiceChat

The context for scheduled_voice_chat handlers.

Shipping

The context for shipping handlers.

StartedVoiceChat

The context for started_voice_chat handlers.

Sticker

The context for sticker handlers.

Text

The context for text handlers.

Unhandled

The context for unhandled 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.