mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-23 15:01:45 +01:00
dd4af30727
This helps with consistency -- every setting is changed in builder. Also `Self -> Self` function sometimes plays more nicely with borrowck.
34 lines
1,003 B
Rust
34 lines
1,003 B
Rust
// This bot answers how many messages it received in total on every message.
|
|
|
|
use std::sync::{
|
|
atomic::{AtomicU64, Ordering},
|
|
Arc,
|
|
};
|
|
|
|
use teloxide::prelude::*;
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
pretty_env_logger::init();
|
|
log::info!("Starting shared state bot...");
|
|
|
|
let bot = Bot::from_env().auto_send();
|
|
let messages_total = Arc::new(AtomicU64::new(0));
|
|
|
|
let handler = Update::filter_message().endpoint(
|
|
|msg: Message, bot: AutoSend<Bot>, messages_total: Arc<AtomicU64>| async move {
|
|
let previous = messages_total.fetch_add(1, Ordering::Relaxed);
|
|
bot.send_message(msg.chat.id, format!("I received {previous} messages in total."))
|
|
.await?;
|
|
respond(())
|
|
},
|
|
);
|
|
|
|
Dispatcher::builder(bot, handler)
|
|
// Pass the shared state to the handler as a dependency.
|
|
.dependencies(dptree::deps![messages_total])
|
|
.enable_ctrlc_handler()
|
|
.build()
|
|
.dispatch()
|
|
.await;
|
|
}
|