diff --git a/README.md b/README.md index c8f1da9d..51e4dabe 100644 --- a/README.md +++ b/README.md @@ -180,11 +180,11 @@ async fn main() { ### Dialogues management -A dialogue is described by an enumeration, where each variant is one of possible dialogue's states. There are also _transition functions_, which turn a dialogue from one state to another, thereby forming an [FSM]. +A dialogue is described by an enumeration, where each variant is one of possible dialogue's states. There are also _subtransition functions_, which turn a dialogue from one state to another, thereby forming a [FSM]. [FSM]: https://en.wikipedia.org/wiki/Finite-state_machine -Below is a bot, which asks you three questions and then sends the answers back to you. Here's possible states for a dialogue: +Below is a bot, which asks you three questions and then sends the answers back to you. First, let's start with an enumeration (a collection of our dialogue's states): ([dialogue_bot/src/dialogue/mod.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/dialogue/mod.rs)) ```rust @@ -205,6 +205,11 @@ impl Default for Dialogue { } ``` +When a user sends a message to our bot, and such a dialogue does not yet exist, `Dialogue::default()` is invoked, which is `Dialogue::Start`. Every time a message is received, an associated dialogue is extracted, and then passed to a corresponding subtransition function: + +
+ Dialogue::Start + ([dialogue_bot/src/dialogue/states/start.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/dialogue/states/start.rs)) ```rust // Imports are omitted... @@ -219,6 +224,34 @@ async fn start(_state: StartState, cx: TransitionIn, _ans: String) -> Transition } ``` +
+ +
+ Dialogue::ReceiveFullName + +([dialogue_bot/src/dialogue/states/receive_full_name.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/dialogue/states/receive_full_name.rs)) +```rust +// Imports are omitted... + +#[derive(Generic)] +pub struct ReceiveFullNameState; + +#[teloxide(subtransition)] +async fn receive_full_name( + state: ReceiveFullNameState, + cx: TransitionIn, + ans: String, +) -> TransitionOut { + cx.answer_str("How old are you?").await?; + next(ReceiveAgeState::up(state, ans)) +} +``` + +
+ +
+ Dialogue::ReceiveAge + ([dialogue_bot/src/dialogue/states/receive_age.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/dialogue/states/receive_age.rs)) ```rust // Imports are omitted... @@ -247,23 +280,10 @@ async fn receive_age_state( } ``` -([dialogue_bot/src/dialogue/states/receive_full_name.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/dialogue/states/receive_full_name.rs)) -```rust -// Imports are omitted... +
-#[derive(Generic)] -pub struct ReceiveFullNameState; - -#[teloxide(subtransition)] -async fn receive_full_name( - state: ReceiveFullNameState, - cx: TransitionIn, - ans: String, -) -> TransitionOut { - cx.answer_str("How old are you?").await?; - next(ReceiveAgeState::up(state, ans)) -} -``` +
+ Dialogue::ReceiveLocation ([dialogue_bot/src/dialogue/states/receive_location.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/dialogue/states/receive_location.rs)) ```rust @@ -287,6 +307,9 @@ async fn receive_location( } ``` + +
+ Finally, the `main` function looks like this: ([dialogue_bot/src/main.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/main.rs))