2020-03-04 14:15:23 +06:00
|
|
|
// This bot answers how many messages it received in total on every message.
|
|
|
|
|
2022-06-29 01:49:49 +06:00
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicU64, Ordering},
|
|
|
|
Arc,
|
|
|
|
};
|
2020-03-04 14:15:23 +06:00
|
|
|
|
2022-03-24 17:25:42 +06:00
|
|
|
use teloxide::prelude::*;
|
2020-03-04 14:15:23 +06:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2022-03-21 18:53:59 +04:00
|
|
|
pretty_env_logger::init();
|
2022-04-27 01:00:08 +06:00
|
|
|
log::info!("Starting shared state bot...");
|
2020-03-04 14:15:23 +06:00
|
|
|
|
2022-09-29 09:37:20 +06:00
|
|
|
let bot = Bot::from_env();
|
2022-06-29 01:49:49 +06:00
|
|
|
let messages_total = Arc::new(AtomicU64::new(0));
|
2020-03-04 14:15:23 +06:00
|
|
|
|
2022-06-29 01:49:49 +06:00
|
|
|
let handler = Update::filter_message().endpoint(
|
2022-10-03 17:54:06 +06:00
|
|
|
|bot: Bot, messages_total: Arc<AtomicU64>, msg: Message| async move {
|
2022-06-29 01:49:49 +06:00
|
|
|
let previous = messages_total.fetch_add(1, Ordering::Relaxed);
|
2022-04-03 16:06:44 +06:00
|
|
|
bot.send_message(msg.chat.id, format!("I received {previous} messages in total."))
|
2022-01-06 13:59:50 +02:00
|
|
|
.await?;
|
2022-01-26 15:51:51 +06:00
|
|
|
respond(())
|
|
|
|
},
|
2022-06-29 01:49:49 +06:00
|
|
|
);
|
2022-01-26 15:51:51 +06:00
|
|
|
|
2022-06-29 01:49:49 +06:00
|
|
|
Dispatcher::builder(bot, handler)
|
|
|
|
// Pass the shared state to the handler as a dependency.
|
|
|
|
.dependencies(dptree::deps![messages_total])
|
2022-07-21 12:36:57 +04:00
|
|
|
.enable_ctrlc_handler()
|
2022-06-29 01:49:49 +06:00
|
|
|
.build()
|
|
|
|
.dispatch()
|
|
|
|
.await;
|
2020-03-04 14:15:23 +06:00
|
|
|
}
|