2022-04-22 19:50:19 +02:00
|
|
|
// This example demonstrates how to deal with messages and callback queries
|
|
|
|
// within a single dialogue.
|
2022-04-22 19:42:50 +02:00
|
|
|
//
|
|
|
|
// # Example
|
|
|
|
// ```
|
|
|
|
// - /start
|
|
|
|
// - Let's start! What's your full name?
|
|
|
|
// - John Doe
|
|
|
|
// - Select a product:
|
|
|
|
// [Apple, Banana, Orange, Potato]
|
|
|
|
// - <A user selects "Banana">
|
|
|
|
// - John Doe, product 'Banana' has been purchased successfully!
|
|
|
|
// ```
|
|
|
|
|
|
|
|
use teloxide::{
|
2022-07-21 12:53:52 +02:00
|
|
|
dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler},
|
2022-04-22 19:42:50 +02:00
|
|
|
prelude::*,
|
|
|
|
types::{InlineKeyboardButton, InlineKeyboardMarkup},
|
|
|
|
utils::command::BotCommands,
|
|
|
|
};
|
|
|
|
|
|
|
|
type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
|
|
|
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
|
|
|
|
2022-06-30 22:02:45 +02:00
|
|
|
#[derive(Clone, Default)]
|
2022-04-22 19:42:50 +02:00
|
|
|
pub enum State {
|
2022-06-30 22:02:45 +02:00
|
|
|
#[default]
|
2022-04-22 19:42:50 +02:00
|
|
|
Start,
|
|
|
|
ReceiveFullName,
|
2022-06-30 22:02:45 +02:00
|
|
|
ReceiveProductChoice {
|
|
|
|
full_name: String,
|
|
|
|
},
|
2022-04-22 19:42:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(BotCommands, Clone)]
|
|
|
|
#[command(rename = "lowercase", description = "These commands are supported:")]
|
|
|
|
enum Command {
|
|
|
|
#[command(description = "display this text.")]
|
|
|
|
Help,
|
|
|
|
#[command(description = "start the purchase procedure.")]
|
|
|
|
Start,
|
2022-04-24 21:23:05 +02:00
|
|
|
#[command(description = "cancel the purchase procedure.")]
|
|
|
|
Cancel,
|
2022-04-22 19:42:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
pretty_env_logger::init();
|
2022-04-25 15:16:05 +02:00
|
|
|
log::info!("Starting purchase bot...");
|
2022-04-22 19:42:50 +02:00
|
|
|
|
2022-09-29 05:37:20 +02:00
|
|
|
let bot = Bot::from_env();
|
2022-04-22 19:42:50 +02:00
|
|
|
|
2022-04-26 16:50:55 +02:00
|
|
|
Dispatcher::builder(bot, schema())
|
|
|
|
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
2022-07-21 10:36:57 +02:00
|
|
|
.enable_ctrlc_handler()
|
2022-04-26 16:50:55 +02:00
|
|
|
.build()
|
|
|
|
.dispatch()
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn schema() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
|
2022-07-21 12:53:52 +02:00
|
|
|
use dptree::case;
|
|
|
|
|
2022-04-25 15:16:05 +02:00
|
|
|
let command_handler = teloxide::filter_command::<Command, _>()
|
2022-04-24 21:30:01 +02:00
|
|
|
.branch(
|
2022-07-21 12:53:52 +02:00
|
|
|
case![State::Start]
|
|
|
|
.branch(case![Command::Help].endpoint(help))
|
|
|
|
.branch(case![Command::Start].endpoint(start)),
|
2022-04-24 21:30:01 +02:00
|
|
|
)
|
2022-07-21 12:53:52 +02:00
|
|
|
.branch(case![Command::Cancel].endpoint(cancel));
|
2022-04-24 21:30:01 +02:00
|
|
|
|
2022-04-25 15:20:47 +02:00
|
|
|
let message_handler = Update::filter_message()
|
|
|
|
.branch(command_handler)
|
2022-07-21 12:53:52 +02:00
|
|
|
.branch(case![State::ReceiveFullName].endpoint(receive_full_name))
|
2022-04-25 15:20:47 +02:00
|
|
|
.branch(dptree::endpoint(invalid_state));
|
|
|
|
|
2022-07-19 15:34:35 +02:00
|
|
|
let callback_query_handler = Update::filter_callback_query().branch(
|
2022-07-21 12:53:52 +02:00
|
|
|
case![State::ReceiveProductChoice { full_name }].endpoint(receive_product_selection),
|
2022-04-25 15:20:47 +02:00
|
|
|
);
|
|
|
|
|
2022-07-21 12:53:52 +02:00
|
|
|
dialogue::enter::<Update, InMemStorage<State>, State, _>()
|
2022-04-26 16:50:55 +02:00
|
|
|
.branch(message_handler)
|
|
|
|
.branch(callback_query_handler)
|
2022-04-22 19:42:50 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 13:54:06 +02:00
|
|
|
async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
2022-04-24 21:23:05 +02:00
|
|
|
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
|
|
|
dialogue.update(State::ReceiveFullName).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-29 05:37:20 +02:00
|
|
|
async fn help(bot: Bot, msg: Message) -> HandlerResult {
|
2022-04-24 21:23:05 +02:00
|
|
|
bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-22 19:42:50 +02:00
|
|
|
|
2022-10-03 13:54:06 +02:00
|
|
|
async fn cancel(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
2022-04-24 21:23:05 +02:00
|
|
|
bot.send_message(msg.chat.id, "Cancelling the dialogue.").await?;
|
|
|
|
dialogue.exit().await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-29 05:37:20 +02:00
|
|
|
async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
|
2022-04-24 21:23:05 +02:00
|
|
|
bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see the usage.")
|
|
|
|
.await?;
|
2022-04-22 19:42:50 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-03 13:54:06 +02:00
|
|
|
async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
2022-04-22 19:42:50 +02:00
|
|
|
match msg.text().map(ToOwned::to_owned) {
|
|
|
|
Some(full_name) => {
|
2022-04-25 14:44:27 +02:00
|
|
|
let products = ["Apple", "Banana", "Orange", "Potato"]
|
|
|
|
.map(|product| InlineKeyboardButton::callback(product, product));
|
2022-04-22 19:42:50 +02:00
|
|
|
|
2022-04-24 21:38:55 +02:00
|
|
|
bot.send_message(msg.chat.id, "Select a product:")
|
|
|
|
.reply_markup(InlineKeyboardMarkup::new([products]))
|
|
|
|
.await?;
|
2022-04-23 18:45:43 +02:00
|
|
|
dialogue.update(State::ReceiveProductChoice { full_name }).await?;
|
2022-04-22 19:42:50 +02:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bot.send_message(msg.chat.id, "Please, send me your full name.").await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_product_selection(
|
2022-09-29 05:37:20 +02:00
|
|
|
bot: Bot,
|
2022-04-22 19:42:50 +02:00
|
|
|
dialogue: MyDialogue,
|
2022-10-03 13:54:06 +02:00
|
|
|
full_name: String, // Available from `State::ReceiveProductChoice`.
|
|
|
|
q: CallbackQuery,
|
2022-04-22 19:42:50 +02:00
|
|
|
) -> HandlerResult {
|
|
|
|
if let Some(product) = &q.data {
|
2022-04-24 19:11:47 +02:00
|
|
|
bot.send_message(
|
|
|
|
dialogue.chat_id(),
|
|
|
|
format!("{full_name}, product '{product}' has been purchased successfully!"),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
dialogue.exit().await?;
|
2022-04-22 19:42:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|