command/
command.rs

1use teloxide::{prelude::*, utils::command::BotCommands};
2
3#[tokio::main]
4async fn main() {
5    pretty_env_logger::init();
6    log::info!("Starting command bot...");
7
8    let bot = Bot::from_env();
9
10    Command::repl(bot, answer).await;
11}
12
13/// These commands are supported:
14#[derive(BotCommands, Clone)]
15#[command(rename_rule = "lowercase")]
16enum Command {
17    /// Display this text.
18    #[command(aliases = ["h", "?"])]
19    Help,
20    /// Handle a username.
21    #[command(alias = "u")]
22    Username(String),
23    /// Handle a username and an age.
24    #[command(parse_with = "split", alias = "ua", hide_aliases)]
25    UsernameAndAge { username: String, age: u8 },
26}
27
28async fn answer(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
29    match cmd {
30        Command::Help => bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?,
31        Command::Username(username) => {
32            bot.send_message(msg.chat.id, format!("Your username is @{username}.")).await?
33        }
34        Command::UsernameAndAge { username, age } => {
35            bot.send_message(msg.chat.id, format!("Your username is @{username} and age is {age}."))
36                .await?
37        }
38    };
39
40    Ok(())
41}