mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-08 19:33:53 +01:00
Show strongly typed behaviour in examples/simple_commands_bot
This commit is contained in:
parent
9442ee5a7f
commit
31ac9bd984
2 changed files with 17 additions and 13 deletions
|
@ -10,6 +10,5 @@ edition = "2018"
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
futures = "0.3.4"
|
futures = "0.3.4"
|
||||||
tokio = "0.2.9"
|
tokio = "0.2.9"
|
||||||
rand = "0.7.3"
|
|
||||||
pretty_env_logger = "0.4.0"
|
pretty_env_logger = "0.4.0"
|
||||||
teloxide = { path = "../../" }
|
teloxide = { path = "../../" }
|
||||||
|
|
|
@ -1,20 +1,17 @@
|
||||||
use teloxide::{prelude::*, utils::command::BotCommand};
|
use teloxide::{prelude::*, utils::command::BotCommand};
|
||||||
|
|
||||||
use rand::{thread_rng, Rng};
|
|
||||||
|
|
||||||
#[derive(BotCommand)]
|
#[derive(BotCommand)]
|
||||||
#[command(rename = "lowercase", description = "These commands are supported:")]
|
#[command(rename = "lowercase", description = "These commands are supported:")]
|
||||||
enum Command {
|
enum Command {
|
||||||
#[command(description = "display this text.")]
|
#[command(description = "display this text.")]
|
||||||
Help,
|
Help,
|
||||||
#[command(description = "be a cat.")]
|
#[command(description = "handle a username.")]
|
||||||
Meow,
|
Username(String),
|
||||||
#[command(description = "generate a random number within [0; 1).")]
|
#[command(
|
||||||
Generate,
|
description = "handle a username and an age.",
|
||||||
}
|
parse_with = "split"
|
||||||
|
)]
|
||||||
fn generate() -> String {
|
UsernameAndAge { username: String, age: u8 },
|
||||||
thread_rng().gen_range(0.0, 1.0).to_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn answer(
|
async fn answer(
|
||||||
|
@ -23,8 +20,16 @@ async fn answer(
|
||||||
) -> ResponseResult<()> {
|
) -> ResponseResult<()> {
|
||||||
match command {
|
match command {
|
||||||
Command::Help => cx.answer(Command::descriptions()).send().await?,
|
Command::Help => cx.answer(Command::descriptions()).send().await?,
|
||||||
Command::Generate => cx.answer(generate()).send().await?,
|
Command::Username(username) => {
|
||||||
Command::Meow => cx.answer("I am a cat! Meow!").send().await?,
|
cx.answer_str(format!("Your username is @{}.", username)).await?
|
||||||
|
}
|
||||||
|
Command::UsernameAndAge { username, age } => {
|
||||||
|
cx.answer_str(format!(
|
||||||
|
"Your username is @{} and age is {}.",
|
||||||
|
username, age
|
||||||
|
))
|
||||||
|
.await?
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue