teloxide/examples/shared_state_bot/src/main.rs

34 lines
922 B
Rust
Raw Normal View History

2020-03-04 09:15:23 +01:00
// This bot answers how many messages it received in total on every message.
use std::sync::atomic::{AtomicU64, Ordering};
use lazy_static::lazy_static;
use teloxide::prelude2::*;
2020-03-04 09:15:23 +01:00
lazy_static! {
static ref MESSAGES_TOTAL: AtomicU64 = AtomicU64::new(0);
}
#[tokio::main]
async fn main() {
teloxide::enable_logging!();
log::info!("Starting shared_state_bot...");
2020-03-04 09:15:23 +01:00
2021-03-05 22:24:10 +01:00
let bot = Bot::from_env().auto_send();
2020-03-04 09:15:23 +01:00
Dispatcher::new(bot)
2022-01-06 12:59:50 +01:00
.messages_handler(|h| {
h.branch(dptree::endpoint(|mes: Message, bot: AutoSend<Bot>| async move {
2020-03-04 09:15:23 +01:00
let previous = MESSAGES_TOTAL.fetch_add(1, Ordering::Relaxed);
2022-01-06 12:59:50 +01:00
bot.send_message(
mes.chat.id,
format!("I received {} messages in total.", previous),
)
.await?;
respond(())
}))
2020-03-04 09:15:23 +01:00
})
.dispatch()
.await;
}