2022-03-24 12:25:42 +01:00
|
|
|
use teloxide::{prelude::*, utils::command::BotCommand};
|
2020-02-13 22:23:41 +01:00
|
|
|
|
2022-01-06 13:04:09 +01:00
|
|
|
use std::error::Error;
|
2021-03-19 12:58:22 +01:00
|
|
|
|
2022-03-24 12:25:42 +01:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
pretty_env_logger::init();
|
|
|
|
log::info!("Starting simple_commands_bot...");
|
|
|
|
|
|
|
|
let bot = Bot::from_env().auto_send();
|
|
|
|
|
|
|
|
teloxide::commands_repl(bot, answer, Command::ty()).await;
|
|
|
|
}
|
|
|
|
|
2022-01-06 13:04:09 +01:00
|
|
|
#[derive(BotCommand, Clone)]
|
2020-02-13 22:23:41 +01:00
|
|
|
#[command(rename = "lowercase", description = "These commands are supported:")]
|
|
|
|
enum Command {
|
|
|
|
#[command(description = "display this text.")]
|
|
|
|
Help,
|
2020-07-06 21:20:09 +02:00
|
|
|
#[command(description = "handle a username.")]
|
|
|
|
Username(String),
|
2020-07-26 19:47:02 +02:00
|
|
|
#[command(description = "handle a username and an age.", parse_with = "split")]
|
2020-07-06 21:20:09 +02:00
|
|
|
UsernameAndAge { username: String, age: u8 },
|
2020-02-18 23:54:41 +01:00
|
|
|
}
|
2020-02-13 22:23:41 +01:00
|
|
|
|
2021-03-19 12:58:22 +01:00
|
|
|
async fn answer(
|
2022-01-06 13:04:09 +01:00
|
|
|
bot: AutoSend<Bot>,
|
|
|
|
message: Message,
|
|
|
|
command: Command,
|
2021-03-19 12:58:22 +01:00
|
|
|
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
2022-01-06 13:04:09 +01:00
|
|
|
match command {
|
2021-12-26 15:55:24 +01:00
|
|
|
Command::Help => bot.send_message(message.chat.id, Command::descriptions()).await?,
|
2020-07-06 21:20:09 +02:00
|
|
|
Command::Username(username) => {
|
2021-12-11 12:11:25 +01:00
|
|
|
bot.send_message(message.chat.id, format!("Your username is @{}.", username)).await?
|
2020-07-06 21:20:09 +02:00
|
|
|
}
|
|
|
|
Command::UsernameAndAge { username, age } => {
|
2021-12-26 15:55:24 +01:00
|
|
|
bot.send_message(
|
|
|
|
message.chat.id,
|
|
|
|
format!("Your username is @{} and age is {}.", username, age),
|
|
|
|
)
|
|
|
|
.await?
|
2020-07-06 21:20:09 +02:00
|
|
|
}
|
2020-02-13 22:23:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|