mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-09 11:43:57 +01:00
Use #[derive(BotCommand)]
in examples/sqlite_remember.rs
and examples/redis_remember.rs
This commit is contained in:
parent
b66beef534
commit
24917a60d4
3 changed files with 52 additions and 14 deletions
|
@ -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"
|
||||||
|
|
|
@ -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,17 +92,25 @@ 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) {
|
||||||
|
Ok(cmd) => match cmd {
|
||||||
|
Command::Get => {
|
||||||
bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?;
|
bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?;
|
||||||
} else if ans.starts_with("/reset") {
|
}
|
||||||
|
Command::Reset => {
|
||||||
dialogue.reset().await?;
|
dialogue.reset().await?;
|
||||||
bot.send_message(msg.chat.id, "Resetted number").await?;
|
bot.send_message(msg.chat.id, "Number resetted").await?;
|
||||||
} else {
|
}
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
bot.send_message(msg.chat.id, "Please, send /get or /reset").await?;
|
bot.send_message(msg.chat.id, "Please, send /get or /reset").await?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,17 +89,25 @@ 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) {
|
||||||
|
Ok(cmd) => match cmd {
|
||||||
|
Command::Get => {
|
||||||
bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?;
|
bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?;
|
||||||
} else if ans.starts_with("/reset") {
|
}
|
||||||
|
Command::Reset => {
|
||||||
dialogue.reset().await?;
|
dialogue.reset().await?;
|
||||||
bot.send_message(msg.chat.id, "Resetted number").await?;
|
bot.send_message(msg.chat.id, "Number resetted").await?;
|
||||||
} else {
|
}
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
bot.send_message(msg.chat.id, "Please, send /get or /reset").await?;
|
bot.send_message(msg.chat.id, "Please, send /get or /reset").await?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue