mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 06:25:10 +01:00
Add examples/shared_state_bot
This commit is contained in:
parent
637dcd2535
commit
2374b5c2d3
4 changed files with 58 additions and 2 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -9,3 +9,4 @@ examples/multiple_handlers_bot/target
|
|||
examples/admin_bot/target
|
||||
examples/guess_a_number_bot/target
|
||||
examples/simple_commands_bot/target
|
||||
examples/shared_state_bot/target
|
|
@ -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. |
|
14
examples/shared_state_bot/Cargo.toml
Normal file
14
examples/shared_state_bot/Cargo.toml
Normal file
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "shared_state_bot"
|
||||
version = "0.1.0"
|
||||
authors = ["Temirkhan Myrzamadi <hirrolot@gmail.com>"]
|
||||
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 = "../../" }
|
41
examples/shared_state_bot/src/main.rs
Normal file
41
examples/shared_state_bot/src/main.rs
Normal file
|
@ -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<Message>| {
|
||||
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;
|
||||
}
|
Loading…
Reference in a new issue