Add BotDialogue

This commit is contained in:
Temirkhan Myrzamadi 2020-07-24 18:04:20 +06:00
parent e6534ac1b8
commit 008e8505c5
6 changed files with 36 additions and 22 deletions

View file

@ -10,10 +10,11 @@ edition = "2018"
log = "0.4.8" log = "0.4.8"
tokio = "0.2.9" tokio = "0.2.9"
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
futures = "0.3.5"
smart-default = "0.6.0" smart-default = "0.6.0"
derive_more = "0.99.9" derive_more = "0.99.9"
teloxide = { path = "../../" } teloxide = { path = "../../" }
teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "ponos" } teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" }
[profile.release] [profile.release]
lto = true lto = true

View file

@ -1,7 +1,5 @@
use teloxide::prelude::*; use teloxide::prelude::*;
use super::transitions::*;
#[derive(Default)] #[derive(Default)]
pub struct StartState; pub struct StartState;
@ -30,20 +28,3 @@ up!(
Receive10x5AnswerState + [_10x5_answer: u8] -> ReceiveGandalfAlternativeNameState, Receive10x5AnswerState + [_10x5_answer: u8] -> ReceiveGandalfAlternativeNameState,
ReceiveGandalfAlternativeNameState + [gandalf_alternative_name: String] -> ExitState, ReceiveGandalfAlternativeNameState + [gandalf_alternative_name: String] -> ExitState,
); );
#[bot_dialogue]
#[derive(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),
}

View file

@ -55,3 +55,19 @@ 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),
}

View file

@ -0,0 +1,14 @@
use crate::{
dispatching::{dialogue::TransitionOut, UpdateWithCx},
types::Message,
};
use futures::future::BoxFuture;
/// Represents a dialogue FSM.
pub trait BotDialogue: Default {
/// Turns itself into another state, depending on the input message.
fn dispatch(
self,
cx: UpdateWithCx<Message>,
) -> BoxFuture<'static, TransitionOut<Self>>;
}

View file

@ -42,6 +42,7 @@
#![allow(clippy::type_complexity)] #![allow(clippy::type_complexity)]
mod bot_dialogue;
mod dialogue_dispatcher; mod dialogue_dispatcher;
mod dialogue_dispatcher_handler; mod dialogue_dispatcher_handler;
mod dialogue_stage; mod dialogue_stage;
@ -50,6 +51,7 @@ mod get_chat_id;
mod storage; mod storage;
use crate::{requests::ResponseResult, types::Message}; use crate::{requests::ResponseResult, types::Message};
pub use bot_dialogue::BotDialogue;
pub use dialogue_dispatcher::DialogueDispatcher; pub use dialogue_dispatcher::DialogueDispatcher;
pub use dialogue_dispatcher_handler::DialogueDispatcherHandler; pub use dialogue_dispatcher_handler::DialogueDispatcherHandler;
pub use dialogue_stage::{exit, next, DialogueStage}; pub use dialogue_stage::{exit, next, DialogueStage};

View file

@ -3,8 +3,8 @@
pub use crate::{ pub use crate::{
dispatching::{ dispatching::{
dialogue::{ dialogue::{
exit, next, DialogueDispatcher, DialogueStage, DialogueWithCx, exit, next, BotDialogue, DialogueDispatcher, DialogueStage,
GetChatId, TransitionIn, TransitionOut, DialogueWithCx, GetChatId, TransitionIn, TransitionOut,
}, },
Dispatcher, DispatcherHandlerRx, DispatcherHandlerRxExt, UpdateWithCx, Dispatcher, DispatcherHandlerRx, DispatcherHandlerRxExt, UpdateWithCx,
}, },