From 2374b5c2d3ef50abcb50cf1faa0d33cb31d8909a Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Wed, 4 Mar 2020 14:15:23 +0600 Subject: [PATCH] Add examples/shared_state_bot --- .gitignore | 3 +- examples/README.md | 2 +- examples/shared_state_bot/Cargo.toml | 14 +++++++++ examples/shared_state_bot/src/main.rs | 41 +++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 examples/shared_state_bot/Cargo.toml create mode 100644 examples/shared_state_bot/src/main.rs diff --git a/.gitignore b/.gitignore index f2d88a0e..be1fcf95 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ examples/dialogue_bot/target examples/multiple_handlers_bot/target examples/admin_bot/target examples/guess_a_number_bot/target -examples/simple_commands_bot/target \ No newline at end of file +examples/simple_commands_bot/target +examples/shared_state_bot/target \ No newline at end of file diff --git a/examples/README.md b/examples/README.md index 69703040..ae4a8990 100644 --- a/examples/README.md +++ b/examples/README.md @@ -7,4 +7,4 @@ Just enter the directory (for example, `cd dialogue_bot`) and execute `cargo run | [guess_a_number_bot](guess_a_number_bot) | The "guess a number" game. | | [dialogue_bot](dialogue_bot) | Drive a dialogue with a user using a type-safe finite automaton. | | [admin_bot](admin_bot) | A bot, which can ban, kick, and mute on a command. | - +| [shared_state_bot](shared_state_bot) | A bot that shows how to deal with shared state. | \ No newline at end of file diff --git a/examples/shared_state_bot/Cargo.toml b/examples/shared_state_bot/Cargo.toml new file mode 100644 index 00000000..136c2ec0 --- /dev/null +++ b/examples/shared_state_bot/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "shared_state_bot" +version = "0.1.0" +authors = ["Temirkhan Myrzamadi "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +log = "0.4.8" +tokio = "0.2.9" +pretty_env_logger = "0.4.0" +lazy_static = "1.4.0" +teloxide = { path = "../../" } \ No newline at end of file diff --git a/examples/shared_state_bot/src/main.rs b/examples/shared_state_bot/src/main.rs new file mode 100644 index 00000000..a066849b --- /dev/null +++ b/examples/shared_state_bot/src/main.rs @@ -0,0 +1,41 @@ +// 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::prelude::*; + +lazy_static! { + static ref MESSAGES_TOTAL: AtomicU64 = AtomicU64::new(0); +} + +#[tokio::main] +async fn main() { + run().await; +} + +async fn run() { + teloxide::enable_logging!(); + log::info!("Starting shared_state_bot!"); + + let bot = Bot::from_env(); + + Dispatcher::new(bot) + .messages_handler(|rx: DispatcherHandlerRx| { + rx.for_each_concurrent(None, |message| async move { + let previous = MESSAGES_TOTAL.fetch_add(1, Ordering::Relaxed); + + message + .answer(format!( + "I received {} messages in total.", + previous + )) + .send() + .await + .log_on_error() + .await; + }) + }) + .dispatch() + .await; +}