mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-14 11:44:04 +01:00
Make a test in examples/dialogue_bot
This commit is contained in:
parent
7bb2b49621
commit
f67d960d43
5 changed files with 54 additions and 84 deletions
|
@ -10,11 +10,8 @@ edition = "2018"
|
|||
log = "0.4.8"
|
||||
tokio = "0.2.9"
|
||||
pretty_env_logger = "0.4.0"
|
||||
|
||||
smart-default = "0.6.0"
|
||||
derive_more = "0.99.9"
|
||||
parse-display = "0.1.1"
|
||||
|
||||
teloxide = { path = "../../" }
|
||||
|
||||
[profile.release]
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
use parse_display::{Display, FromStr};
|
||||
|
||||
use teloxide::types::{KeyboardButton, ReplyKeyboardMarkup};
|
||||
|
||||
#[derive(Copy, Clone, Display, FromStr)]
|
||||
pub enum FavouriteMusic {
|
||||
Rock,
|
||||
Metal,
|
||||
Pop,
|
||||
Other,
|
||||
}
|
||||
|
||||
impl FavouriteMusic {
|
||||
pub fn markup() -> ReplyKeyboardMarkup {
|
||||
ReplyKeyboardMarkup::default().append_row(vec![
|
||||
KeyboardButton::new("Rock"),
|
||||
KeyboardButton::new("Metal"),
|
||||
KeyboardButton::new("Pop"),
|
||||
KeyboardButton::new("Other"),
|
||||
])
|
||||
}
|
||||
}
|
|
@ -22,7 +22,6 @@ extern crate smart_default;
|
|||
#[macro_use]
|
||||
extern crate derive_more;
|
||||
|
||||
mod favourite_music;
|
||||
mod states;
|
||||
mod transitions;
|
||||
|
||||
|
|
|
@ -1,47 +1,39 @@
|
|||
use teloxide::prelude::*;
|
||||
|
||||
use super::favourite_music::FavouriteMusic;
|
||||
use parse_display::Display;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct StartState;
|
||||
|
||||
pub struct ReceiveFullNameState {
|
||||
pub struct ReceiveDaysOfWeekState {
|
||||
rest: StartState,
|
||||
}
|
||||
|
||||
pub struct ReceiveAgeState {
|
||||
rest: ReceiveFullNameState,
|
||||
full_name: String,
|
||||
pub struct Receive10x5AnswerState {
|
||||
rest: ReceiveDaysOfWeekState,
|
||||
days_of_week: u8,
|
||||
}
|
||||
|
||||
pub struct ReceiveFavouriteMusicState {
|
||||
rest: ReceiveAgeState,
|
||||
age: u8,
|
||||
pub struct ReceiveGandalfAlternativeNameState {
|
||||
rest: Receive10x5AnswerState,
|
||||
_10x5_answer: u8,
|
||||
}
|
||||
|
||||
#[derive(Display)]
|
||||
#[display(
|
||||
"Your full name: {rest.rest.full_name}, your age: {rest.age}, your \
|
||||
favourite music: {favourite_music}"
|
||||
)]
|
||||
pub struct ExitState {
|
||||
rest: ReceiveFavouriteMusicState,
|
||||
favourite_music: FavouriteMusic,
|
||||
rest: ReceiveGandalfAlternativeNameState,
|
||||
gandalf_alternative_name: String,
|
||||
}
|
||||
|
||||
up!(
|
||||
StartState -> ReceiveFullNameState,
|
||||
ReceiveFullNameState + [full_name: String] -> ReceiveAgeState,
|
||||
ReceiveAgeState + [age: u8] -> ReceiveFavouriteMusicState,
|
||||
ReceiveFavouriteMusicState + [favourite_music: FavouriteMusic] -> ExitState,
|
||||
StartState -> ReceiveDaysOfWeekState,
|
||||
ReceiveDaysOfWeekState + [days_of_week: u8] -> Receive10x5AnswerState,
|
||||
Receive10x5AnswerState + [_10x5_answer: u8] -> ReceiveGandalfAlternativeNameState,
|
||||
ReceiveGandalfAlternativeNameState + [gandalf_alternative_name: String] -> ExitState,
|
||||
);
|
||||
|
||||
#[derive(SmartDefault, From)]
|
||||
pub enum Dialogue {
|
||||
#[default]
|
||||
Start(StartState),
|
||||
ReceiveFullName(ReceiveFullNameState),
|
||||
ReceiveAge(ReceiveAgeState),
|
||||
ReceiveFavouriteMusic(ReceiveFavouriteMusicState),
|
||||
ReceiveDaysOfWeek(ReceiveDaysOfWeekState),
|
||||
Receive10x5Answer(Receive10x5AnswerState),
|
||||
ReceiveGandalfAlternativeName(ReceiveGandalfAlternativeNameState),
|
||||
}
|
||||
|
|
|
@ -1,56 +1,56 @@
|
|||
use teloxide::prelude::*;
|
||||
|
||||
use super::{favourite_music::FavouriteMusic, states::*};
|
||||
use super::states::*;
|
||||
|
||||
pub type Cx = UpdateWithCx<Message>;
|
||||
pub type Out = TransitionOut<Dialogue>;
|
||||
|
||||
async fn start(cx: Cx, state: StartState) -> Out {
|
||||
cx.answer_str("Let's start! First, what's your full name?").await?;
|
||||
cx.answer_str("Let's start our test! How many days per week are there?")
|
||||
.await?;
|
||||
next(state.up())
|
||||
}
|
||||
|
||||
async fn receive_full_name(cx: Cx, state: ReceiveFullNameState) -> Out {
|
||||
match cx.update.text_owned() {
|
||||
Some(full_name) => {
|
||||
cx.answer_str("What a wonderful name! Your age?").await?;
|
||||
next(state.up(full_name))
|
||||
}
|
||||
_ => {
|
||||
cx.answer_str("Please, enter a text message!").await?;
|
||||
next(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn receive_age(cx: Cx, state: ReceiveAgeState) -> Out {
|
||||
async fn receive_days_of_week(cx: Cx, state: ReceiveDaysOfWeekState) -> Out {
|
||||
match cx.update.text().map(str::parse) {
|
||||
Some(Ok(age)) => {
|
||||
cx.answer("Good. Now choose your favourite music:")
|
||||
.reply_markup(FavouriteMusic::markup())
|
||||
.send()
|
||||
.await?;
|
||||
next(state.up(age))
|
||||
Some(Ok(ans)) if ans == 7 => {
|
||||
cx.answer_str("10*5 = ?").await?;
|
||||
next(state.up(ans))
|
||||
}
|
||||
_ => {
|
||||
cx.answer_str("Please, enter a number!").await?;
|
||||
cx.answer_str("Try again.").await?;
|
||||
next(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn receive_favourite_music(
|
||||
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?;
|
||||
next(state.up(ans))
|
||||
}
|
||||
_ => {
|
||||
cx.answer_str("Try again.").await?;
|
||||
next(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn receive_gandalf_alternative_name(
|
||||
cx: Cx,
|
||||
state: ReceiveFavouriteMusicState,
|
||||
state: ReceiveGandalfAlternativeNameState,
|
||||
) -> Out {
|
||||
match cx.update.text().map(str::parse) {
|
||||
Some(Ok(favourite_music)) => {
|
||||
cx.answer_str(format!("Fine. {}", state.up(favourite_music)))
|
||||
.await?;
|
||||
match cx.update.text() {
|
||||
Some(ans) if ans == "Mithrandir" => {
|
||||
cx.answer_str(
|
||||
"Congratulations! You've successfully passed the test!",
|
||||
)
|
||||
.await?;
|
||||
exit()
|
||||
}
|
||||
_ => {
|
||||
cx.answer_str("Please, enter from the keyboard!").await?;
|
||||
cx.answer_str("Try again.").await?;
|
||||
next(state)
|
||||
}
|
||||
}
|
||||
|
@ -59,10 +59,14 @@ async fn receive_favourite_music(
|
|||
pub async fn dispatch(cx: Cx, dialogue: Dialogue) -> Out {
|
||||
match dialogue {
|
||||
Dialogue::Start(state) => start(cx, state).await,
|
||||
Dialogue::ReceiveFullName(state) => receive_full_name(cx, state).await,
|
||||
Dialogue::ReceiveAge(state) => receive_age(cx, state).await,
|
||||
Dialogue::ReceiveFavouriteMusic(state) => {
|
||||
receive_favourite_music(cx, state).await
|
||||
Dialogue::ReceiveDaysOfWeek(state) => {
|
||||
receive_days_of_week(cx, state).await
|
||||
}
|
||||
Dialogue::Receive10x5Answer(state) => {
|
||||
receive_10x5_answer(cx, state).await
|
||||
}
|
||||
Dialogue::ReceiveGandalfAlternativeName(state) => {
|
||||
receive_gandalf_alternative_name(cx, state).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue