From 24917a60d49a1196eec51519780ca0afb9d35d93 Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Fri, 4 Feb 2022 20:59:46 +0600 Subject: [PATCH] Use `#[derive(BotCommand)]` in `examples/sqlite_remember.rs` and `examples/redis_remember.rs` --- Cargo.toml | 1 + examples/redis_remember.rs | 32 +++++++++++++++++++++++++------- examples/sqlite_remember.rs | 33 ++++++++++++++++++++++++++------- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 10b0d85a..71fb7fc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,6 +97,7 @@ smart-default = "0.6.0" rand = "0.8.3" pretty_env_logger = "0.4.0" once_cell = "1.9.0" +lazy_static = "1.4.0" anyhow = "1.0.52" serde = "1" serde_json = "1" diff --git a/examples/redis_remember.rs b/examples/redis_remember.rs index 8fc9f62f..0935bcdf 100644 --- a/examples/redis_remember.rs +++ b/examples/redis_remember.rs @@ -2,6 +2,8 @@ use teloxide::{ dispatching2::dialogue::{serializer::Bincode, RedisStorage, Storage}, macros::DialogueState, prelude2::*, + types::Me, + utils::command::BotCommand, RequestError, }; use thiserror::Error; @@ -34,6 +36,14 @@ impl Default for State { } } +#[derive(BotCommand)] +#[command(rename = "lowercase", description = "These commands are supported:")] +pub enum Command { + #[command(description = "get your number.")] + Get, + #[command(description = "reset your number.")] + Reset, +} #[tokio::main] async fn main() { let bot = Bot::from_env().auto_send(); @@ -82,16 +92,24 @@ async fn handle_got_number( msg: Message, dialogue: MyDialogue, num: i32, + me: Me, ) -> anyhow::Result<()> { let ans = msg.text().unwrap(); + let bot_name = me.user.username.unwrap(); - if ans.starts_with("/get") { - bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?; - } else if ans.starts_with("/reset") { - dialogue.reset().await?; - bot.send_message(msg.chat.id, "Resetted number").await?; - } else { - bot.send_message(msg.chat.id, "Please, send /get or /reset").await?; + match Command::parse(ans, bot_name) { + Ok(cmd) => match cmd { + Command::Get => { + bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?; + } + Command::Reset => { + dialogue.reset().await?; + bot.send_message(msg.chat.id, "Number resetted").await?; + } + }, + Err(_) => { + bot.send_message(msg.chat.id, "Please, send /get or /reset").await?; + } } Ok(()) diff --git a/examples/sqlite_remember.rs b/examples/sqlite_remember.rs index 5a109f83..e7a7fabd 100644 --- a/examples/sqlite_remember.rs +++ b/examples/sqlite_remember.rs @@ -2,6 +2,8 @@ use teloxide::{ dispatching2::dialogue::{serializer::Json, SqliteStorage, Storage}, macros::DialogueState, prelude2::*, + types::Me, + utils::command::BotCommand, RequestError, }; use thiserror::Error; @@ -34,6 +36,15 @@ impl Default for State { } } +#[derive(BotCommand)] +#[command(rename = "lowercase", description = "These commands are supported:")] +pub enum Command { + #[command(description = "get your number.")] + Get, + #[command(description = "reset your number.")] + Reset, +} + #[tokio::main] async fn main() { let bot = Bot::from_env().auto_send(); @@ -78,16 +89,24 @@ async fn handle_got_number( msg: Message, dialogue: MyDialogue, num: i32, + me: Me, ) -> anyhow::Result<()> { let ans = msg.text().unwrap(); + let bot_name = me.user.username.unwrap(); - if ans.starts_with("/get") { - bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?; - } else if ans.starts_with("/reset") { - dialogue.reset().await?; - bot.send_message(msg.chat.id, "Resetted number").await?; - } else { - bot.send_message(msg.chat.id, "Please, send /get or /reset").await?; + match Command::parse(ans, bot_name) { + Ok(cmd) => match cmd { + Command::Get => { + bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?; + } + Command::Reset => { + dialogue.reset().await?; + bot.send_message(msg.chat.id, "Number resetted").await?; + } + }, + Err(_) => { + bot.send_message(msg.chat.id, "Please, send /get or /reset").await?; + } } Ok(())