mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-14 11:44:04 +01:00
Add the state! macro
This commit is contained in:
parent
afb6677a1e
commit
0ae2d975df
4 changed files with 22 additions and 5 deletions
|
@ -87,7 +87,7 @@ async fn start(mut ctx: Ctx) -> Res {
|
|||
ctx.answer("Let's start! First, what's your full name?")
|
||||
.send()
|
||||
.await?;
|
||||
ctx.dialogue.state = State::FullName;
|
||||
state!(ctx, State::FullName);
|
||||
next(ctx.dialogue)
|
||||
}
|
||||
|
||||
|
@ -96,7 +96,7 @@ async fn full_name(mut ctx: Ctx) -> Res {
|
|||
.send()
|
||||
.await?;
|
||||
ctx.dialogue.data.full_name = Some(ctx.update.text().unwrap().to_owned());
|
||||
ctx.dialogue.state = State::Age;
|
||||
state!(ctx, State::Age);
|
||||
next(ctx.dialogue)
|
||||
}
|
||||
|
||||
|
@ -105,7 +105,7 @@ async fn age(mut ctx: Ctx) -> Res {
|
|||
Ok(ok) => {
|
||||
send_favourite_music_types(&ctx).await?;
|
||||
ctx.dialogue.data.age = Some(ok);
|
||||
ctx.dialogue.state = State::FavouriteMusic;
|
||||
state!(ctx, State::FavouriteMusic);
|
||||
}
|
||||
Err(_) => ctx
|
||||
.answer("Oh, please, enter a number!")
|
||||
|
|
|
@ -20,6 +20,21 @@ pub struct DialogueHandlerCtx<Upd, State, T> {
|
|||
pub dialogue: Dialogue<State, T>,
|
||||
}
|
||||
|
||||
/// Sets a new state.
|
||||
///
|
||||
/// Use it like this: `state!(ctx, State::RequestAge)`, where `ctx` is
|
||||
/// [`DialogueHandlerCtx<Upd, State, T>`] and `State::RequestAge` is of type
|
||||
/// `State`.
|
||||
///
|
||||
/// [`DialogueHandlerCtx<Upd, State, T>`]:
|
||||
/// crate::dispatching::dialogue::DialogueHandlerCtx
|
||||
#[macro_export]
|
||||
macro_rules! state {
|
||||
($ctx:ident, $state:expr) => {
|
||||
$ctx.dialogue.state = $state;
|
||||
};
|
||||
}
|
||||
|
||||
impl<Upd, State, T> GetChatId for DialogueHandlerCtx<Upd, State, T>
|
||||
where
|
||||
Upd: GetChatId,
|
||||
|
|
|
@ -9,6 +9,7 @@ pub use crate::{
|
|||
Dispatcher, DispatcherHandlerCtx,
|
||||
},
|
||||
requests::{Request, ResponseResult},
|
||||
state,
|
||||
types::Message,
|
||||
Bot, RequestError,
|
||||
};
|
||||
|
|
|
@ -39,8 +39,9 @@
|
|||
//! assert_eq!(args, vec!["3", "hours"]);
|
||||
//! ```
|
||||
//!
|
||||
//! [`parse_command`]: crate::utils::parse_command
|
||||
//! [`parse_command_with_prefix`]: crate::utils::parse_command_with_prefix
|
||||
//! [`parse_command`]: crate::utils::command::parse_command
|
||||
//! [`parse_command_with_prefix`]:
|
||||
//! crate::utils::command::parse_command_with_prefix
|
||||
|
||||
pub use teloxide_macros::BotCommand;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue