mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-23 23:29:37 +01:00
Add Bot::{from_env, from_env_with_client}
This commit is contained in:
parent
ad0e241195
commit
690fdd06b4
4 changed files with 40 additions and 3 deletions
|
@ -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 {
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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>,
|
||||
|
|
Loading…
Add table
Reference in a new issue