From ddd94543d1acf49680572d83b50bb2b64a5515ad Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Sun, 24 May 2020 01:06:04 +0600 Subject: [PATCH] Update examples/dialogue_bot --- examples/dialogue_bot/src/main.rs | 34 ++++++++++++++++++------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/examples/dialogue_bot/src/main.rs b/examples/dialogue_bot/src/main.rs index 2c839933..9b3d3a51 100644 --- a/examples/dialogue_bot/src/main.rs +++ b/examples/dialogue_bot/src/main.rs @@ -79,7 +79,7 @@ impl ReceiveFavouriteMusicState { #[derive(Display)] #[display( -"Your full name: {full_name}, your age: {age}, your favourite music: \ + "Your full name: {full_name}, your age: {age}, your favourite music: \ {favourite_music}" )] struct ExitState { @@ -105,11 +105,15 @@ type Cx = DialogueDispatcherHandlerCx; type Res = ResponseResult>; async fn start(cx: Cx<()>) -> Res { + let DialogueDispatcherHandlerCx { cx, .. } = cx; + cx.answer("Let's start! First, what's your full name?").send().await?; next(Dialogue::ReceiveFullName) } async fn full_name(cx: Cx<()>) -> Res { + let DialogueDispatcherHandlerCx { cx, .. } = cx; + match cx.update.text() { None => { cx.answer("Please, send me a text message!").send().await?; @@ -125,57 +129,59 @@ async fn full_name(cx: Cx<()>) -> Res { } async fn age(cx: Cx) -> Res { + let DialogueDispatcherHandlerCx { cx, dialogue } = cx; + let dialogue = dialogue.unwrap(); + match cx.update.text().unwrap().parse() { Ok(age) => { cx.answer("Good. Now choose your favourite music:") .reply_markup(FavouriteMusic::markup()) .send() .await?; - next(Dialogue::ReceiveFavouriteMusic(cx.dialogue.unwrap().up(age))) + next(Dialogue::ReceiveFavouriteMusic(dialogue.up(age))) } Err(_) => { cx.answer("Oh, please, enter a number!").send().await?; - next(Dialogue::ReceiveAge(cx.dialogue.unwrap())) + next(Dialogue::ReceiveAge(dialogue)) } } } async fn favourite_music(cx: Cx) -> Res { + let DialogueDispatcherHandlerCx { cx, dialogue } = cx; + let dialogue = dialogue.unwrap(); + match cx.update.text().unwrap().parse() { Ok(favourite_music) => { - cx.answer(format!( - "Fine. {}", - cx.dialogue.clone().unwrap().up(favourite_music) - )) + cx.answer(format!("Fine. {}", dialogue.up(favourite_music))) .send() .await?; exit() } Err(_) => { cx.answer("Oh, please, enter from the keyboard!").send().await?; - next(Dialogue::ReceiveFavouriteMusic(cx.dialogue.unwrap())) + next(Dialogue::ReceiveFavouriteMusic(dialogue)) } } } async fn handle_message(cx: Cx) -> Res { - let DialogueDispatcherHandlerCx { bot, update, dialogue } = cx; + let DialogueDispatcherHandlerCx { cx, 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 + start(DialogueDispatcherHandlerCx::new(cx, ())).await } Dialogue::ReceiveFullName => { - full_name(DialogueDispatcherHandlerCx::new(bot, update, ())).await + full_name(DialogueDispatcherHandlerCx::new(cx, ())).await } Dialogue::ReceiveAge(s) => { - age(DialogueDispatcherHandlerCx::new(bot, update, s)).await + age(DialogueDispatcherHandlerCx::new(cx, s)).await } Dialogue::ReceiveFavouriteMusic(s) => { - favourite_music(DialogueDispatcherHandlerCx::new(bot, update, s)) - .await + favourite_music(DialogueDispatcherHandlerCx::new(cx, s)).await } } }