mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
529a316b0f
Former-commit-id: dc652da1ac
37 lines
1.2 KiB
Rust
37 lines
1.2 KiB
Rust
use teloxide::{prelude::*, utils::command::BotCommands};
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
pretty_env_logger::init();
|
|
log::info!("Starting command bot...");
|
|
|
|
let bot = Bot::from_env();
|
|
|
|
teloxide::commands_repl(bot, answer, Command::ty()).await;
|
|
}
|
|
|
|
#[derive(BotCommands, Clone)]
|
|
#[command(rename_rule = "lowercase", description = "These commands are supported:")]
|
|
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 },
|
|
}
|
|
|
|
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?
|
|
}
|
|
};
|
|
|
|
Ok(())
|
|
}
|