teloxide/examples/command.rs

38 lines
1.2 KiB
Rust
Raw Normal View History

2022-04-02 11:34:43 +02:00
use teloxide::{prelude::*, utils::command::BotCommands};
2020-02-13 22:23:41 +01:00
2022-03-24 12:25:42 +01:00
#[tokio::main]
async fn main() {
pretty_env_logger::init();
log::info!("Starting command bot...");
2022-03-24 12:25:42 +01:00
let bot = Bot::from_env();
2022-03-24 12:25:42 +01:00
teloxide::commands_repl(bot, answer, Command::ty()).await;
}
2022-04-02 11:34:43 +02:00
#[derive(BotCommands, Clone)]
#[command(rename_rule = "lowercase", description = "These commands are supported:")]
2020-02-13 22:23:41 +01:00
enum Command {
#[command(description = "display this text.")]
Help,
#[command(description = "handle a username.")]
Username(String),
#[command(description = "handle a username and an age.", parse_with = "split")]
UsernameAndAge { username: String, age: u8 },
2020-02-18 23:54:41 +01:00
}
2020-02-13 22:23:41 +01:00
async fn answer(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
match cmd {
Command::Help => bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?,
Command::Username(username) => {
bot.send_message(msg.chat.id, format!("Your username is @{username}.")).await?
}
Command::UsernameAndAge { username, age } => {
bot.send_message(msg.chat.id, format!("Your username is @{username} and age is {age}."))
.await?
}
2020-02-13 22:23:41 +01:00
};
Ok(())
}