Struct tbot::bot::Bot

source ·
pub struct Bot { /* private fields */ }
Expand description

A Bot is the entry point to interacting with the Bot API.

Using a Bot, you can call methods from the methods module:

let bot = tbot::from_env!("BOT_TOKEN");
let me = bot.get_me().call().await?;
dbg!(me);

A Bot is also used to construct an EventLoop — a struct responsible for configuring handlers and listening to updates:

use tbot::prelude::*;

let mut bot = tbot::from_env!("BOT_TOKEN").event_loop();

bot.start(|context| async move {
   let result = context.send_message_in_reply("Hi!").call().await;

    if let Err(error) = result {
        eprintln!("Failed to send a message: {}", error);
    }
});

bot.polling().start().await?;

A Bot can be constructed in several ways:

  • The from_env! macro constructs a bot extracting its token from an enviroment variable at compile time;
  • The Bot::from_env method constructs a bot extracting its token from an environment variable at runtime;
  • The Bot::new method constructs a bot, and its token is provided as a parameter;
  • The bot::Builder provides advanced configuration of a bot. You can set up an HTTPS/SOCKS5 proxy or your local Bot API server’s URL using it.

The bot’s internal data (its token, proxy and the URL where it makes requests) is kept behind an Arc. It means that you can clone a Bot cheaply to share it between tasks.

Implementations§

Constructs a new Bot.

This method is a shorthand for a common case. If you need advanced configuration, e.g. you want to set a proxy or use a local Bot API server, construct a Bot using a bot::Builder.

Constructs a new Bot, extracting the token from the environment at runtime.

If you need to extract the token at compile time, use from_env!.

This method is a shorthand for a common case. If you need advanced configuration, e.g. you want to set a proxy or use a local Bot API server, construct a Bot using a bot::Builder.

Example
use tbot::Bot;

let bot = Bot::from_env("BOT_TOKEN");
let me = bot.get_me().call().await.unwrap();
dbg!(me);

Downloads a file.

If you use a self-hosted Bot API server, file.path may be an absolute local path. In this case, tbot reads the file at the given and returns it. If the server is running on another machine, you have to handle this case manually before calling this method.

Constructs an EventLoop.

Constructs a stateful event loop.

Adds a new sticker to an existing sticker set.

Copies a message.

Creates a secondary invite link for a chat.

Creates a new sticker set.

Deletes a chat’s photo.

Deletes a chat’s sticker set.

Deletes a message from a chat.

Deletes the list of the bot’s commands.

Deletes a sticker from a sticker set.

Edits a secondary invite link for a chat.

Edits the caption of a media message sent via the inline mode.

Edits a live location sent via the inline mode.

Edits the media of a message sent via the inline mode.

Edits the inline keyboard of a message sent via the inline mode.

Edits the text of a message sent via the inline mode.

Edits the caption of a media message sent by the bot itself.

Edits a live location sent by the bot itself.

Edits a live location sent by the bot itself.

Edits the inline keyboard of a message sent by the bot itself.

Edits the text of a message sent by the bot itself.

Exports a chat’s invite link.

Forwards a message.

Gets information about a chat.

Gets information about a file.

Gets an excerpt from the high score table of a game sent via the inline mode.

Gets information about a chat’s admins.

Gets information about a chat’s member.

Gets a chat’s member count.

Gets an excerpt from the high score table of a game sent by the bot itself.

Gets information about the bot.

Gets the list of the bot’s commands.

Gets a sticker set by its name.

Gets a user’s profile photos.

Gets information about the bot’s webhook.

Bans a member in a chat.

Leaves a chat.

Pins a message in a chat.

Promotes a chat member to an admin.

Restricts a chat member.

Revokes an invite link for a chat.

Sends an animation.

Sends an audio.

Sends a chat action.

Sends a contact.

Sends a game.

Sends a dice.

Sends a document.

Sends an invoice.

Sends a location.

Sends an album.

Sends a text message.

Sends a photo.

Sends a poll.

Sends a sticker.

Sends a venue.

Sends a video note.

Sends a video.

Sends a voice.

Sets a custom title for an admin in a chat.

Sets a chat’s description.

Sets a group’s global permissions.

Sets a chat’s photo.

Sets a group’s sticker set.

Sets a group’s title.

Sets a user’s new high score in a game sent via the inline mode.

Sets a user’s new high score in a game sent by the bot itself.

Sets the list of the bot’s commands.

Reports passport errors to the user.

Changes a sticker’s position in a sticker set.

Sets the thumb of a sticker set.

Stops a live location sent via the inline mode.

Stops a live location sent by the bot itself.

Stops a poll.

Lifts all restrictions from a group’s member.

Unpins all messages in a chat.

Unpins a chat message.

Uploads a sticker file.

Trait Implementations§

Sends a chat action in an infinite loop, returning only if failed to send the action. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more