teloxide/examples/shared_state.rs
Hirrolot 53be9d0e92 Reorder parameters in the examples
In reordering the parameters, I stick the following principle: place parameters from least changing to most changing. Thus, we have config and bot right from the beginning, next a dialogue with a possible payload, and next updates such as messages, inline queries, etc. This principle is used in languages with a native support for currying, although in Rust people appear to order parameters arbitrarily, so this commit is mostly for the sake of consistency.


Former-commit-id: 5922984f6c
2022-10-03 17:54:06 +06:00

34 lines
981 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();
let messages_total = Arc::new(AtomicU64::new(0));
let handler = Update::filter_message().endpoint(
|bot: Bot, messages_total: Arc<AtomicU64>, msg: Message| 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;
}