mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Add BotDialogue
This commit is contained in:
parent
e6534ac1b8
commit
008e8505c5
6 changed files with 36 additions and 22 deletions
|
@ -10,10 +10,11 @@ edition = "2018"
|
|||
log = "0.4.8"
|
||||
tokio = "0.2.9"
|
||||
pretty_env_logger = "0.4.0"
|
||||
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 = "ponos" }
|
||||
teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" }
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
|
@ -1,7 +1,5 @@
|
|||
use teloxide::prelude::*;
|
||||
|
||||
use super::transitions::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct StartState;
|
||||
|
||||
|
@ -30,20 +28,3 @@ up!(
|
|||
Receive10x5AnswerState + [_10x5_answer: u8] -> ReceiveGandalfAlternativeNameState,
|
||||
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),
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
|
|
14
src/dispatching/dialogue/bot_dialogue.rs
Normal file
14
src/dispatching/dialogue/bot_dialogue.rs
Normal 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>>;
|
||||
}
|
|
@ -42,6 +42,7 @@
|
|||
|
||||
#![allow(clippy::type_complexity)]
|
||||
|
||||
mod bot_dialogue;
|
||||
mod dialogue_dispatcher;
|
||||
mod dialogue_dispatcher_handler;
|
||||
mod dialogue_stage;
|
||||
|
@ -50,6 +51,7 @@ mod get_chat_id;
|
|||
mod storage;
|
||||
|
||||
use crate::{requests::ResponseResult, types::Message};
|
||||
pub use bot_dialogue::BotDialogue;
|
||||
pub use dialogue_dispatcher::DialogueDispatcher;
|
||||
pub use dialogue_dispatcher_handler::DialogueDispatcherHandler;
|
||||
pub use dialogue_stage::{exit, next, DialogueStage};
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
pub use crate::{
|
||||
dispatching::{
|
||||
dialogue::{
|
||||
exit, next, DialogueDispatcher, DialogueStage, DialogueWithCx,
|
||||
GetChatId, TransitionIn, TransitionOut,
|
||||
exit, next, BotDialogue, DialogueDispatcher, DialogueStage,
|
||||
DialogueWithCx, GetChatId, TransitionIn, TransitionOut,
|
||||
},
|
||||
Dispatcher, DispatcherHandlerRx, DispatcherHandlerRxExt, UpdateWithCx,
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue