teloxide/crates/teloxide-core/examples/erased.rs

43 lines
1.6 KiB
Rust
Raw Normal View History

2021-07-12 15:58:51 +02:00
use std::{env::VarError, time::Duration};
use teloxide_core::{adaptors::trace, prelude::*, types::ChatAction};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
pretty_env_logger::init();
2022-11-07 13:13:29 +01:00
let chat_id =
ChatId(std::env::var("CHAT_ID").expect("Expected CHAT_ID env var").parse::<i64>()?);
2021-07-12 15:58:51 +02:00
let trace_settings = match std::env::var("TRACE").as_deref() {
Ok("EVERYTHING_VERBOSE") => trace::Settings::TRACE_EVERYTHING_VERBOSE,
Ok("EVERYTHING") => trace::Settings::TRACE_EVERYTHING,
Ok("REQUESTS_VERBOSE") => trace::Settings::TRACE_REQUESTS_VERBOSE,
Ok("REQUESTS") => trace::Settings::TRACE_REQUESTS,
Ok("RESPONSES_VERBOSE") => trace::Settings::TRACE_RESPONSES_VERBOSE,
Ok("RESPONSES") => trace::Settings::TRACE_RESPONSES,
Ok("EMPTY") | Ok("") | Err(VarError::NotPresent) => trace::Settings::empty(),
Ok(_) | Err(VarError::NotUnicode(_)) => {
panic!(
"Expected `TRACE` environment variable to be equal to any of the following: \
`EVERYTHING_VERBOSE`, `EVERYTHING`, `REQUESTS_VERBOSE`, `REQUESTS`, \
`RESPONSES_VERBOSE`, `RESPONSES`, `EMPTY`, `` (empty string)"
)
}
};
log::info!("Trace settings: {:?}", trace_settings);
let bot = if trace_settings.is_empty() {
2022-09-23 15:48:26 +02:00
Bot::from_env().erase()
2021-07-12 15:58:51 +02:00
} else {
2022-09-23 15:48:26 +02:00
Bot::from_env().trace(trace_settings).erase()
2021-07-12 15:58:51 +02:00
};
bot.send_chat_action(chat_id, ChatAction::Typing).await?;
tokio::time::sleep(Duration::from_secs(1)).await;
bot.send_message(chat_id, "Hey hey hey").await?;
Ok(())
}