diff --git a/examples/dialogue_bot/src/main.rs b/examples/dialogue_bot/src/main.rs index 663c503c..9d610c2f 100644 --- a/examples/dialogue_bot/src/main.rs +++ b/examples/dialogue_bot/src/main.rs @@ -153,7 +153,10 @@ async fn favourite_music(cx: Cx<ReceiveFavouriteMusicState>) -> Res { async fn handle_message(cx: Cx<Dialogue>) -> Res { let DialogueDispatcherHandlerCx { bot, update, dialogue } = cx; - match dialogue.unwrap() { + + // You need handle the error instead of panicking in real-world code, maybe + // send diagnostics to a development chat. + match dialogue.expect("Failed to get dialogue info from storage") { Dialogue::Start => { start(DialogueDispatcherHandlerCx::new(bot, update, ())).await } @@ -186,10 +189,8 @@ async fn run() { let bot = Bot::from_env(); Dispatcher::new(bot) - .messages_handler(DialogueDispatcher::new(|cx| { - async move { - handle_message(cx).await.expect("Something wrong with the bot!") - } + .messages_handler(DialogueDispatcher::new(|cx| async move { + handle_message(cx).await.expect("Something wrong with the bot!") })) .dispatch() .await; diff --git a/examples/guess_a_number_bot/src/main.rs b/examples/guess_a_number_bot/src/main.rs index 78727647..019f593a 100644 --- a/examples/guess_a_number_bot/src/main.rs +++ b/examples/guess_a_number_bot/src/main.rs @@ -21,8 +21,8 @@ extern crate smart_default; use teloxide::prelude::*; -use std::convert::Infallible; use rand::{thread_rng, Rng}; +use std::convert::Infallible; // ============================================================================ // [A type-safe finite automaton] @@ -80,23 +80,20 @@ async fn receive_attempt(cx: Cx<u8>) -> Res { async fn handle_message( cx: DialogueDispatcherHandlerCx<Message, Dialogue, Infallible>, ) -> Res { - match cx { - DialogueDispatcherHandlerCx { - bot, - update, - dialogue: Ok(Dialogue::Start), - } => start(DialogueDispatcherHandlerCx::new(bot, update, ())).await, - DialogueDispatcherHandlerCx { - bot, - update, - dialogue: Ok(Dialogue::ReceiveAttempt(secret)), - } => { + let DialogueDispatcherHandlerCx { bot, update, dialogue } = cx; + + // You need handle the error instead of panicking in real-world code, maybe + // send diagnostics to a development chat. + match dialogue.expect("Failed to get dialogue info from storage") { + Dialogue::Start => { + start(DialogueDispatcherHandlerCx::new(bot, update, ())).await + } + Dialogue::ReceiveAttempt(secret) => { receive_attempt(DialogueDispatcherHandlerCx::new( bot, update, secret, )) .await } - _ => panic!("Failed to get dialogue info from storage") } }