mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-20 13:59:00 +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"
|
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
|
|
@ -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),
|
|
||||||
}
|
|
||||||
|
|
|
@ -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)]
|
#![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};
|
||||||
|
|
|
@ -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,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue