mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Merge pull request #602 from teloxide/restrict-commands-by-state-purchase
Restrict all purchase bot's commands by `State::Start`
This commit is contained in:
commit
ae480cae23
2 changed files with 45 additions and 32 deletions
|
@ -42,6 +42,8 @@ enum Command {
|
||||||
Help,
|
Help,
|
||||||
#[command(description = "start the purchase procedure.")]
|
#[command(description = "start the purchase procedure.")]
|
||||||
Start,
|
Start,
|
||||||
|
#[command(description = "cancel the purchase procedure.")]
|
||||||
|
Cancel,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -51,13 +53,22 @@ async fn main() {
|
||||||
|
|
||||||
let bot = Bot::from_env().auto_send();
|
let bot = Bot::from_env().auto_send();
|
||||||
|
|
||||||
|
let command_handler = dptree::entry()
|
||||||
|
.filter_command::<Command>()
|
||||||
|
.branch(
|
||||||
|
teloxide::handler![State::Start]
|
||||||
|
.branch(teloxide::handler![Command::Help].endpoint(help))
|
||||||
|
.branch(teloxide::handler![Command::Start].endpoint(start)),
|
||||||
|
)
|
||||||
|
.branch(teloxide::handler![Command::Cancel].endpoint(cancel));
|
||||||
|
|
||||||
Dispatcher::builder(
|
Dispatcher::builder(
|
||||||
bot,
|
bot,
|
||||||
dialogue::enter::<Update, InMemStorage<State>, State, _>()
|
dialogue::enter::<Update, InMemStorage<State>, State, _>()
|
||||||
.branch(
|
.branch(
|
||||||
Update::filter_message()
|
Update::filter_message()
|
||||||
|
.branch(command_handler)
|
||||||
.branch(teloxide::handler![State::ReceiveFullName].endpoint(receive_full_name))
|
.branch(teloxide::handler![State::ReceiveFullName].endpoint(receive_full_name))
|
||||||
.branch(dptree::entry().filter_command::<Command>().endpoint(handle_command))
|
|
||||||
.branch(dptree::endpoint(invalid_state)),
|
.branch(dptree::endpoint(invalid_state)),
|
||||||
)
|
)
|
||||||
.branch(
|
.branch(
|
||||||
|
@ -74,22 +85,26 @@ async fn main() {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_command(
|
async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||||
bot: AutoSend<Bot>,
|
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
||||||
msg: Message,
|
dialogue.update(State::ReceiveFullName).await?;
|
||||||
cmd: Command,
|
Ok(())
|
||||||
dialogue: MyDialogue,
|
}
|
||||||
) -> HandlerResult {
|
|
||||||
match cmd {
|
|
||||||
Command::Help => {
|
|
||||||
bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
|
|
||||||
}
|
|
||||||
Command::Start => {
|
|
||||||
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
|
||||||
dialogue.update(State::ReceiveFullName).await?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
async fn help(bot: AutoSend<Bot>, msg: Message) -> HandlerResult {
|
||||||
|
bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cancel(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||||
|
bot.send_message(msg.chat.id, "Cancelling the dialogue.").await?;
|
||||||
|
dialogue.exit().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn invalid_state(bot: AutoSend<Bot>, msg: Message) -> HandlerResult {
|
||||||
|
bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see the usage.")
|
||||||
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,13 +115,13 @@ async fn receive_full_name(
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
match msg.text().map(ToOwned::to_owned) {
|
match msg.text().map(ToOwned::to_owned) {
|
||||||
Some(full_name) => {
|
Some(full_name) => {
|
||||||
let products = InlineKeyboardMarkup::default().append_row(
|
let products = ["Apple", "Banana", "Orange", "Potato"].map(|product| {
|
||||||
vec!["Apple", "Banana", "Orange", "Potato"].into_iter().map(|product| {
|
InlineKeyboardButton::callback(product.to_owned(), product.to_owned())
|
||||||
InlineKeyboardButton::callback(product.to_owned(), product.to_owned())
|
});
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
bot.send_message(msg.chat.id, "Select a product:").reply_markup(products).await?;
|
bot.send_message(msg.chat.id, "Select a product:")
|
||||||
|
.reply_markup(InlineKeyboardMarkup::new([products]))
|
||||||
|
.await?;
|
||||||
dialogue.update(State::ReceiveProductChoice { full_name }).await?;
|
dialogue.update(State::ReceiveProductChoice { full_name }).await?;
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
|
@ -134,9 +149,3 @@ async fn receive_product_selection(
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn invalid_state(bot: AutoSend<Bot>, msg: Message) -> HandlerResult {
|
|
||||||
bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see the usage.")
|
|
||||||
.await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
|
@ -214,11 +214,13 @@ where
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Perform a dialogue FSM transition.
|
/// Filters an enumeration, passing its payload forwards.
|
||||||
///
|
///
|
||||||
/// This macro expands to a [`dptree::Handler`] that filters your dialogue
|
/// This macro expands to a [`dptree::Handler`] that acts on your enumeration
|
||||||
/// state: if the state enumeration is of a certain variant, the execution
|
/// type: if the enumeration is of a certain variant, the execution continues;
|
||||||
/// continues; otherwise, `dptree` will try the next branch.
|
/// otherwise, `dptree` will try the next branch. This is very useful for
|
||||||
|
/// dialogue FSM transitions and Telegram command filtering; for a complete
|
||||||
|
/// example, please see [`examples/purchase.rs`].
|
||||||
///
|
///
|
||||||
/// Variants can take the following forms:
|
/// Variants can take the following forms:
|
||||||
///
|
///
|
||||||
|
@ -243,6 +245,8 @@ where
|
||||||
/// ## Dependency requirements
|
/// ## Dependency requirements
|
||||||
///
|
///
|
||||||
/// - Your dialogue state enumeration `State`.
|
/// - Your dialogue state enumeration `State`.
|
||||||
|
///
|
||||||
|
/// [`examples/purchase.rs`]: https://github.com/teloxide/teloxide/blob/master/examples/purchase.rs
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! handler {
|
macro_rules! handler {
|
||||||
($($variant:ident)::+) => {
|
($($variant:ident)::+) => {
|
||||||
|
|
Loading…
Reference in a new issue