Struct tbot::bot::Builder

source ·
pub struct Builder(_);
Expand description

A builder for a Bot with advanced configuration.

Implementations§

Starts constructing a Bot with the provided token.

Starts constructing a Bot, extracting the token from the provided environment variable.

Panics

Panics if the variable couldn’t be found.

Configures a proxy through which all the request will go.

Configures the URI where the bot will make requests.

You only need to use this if you’re going to use a self-hosted Bot API server. The provided URI may be http or https, it also may contain a path (e.g. http://localhost/self-hosted-bot-api/), and tbot will append bot$TOKEN/$METHOD to it, in case the server is behind a reverse proxy. The URI may also contain a query (e.g. https://localhost/?foo), in which case tbot will move it after the bot$TOKEN/$METHOD part. For example:

The provided URIA URI generated by tbot
http://localhosthttp://localhost/bot$TOKEN/$METHOD
http://localhost/foohttp://localhost/foo/bot$TOKEN/$METHOD
http://localhost/?foohttp://localhost/bot$TOKEN/$METHOD?foo

Note that tbot itself does not use the query part. tbot allows you to set it just in case your self-hosted Bot API server is behind a reverse proxy and you need to set the query for some reason. In this case, the query part is supposed to be removed when it gets to the Bot API server.

Do not forget to call log_out when you’re moving from the cloud Bot API server, or close when you’re moving from one self-hosted server to another. This method calls neither method and assumes that you’ve already migrated to the server you’re configuring.

Example

Say that you’ve started your local Bot API server on http://localhost:8081, and this is the first time you configure your bot to use your Bot API server. First, you need to call log_out, and only then you call server_uri:

use tbot::bot;

let bot = bot::Builder::with_env_token("BOT_TOKEN")
    .log_out().await? // log out from cloud Bot API first
    .server_uri("http://localhost:8081".parse()?)
    .build();

Now tbot will make requests to your Bot API server. You only need to call log_out once (unless you use the cloud Bot API server again), so after you did it once, you can remove that line:

use tbot::bot;

let bot = bot::Builder::with_env_token("BOT_TOKEN")
    .server_uri("http://localhost:8081".parse()?)
    .build();

If you’re moving from one local server to another, you’re going to call this method twice:

use tbot::bot;

let bot = bot::Builder::with_env_token("BOT_TOKEN")
    .server_uri("http://other-server:8081".parse()?)
    .close().await? // close the bot on the old server first
    .server_uri("http://localhost:8081".parse()?)
    .build();

Just like with logging out from the cloud Bot API server, you only have to do it once, and after that you can leave only the last server_uri call. If you use webhooks, do not forget to call delete_webhook before calling close to ensure that your bot isn’t launched on the old server when it restarts.

Logs out from the cloud Bot API server.

Note that after calling this method you must change the URI where tbot makes requests to your local Bot API server using server_uri. Once you log out, you cannot log back in the cloud server for 10 minutes.

In case of an error, a tuple of (errors::MethodCall, Self) is returned in case you expect an error and can recover from it.

Logs out from a self-hosted Bot API server.

Note that after calling this method you must change the URI where tbot makes requests to your local Bot API server using server_uri. Once you log out, you cannot log back in this server for 10 minutes. You may also need to call delete_webhook before calling this method to ensure that the bot isn’t launched on the old server if it restarts.

In case of an error, a tuple of (errors::MethodCall, Self) is returned in case you expect an error and can recover from it.

Deletes the bot’s webhook.

Before you close your bot on a self-hosted server, you should delete the bot’s webhook to ensure that, when that server is restarted, your bot isn’t laucnhed there.

You might want to call this method if you’re switching from webhooks to polling. Though this method can be used this way, this isn’t the intended usecase: starting a polling event loop already does it automatically before the first call to getUpdates.

In case of an error, a tuple of (errors::MethodCall, Self) is returned in case you expect an error and can recover from it.

Finishes constructing the Bot.

Trait Implementations§

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 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