diff --git a/README.md b/README.md index 5194427f..30e37f2e 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ Wanna see more? This is a bot, which starts a game on each incoming message. You ([Full](https://github.com/teloxide/teloxide/blob/master/examples/guess_a_number_bot/src/main.rs)) ```rust -// Setup is omitted... +// Imports are omitted... #[derive(SmartDefault)] enum Dialogue { @@ -190,56 +190,57 @@ enum Dialogue { ReceiveAttempt(u8), } -async fn handle_message( - cx: DialogueDispatcherHandlerCx, -) -> ResponseResult> { - match cx.dialogue { - Dialogue::Start => { - cx.answer( - "Let's play a game! Guess a number from 1 to 10 (inclusively).", - ) - .send() - .await?; - next(Dialogue::ReceiveAttempt(thread_rng().gen_range(1, 11))) +type Cx = DialogueDispatcherHandlerCx; +type Res = ResponseResult>; + +async fn start(cx: Cx<()>) -> Res { + cx.answer("Let's play a game! Guess a number from 1 to 10 (inclusively).") + .send() + .await?; + next(Dialogue::ReceiveAttempt(thread_rng().gen_range(1, 11))) +} + +async fn receive_attempt(cx: Cx) -> Res { + match cx.update.text() { + None => { + cx.answer("Oh, please, send me a text message!").send().await?; + next(Dialogue::ReceiveAttempt(cx.dialogue)) } - Dialogue::ReceiveAttempt(secret) => match cx.update.text() { - None => { - cx.answer("Oh, please, send me a text message!").send().await?; - next(cx.dialogue) - } - Some(text) => match text.parse::() { - Ok(attempt) => match attempt { - x if !(1..=10).contains(&x) => { - cx.answer( - "Oh, please, send me a number in the range [1; \ - 10]!", - ) - .send() - .await?; - next(cx.dialogue) - } - x if x == secret => { - cx.answer("Congratulations! You won!").send().await?; - exit() - } - _ => { - cx.answer("No.").send().await?; - next(cx.dialogue) - } - }, - Err(_) => { + Some(text) => match text.parse::() { + Ok(attempt) => match attempt { + x if !(1..=10).contains(&x) => { cx.answer( "Oh, please, send me a number in the range [1; 10]!", ) .send() .await?; - next(cx.dialogue) + next(Dialogue::ReceiveAttempt(cx.dialogue)) + } + x if x == cx.dialogue => { + cx.answer("Congratulations! You won!").send().await?; + exit() + } + _ => { + cx.answer("No.").send().await?; + next(Dialogue::ReceiveAttempt(cx.dialogue)) } }, + Err(_) => { + cx.answer("Oh, please, send me a number in the range [1; 10]!") + .send() + .await?; + next(Dialogue::ReceiveAttempt(cx.dialogue)) + } }, } } +async fn handle_message( + cx: DialogueDispatcherHandlerCx, +) -> Res { + // Match is Omitted... +} + #[tokio::main] async fn main() { // Setup is omitted... diff --git a/examples/guess_a_number_bot/src/main.rs b/examples/guess_a_number_bot/src/main.rs index 12effdbf..2bab290a 100644 --- a/examples/guess_a_number_bot/src/main.rs +++ b/examples/guess_a_number_bot/src/main.rs @@ -38,56 +38,73 @@ enum Dialogue { // [Control a dialogue] // ============================================================================ -async fn handle_message( - cx: DialogueDispatcherHandlerCx, -) -> ResponseResult> { - match cx.dialogue { - Dialogue::Start => { - cx.answer( - "Let's play a game! Guess a number from 1 to 10 (inclusively).", - ) - .send() - .await?; - next(Dialogue::ReceiveAttempt(thread_rng().gen_range(1, 11))) +type Cx = DialogueDispatcherHandlerCx; +type Res = ResponseResult>; + +async fn start(cx: Cx<()>) -> Res { + cx.answer("Let's play a game! Guess a number from 1 to 10 (inclusively).") + .send() + .await?; + next(Dialogue::ReceiveAttempt(thread_rng().gen_range(1, 11))) +} + +async fn receive_attempt(cx: Cx) -> Res { + match cx.update.text() { + None => { + cx.answer("Oh, please, send me a text message!").send().await?; + next(Dialogue::ReceiveAttempt(cx.dialogue)) } - Dialogue::ReceiveAttempt(secret) => match cx.update.text() { - None => { - cx.answer("Oh, please, send me a text message!").send().await?; - next(cx.dialogue) - } - Some(text) => match text.parse::() { - Ok(attempt) => match attempt { - x if !(1..=10).contains(&x) => { - cx.answer( - "Oh, please, send me a number in the range [1; \ - 10]!", - ) - .send() - .await?; - next(cx.dialogue) - } - x if x == secret => { - cx.answer("Congratulations! You won!").send().await?; - exit() - } - _ => { - cx.answer("No.").send().await?; - next(cx.dialogue) - } - }, - Err(_) => { + Some(text) => match text.parse::() { + Ok(attempt) => match attempt { + x if !(1..=10).contains(&x) => { cx.answer( "Oh, please, send me a number in the range [1; 10]!", ) .send() .await?; - next(cx.dialogue) + next(Dialogue::ReceiveAttempt(cx.dialogue)) + } + x if x == cx.dialogue => { + cx.answer("Congratulations! You won!").send().await?; + exit() + } + _ => { + cx.answer("No.").send().await?; + next(Dialogue::ReceiveAttempt(cx.dialogue)) } }, + Err(_) => { + cx.answer("Oh, please, send me a number in the range [1; 10]!") + .send() + .await?; + next(Dialogue::ReceiveAttempt(cx.dialogue)) + } }, } } +async fn handle_message( + cx: DialogueDispatcherHandlerCx, +) -> Res { + match cx { + DialogueDispatcherHandlerCx { + bot, + update, + dialogue: Dialogue::Start, + } => start(DialogueDispatcherHandlerCx::new(bot, update, ())).await, + DialogueDispatcherHandlerCx { + bot, + update, + dialogue: Dialogue::ReceiveAttempt(secret), + } => { + receive_attempt(DialogueDispatcherHandlerCx::new( + bot, update, secret, + )) + .await + } + } +} + // ============================================================================ // [Run!] // ============================================================================