pub struct Builder(_);
Expand description
A builder for a Bot
with advanced configuration.
Implementations§
source§impl Builder
impl Builder
sourcepub fn with_string_token(token: String) -> Self
pub fn with_string_token(token: String) -> Self
Starts constructing a Bot
with the provided token.
sourcepub fn with_env_token(env_var: &'static str) -> Self
pub fn with_env_token(env_var: &'static str) -> Self
Starts constructing a Bot
, extracting the token from the provided
environment variable.
Panics
Panics if the variable couldn’t be found.
sourcepub fn proxy(self, proxy: impl Into<Proxy>) -> Self
pub fn proxy(self, proxy: impl Into<Proxy>) -> Self
Configures a proxy through which all the request will go.
sourcepub fn server_uri(self, uri: Uri) -> Self
pub fn server_uri(self, uri: Uri) -> Self
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 URI | A URI generated by tbot |
---|---|
http://localhost | http://localhost/bot$TOKEN/$METHOD |
http://localhost/foo | http://localhost/foo/bot$TOKEN/$METHOD |
http://localhost/?foo | http://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.
sourcepub async fn log_out(self) -> Result<Self, (MethodCall, Self)>
pub async fn log_out(self) -> Result<Self, (MethodCall, Self)>
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.
sourcepub async fn close(self) -> Result<Self, (MethodCall, Self)>
pub async fn close(self) -> Result<Self, (MethodCall, Self)>
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.
sourcepub async fn delete_webhook(self) -> Result<Self, (MethodCall, Self)>
pub async fn delete_webhook(self) -> Result<Self, (MethodCall, Self)>
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.