Struct tbot::bot::Builder[][src]

#[must_use]
pub struct Builder(_);

A builder for a Bot with advanced configuration.

Implementations

impl Builder[src]

pub fn with_string_token(token: String) -> Self[src]

Starts constructing a Bot with the provided token.

pub fn with_env_token(env_var: &'static str) -> Self[src]

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

Panics

Panics if the variable couldn’t be found.

pub fn proxy(self, proxy: impl Into<Proxy>) -> Self[src]

Configures a proxy through which all the request will go.

pub fn server_uri(self, uri: Uri) -> Self[src]

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.

pub async fn log_out(self) -> Result<Self, (MethodCall, Self)>[src]

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.

pub async fn close(self) -> Result<Self, (MethodCall, Self)>[src]

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.

pub async fn delete_webhook(self) -> Result<Self, (MethodCall, Self)>[src]

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.

pub fn build(self) -> Bot[src]

Finishes constructing the Bot.

Trait Implementations

impl Debug for Builder[src]

Auto Trait Implementations

impl !RefUnwindSafe for Builder

impl Send for Builder

impl Sync for Builder

impl Unpin for Builder

impl !UnwindSafe for Builder

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.