Refactor examples/dialogue_bot

This commit is contained in:
Temirkhan Myrzamadi 2020-07-24 18:25:52 +06:00
parent 008e8505c5
commit 34633ad57b
4 changed files with 34 additions and 24 deletions

View file

@ -14,7 +14,8 @@ futures = "0.3.5"
smart-default = "0.6.0"
derive_more = "0.99.9"
teloxide = { path = "../../" }
teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" }
teloxide-macros = { path = "../../../teloxide-macros" }
#teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" }
[profile.release]
lto = true

View file

@ -28,7 +28,6 @@ mod states;
mod transitions;
use states::*;
use transitions::*;
use std::convert::Infallible;
use teloxide::prelude::*;

View file

@ -1,5 +1,26 @@
use teloxide::prelude::*;
use super::transitions::{
receive_10x5_answer, receive_days_of_week,
receive_gandalf_alternative_name, start,
};
#[derive(BotDialogue, SmartDefault, From)]
pub enum Dialogue {
#[default]
#[handler(start)]
Start(StartState),
#[handler(receive_days_of_week)]
ReceiveDaysOfWeek(ReceiveDaysOfWeekState),
#[handler(receive_10x5_answer)]
Receive10x5Answer(Receive10x5AnswerState),
#[handler(receive_gandalf_alternative_name)]
ReceiveGandalfAlternativeName(ReceiveGandalfAlternativeNameState),
}
#[derive(Default)]
pub struct StartState;

View file

@ -1,17 +1,22 @@
use crate::states::{
Dialogue, Receive10x5AnswerState, ReceiveDaysOfWeekState,
ReceiveGandalfAlternativeNameState, StartState,
};
use teloxide::prelude::*;
use super::states::*;
pub type Cx = UpdateWithCx<Message>;
pub type Out = TransitionOut<Dialogue>;
async fn start(cx: Cx, state: StartState) -> Out {
pub async fn start(cx: Cx, state: StartState) -> Out {
cx.answer_str("Let's start our test! How many days per week are there?")
.await?;
next(state.up())
}
async fn receive_days_of_week(cx: Cx, state: ReceiveDaysOfWeekState) -> Out {
pub async fn receive_days_of_week(
cx: Cx,
state: ReceiveDaysOfWeekState,
) -> Out {
match cx.update.text().map(str::parse) {
Some(Ok(ans)) if ans == 7 => {
cx.answer_str("10*5 = ?").await?;
@ -24,7 +29,7 @@ async fn receive_days_of_week(cx: Cx, state: ReceiveDaysOfWeekState) -> Out {
}
}
async fn receive_10x5_answer(cx: Cx, state: Receive10x5AnswerState) -> Out {
pub async fn receive_10x5_answer(cx: Cx, state: Receive10x5AnswerState) -> Out {
match cx.update.text().map(str::parse) {
Some(Ok(ans)) if ans == 50 => {
cx.answer_str("What's an alternative name of Gandalf?").await?;
@ -37,7 +42,7 @@ async fn receive_10x5_answer(cx: Cx, state: Receive10x5AnswerState) -> Out {
}
}
async fn receive_gandalf_alternative_name(
pub async fn receive_gandalf_alternative_name(
cx: Cx,
state: ReceiveGandalfAlternativeNameState,
) -> Out {
@ -55,19 +60,3 @@ async fn receive_gandalf_alternative_name(
}
}
}
#[derive(BotDialogue, SmartDefault, From)]
pub enum Dialogue {
#[default]
#[handler(start)]
Start(StartState),
#[handler(receive_days_of_week)]
ReceiveDaysOfWeek(ReceiveDaysOfWeekState),
#[handler(receive_10x5_answer)]
Receive10x5Answer(Receive10x5AnswerState),
#[handler(receive_gandalf_alternative_name)]
ReceiveGandalfAlternativeName(ReceiveGandalfAlternativeNameState),
}