update simple_commands_bot example

This commit is contained in:
p0lunin 2022-01-06 14:04:09 +02:00
parent 593dbc29f7
commit d6e439b0cf

View file

@ -1,8 +1,9 @@
use teloxide::{prelude::*, utils::command::BotCommand}; use teloxide::{prelude::*, utils::command::BotCommand};
use std::{error::Error, sync::Arc}; use std::error::Error;
use teloxide::types::Me;
#[derive(BotCommand)] #[derive(BotCommand, Clone)]
#[command(rename = "lowercase", description = "These commands are supported:")] #[command(rename = "lowercase", description = "These commands are supported:")]
enum Command { enum Command {
#[command(description = "display this text.")] #[command(description = "display this text.")]
@ -14,11 +15,11 @@ enum Command {
} }
async fn answer( async fn answer(
bot: Arc<AutoSend<Bot>>, bot: AutoSend<Bot>,
message: Arc<Message>, message: Message,
command: Arc<Command>, command: Command,
) -> Result<(), Box<dyn Error + Send + Sync>> { ) -> Result<(), Box<dyn Error + Send + Sync>> {
match command.as_ref() { match command {
Command::Help => bot.send_message(message.chat.id, Command::descriptions()).await?, Command::Help => bot.send_message(message.chat.id, Command::descriptions()).await?,
Command::Username(username) => { Command::Username(username) => {
bot.send_message(message.chat.id, format!("Your username is @{}.", username)).await? bot.send_message(message.chat.id, format!("Your username is @{}.", username)).await?
@ -42,6 +43,8 @@ async fn main() {
let bot = Bot::from_env().auto_send(); let bot = Bot::from_env().auto_send();
let bot_name: String = "".into(); // panic!("Your bot's name here"); let Me { user: bot_user, .. } = bot.get_me().await.unwrap();
let bot_name = bot_user.username.expect("Bots must have usernames");
teloxide::commands_repl(bot, bot_name, answer, Command::ty()).await; teloxide::commands_repl(bot, bot_name, answer, Command::ty()).await;
} }