2022-04-02 15:34:43 +06:00
|
|
|
use teloxide::{prelude::*, utils::command::BotCommands};
|
2020-02-14 03:23:41 +06:00
|
|
|
|
2022-03-24 17:25:42 +06:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
pretty_env_logger::init();
|
2022-04-27 00:54:37 +06:00
|
|
|
log::info!("Starting command bot...");
|
2022-03-24 17:25:42 +06:00
|
|
|
|
2022-09-29 09:37:20 +06:00
|
|
|
let bot = Bot::from_env();
|
2022-03-24 17:25:42 +06:00
|
|
|
|
2022-10-11 14:54:51 +06:00
|
|
|
Command::repl(bot, answer).await;
|
2022-03-24 17:25:42 +06:00
|
|
|
}
|
|
|
|
|
2022-04-02 15:34:43 +06:00
|
|
|
#[derive(BotCommands, Clone)]
|
2022-10-05 10:08:50 +04:00
|
|
|
#[command(rename_rule = "lowercase", description = "These commands are supported:")]
|
2020-02-14 03:23:41 +06:00
|
|
|
enum Command {
|
|
|
|
#[command(description = "display this text.")]
|
|
|
|
Help,
|
2020-07-07 01:20:09 +06:00
|
|
|
#[command(description = "handle a username.")]
|
|
|
|
Username(String),
|
2020-07-26 23:47:02 +06:00
|
|
|
#[command(description = "handle a username and an age.", parse_with = "split")]
|
2020-07-07 01:20:09 +06:00
|
|
|
UsernameAndAge { username: String, age: u8 },
|
2020-02-19 04:54:41 +06:00
|
|
|
}
|
2020-02-14 03:23:41 +06:00
|
|
|
|
2022-10-03 17:07:38 +06: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?,
|
2020-07-07 01:20:09 +06:00
|
|
|
Command::Username(username) => {
|
2022-10-03 17:07:38 +06:00
|
|
|
bot.send_message(msg.chat.id, format!("Your username is @{username}.")).await?
|
2020-07-07 01:20:09 +06:00
|
|
|
}
|
|
|
|
Command::UsernameAndAge { username, age } => {
|
2022-10-03 17:07:38 +06:00
|
|
|
bot.send_message(msg.chat.id, format!("Your username is @{username} and age is {age}."))
|
|
|
|
.await?
|
2020-07-07 01:20:09 +06:00
|
|
|
}
|
2020-02-14 03:23:41 +06:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|