From c3d5ed9fb70ec8cc5fd670504ef0d711257cdc86 Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Mon, 25 Apr 2022 01:23:05 +0600 Subject: [PATCH] Use `teloxide::handler!` for command handling too --- examples/purchase.rs | 51 ++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/examples/purchase.rs b/examples/purchase.rs index ea81b303..034441bf 100644 --- a/examples/purchase.rs +++ b/examples/purchase.rs @@ -42,6 +42,8 @@ enum Command { Help, #[command(description = "start the purchase procedure.")] Start, + #[command(description = "cancel the purchase procedure.")] + Cancel, } #[tokio::main] @@ -57,9 +59,14 @@ async fn main() { .branch( Update::filter_message() .branch( - teloxide::handler![State::Start] + dptree::entry() .filter_command::() - .endpoint(handle_command), + .branch( + teloxide::handler![State::Start] + .branch(teloxide::handler![Command::Help].endpoint(help)) + .branch(teloxide::handler![Command::Start].endpoint(start)), + ) + .branch(teloxide::handler![Command::Cancel].endpoint(cancel)), ) .branch(teloxide::handler![State::ReceiveFullName].endpoint(receive_full_name)) .branch(dptree::endpoint(invalid_state)), @@ -78,22 +85,26 @@ async fn main() { .await; } -async fn handle_command( - bot: AutoSend, - msg: Message, - cmd: Command, - dialogue: MyDialogue, -) -> HandlerResult { - match cmd { - Command::Help => { - bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?; - } - Command::Start => { - bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?; - dialogue.update(State::ReceiveFullName).await?; - } - } +async fn start(bot: AutoSend, msg: Message, dialogue: MyDialogue) -> HandlerResult { + bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?; + dialogue.update(State::ReceiveFullName).await?; + Ok(()) +} +async fn help(bot: AutoSend, msg: Message) -> HandlerResult { + bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?; + Ok(()) +} + +async fn cancel(bot: AutoSend, msg: Message, dialogue: MyDialogue) -> HandlerResult { + bot.send_message(msg.chat.id, "Cancelling the dialogue.").await?; + dialogue.exit().await?; + Ok(()) +} + +async fn invalid_state(bot: AutoSend, msg: Message) -> HandlerResult { + bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see the usage.") + .await?; Ok(()) } @@ -138,9 +149,3 @@ async fn receive_product_selection( Ok(()) } - -async fn invalid_state(bot: AutoSend, msg: Message) -> HandlerResult { - bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see the usage.") - .await?; - Ok(()) -}