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"
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"

View file

@ -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(())

View file

@ -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(())