Add Bot::{from_env, from_env_with_client}

This commit is contained in:
Temirkhan Myrzamadi 2020-02-13 18:36:12 +06:00
parent ad0e241195
commit 690fdd06b4
4 changed files with 40 additions and 3 deletions

View file

@ -181,7 +181,7 @@ async fn run() {
pretty_env_logger::init();
log::info!("Starting dialogue_bot!");
let bot = Bot::new(std::env::var("TELOXIDE_TOKEN").unwrap());
let bot = Bot::from_env();
Dispatcher::new(bot)
.message_handler(&DialogueDispatcher::new(|ctx| async move {

View file

@ -11,7 +11,7 @@ async fn run() {
pretty_env_logger::init();
log::info!("Starting multiple_handlers_bot!");
let bot = Bot::new(std::env::var("TELOXIDE_TOKEN").unwrap());
let bot = Bot::from_env();
// Create a dispatcher with multiple handlers of different types. This will
// print One! and Two! on every incoming UpdateKind::Message.

View file

@ -11,7 +11,7 @@ async fn run() {
pretty_env_logger::init();
log::info!("Starting ping_pong_bot!");
let bot = Bot::new(std::env::var("TELOXIDE_TOKEN").unwrap());
let bot = Bot::from_env();
// Create a dispatcher with a single message handler that answers "pong" to
// each incoming message.

View file

@ -12,6 +12,39 @@ pub struct Bot {
}
impl Bot {
/// Creates a new `Bot` with the `TELOXIDE_TOKEN` environmental variable (a
/// bot's token) and the default [`reqwest::Client`].
///
/// # Panics
/// If cannot get the `TELOXIDE_TOKEN` environmental variable.
///
/// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html
pub fn from_env() -> Arc<Self> {
Self::new(
std::env::var("TELOXIDE_TOKEN")
.expect("Cannot get the TELOXIDE_TOKEN env variable"),
)
}
/// Creates a new `Bot` with the `TELOXIDE_TOKEN` environmental variable (a
/// bot's token) and your [`reqwest::Client`].
///
/// # Panics
/// If cannot get the `TELOXIDE_TOKEN` environmental variable.
///
/// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html
pub fn from_env_with_client(client: Client) -> Arc<Self> {
Self::with_client(
std::env::var("TELOXIDE_TOKEN")
.expect("Cannot get the TELOXIDE_TOKEN env variable"),
client,
)
}
/// Creates a new `Bot` with the specified token and the default
/// [`reqwest::Client`].
///
/// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html
pub fn new<S>(token: S) -> Arc<Self>
where
S: Into<String>,
@ -19,6 +52,10 @@ impl Bot {
Self::with_client(token, Client::new())
}
/// Creates a new `Bot` with the specified token and your
/// [`reqwest::Client`].
///
/// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html
pub fn with_client<S>(token: S, client: Client) -> Arc<Self>
where
S: Into<String>,