Merge pull request #665 from teloxide/update-shared-state-example

Use the new DI in `examples/shared_state.rs`
This commit is contained in:
Hirrolot 2022-06-29 22:38:15 +06:00 committed by GitHub
commit 3b0e3f3a0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 37 deletions

View file

@ -95,7 +95,6 @@ tower-http = { version = "0.2.5", features = ["trace"], optional = true }
[dev-dependencies]
rand = "0.8.3"
pretty_env_logger = "0.4.0"
once_cell = "1.9.0"
serde = "1"
serde_json = "1"
tokio = { version = "1.8", features = ["fs", "rt-multi-thread", "macros"] }

View file

@ -1,27 +1,34 @@
// This bot answers how many messages it received in total on every message.
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{
atomic::{AtomicU64, Ordering},
Arc,
};
use once_cell::sync::Lazy;
use teloxide::prelude::*;
static MESSAGES_TOTAL: Lazy<AtomicU64> = Lazy::new(AtomicU64::default);
#[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().branch(dptree::endpoint(
|msg: Message, bot: AutoSend<Bot>| async move {
let previous = MESSAGES_TOTAL.fetch_add(1, Ordering::Relaxed);
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).build().setup_ctrlc_handler().dispatch().await;
Dispatcher::builder(bot, handler)
// Pass the shared state to the handler as a dependency.
.dependencies(dptree::deps![messages_total])
.build()
.setup_ctrlc_handler()
.dispatch()
.await;
}

View file

@ -29,31 +29,7 @@
//! ([Full](https://github.com/teloxide/teloxide/blob/master/examples/shared_state.rs))
//!
//! ```no_run
//! use std::sync::atomic::{AtomicU64, Ordering};
//!
//! use once_cell::sync::Lazy;
//! use teloxide::prelude::*;
//!
//! static MESSAGES_TOTAL: Lazy<AtomicU64> = Lazy::new(AtomicU64::default);
//!
//! # #[tokio::main]
//! # async fn main() {
//! pretty_env_logger::init();
//! log::info!("Starting shared state bot...");
//!
//! let bot = Bot::from_env().auto_send();
//!
//! let handler = Update::filter_message().branch(dptree::endpoint(
//! |msg: Message, bot: AutoSend<Bot>| async move {
//! let previous = MESSAGES_TOTAL.fetch_add(1, Ordering::Relaxed);
//! bot.send_message(msg.chat.id, format!("I received {} messages in total.", previous))
//! .await?;
//! respond(())
//! },
//! ));
//!
//! Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await;
//! # }
//! // TODO: examples/purchase.rs
//! ```
//!
//! 1. First, we create the bot: `let bot = Bot::from_env().auto_send()`.
@ -61,8 +37,8 @@
//! kinds of [`crate::types::Update`], here we are only interested in
//! [`crate::types::Message`]: [`UpdateFilterExt::filter_message`] create a
//! handler object which filters all messages out of a generic update.
//! 3. By doing `.branch(dptree::endpoint(...))`, we set up a custom handling
//! closure that receives `msg: Message` and `bot: AutoSend<Bot>`. There are
//! 3. By doing `.endpoint(...)` we set up a custom handling closure that
//! receives `msg: Message` and `bot: AutoSend<Bot>`. There are
//! called dependencies: `msg` is supplied by
//! [`UpdateFilterExt::filter_message`], while `bot` is supplied by
//! [`Dispatcher`].