Use #[derive(BotCommand)] in examples/sqlite_remember.rs and examples/redis_remember.rs

This commit is contained in:
Hirrolot 2022-02-04 20:59:46 +06:00
parent b66beef534
commit 24917a60d4
3 changed files with 52 additions and 14 deletions

View file

@ -97,6 +97,7 @@ smart-default = "0.6.0"
rand = "0.8.3" rand = "0.8.3"
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
once_cell = "1.9.0" once_cell = "1.9.0"
lazy_static = "1.4.0"
anyhow = "1.0.52" anyhow = "1.0.52"
serde = "1" serde = "1"
serde_json = "1" serde_json = "1"

View file

@ -2,6 +2,8 @@ use teloxide::{
dispatching2::dialogue::{serializer::Bincode, RedisStorage, Storage}, dispatching2::dialogue::{serializer::Bincode, RedisStorage, Storage},
macros::DialogueState, macros::DialogueState,
prelude2::*, prelude2::*,
types::Me,
utils::command::BotCommand,
RequestError, RequestError,
}; };
use thiserror::Error; 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] #[tokio::main]
async fn main() { async fn main() {
let bot = Bot::from_env().auto_send(); let bot = Bot::from_env().auto_send();
@ -82,16 +92,24 @@ async fn handle_got_number(
msg: Message, msg: Message,
dialogue: MyDialogue, dialogue: MyDialogue,
num: i32, num: i32,
me: Me,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let ans = msg.text().unwrap(); let ans = msg.text().unwrap();
let bot_name = me.user.username.unwrap();
if ans.starts_with("/get") { match Command::parse(ans, bot_name) {
bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?; Ok(cmd) => match cmd {
} else if ans.starts_with("/reset") { Command::Get => {
dialogue.reset().await?; bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?;
bot.send_message(msg.chat.id, "Resetted number").await?; }
} else { Command::Reset => {
bot.send_message(msg.chat.id, "Please, send /get or /reset").await?; 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(()) Ok(())

View file

@ -2,6 +2,8 @@ use teloxide::{
dispatching2::dialogue::{serializer::Json, SqliteStorage, Storage}, dispatching2::dialogue::{serializer::Json, SqliteStorage, Storage},
macros::DialogueState, macros::DialogueState,
prelude2::*, prelude2::*,
types::Me,
utils::command::BotCommand,
RequestError, RequestError,
}; };
use thiserror::Error; 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] #[tokio::main]
async fn main() { async fn main() {
let bot = Bot::from_env().auto_send(); let bot = Bot::from_env().auto_send();
@ -78,16 +89,24 @@ async fn handle_got_number(
msg: Message, msg: Message,
dialogue: MyDialogue, dialogue: MyDialogue,
num: i32, num: i32,
me: Me,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let ans = msg.text().unwrap(); let ans = msg.text().unwrap();
let bot_name = me.user.username.unwrap();
if ans.starts_with("/get") { match Command::parse(ans, bot_name) {
bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?; Ok(cmd) => match cmd {
} else if ans.starts_with("/reset") { Command::Get => {
dialogue.reset().await?; bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?;
bot.send_message(msg.chat.id, "Resetted number").await?; }
} else { Command::Reset => {
bot.send_message(msg.chat.id, "Please, send /get or /reset").await?; 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(()) Ok(())