Skip to main content

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 => {
31            bot.send_message(Command::descriptions().to_string()).chat_id(msg.chat.id).await?
32        }
33        Command::Username(username) => {
34            bot.send_message(format!("Your username is @{username}.")).chat_id(msg.chat.id).await?
35        }
36        Command::UsernameAndAge { username, age } => {
37            bot.send_message(format!("Your username is @{username} and age is {age}."))
38                .chat_id(msg.chat.id)
39                .await?
40        }
41    };
42
43    Ok(())
44}