2020-07-25 18:37:58 +02:00
|
|
|
// This is a bot that asks you three questions, e.g. a simple test.
|
2020-02-13 18:38:05 +01:00
|
|
|
//
|
|
|
|
// # Example
|
|
|
|
// ```
|
2020-07-26 09:18:29 +02:00
|
|
|
// - Hey
|
|
|
|
// - Let's start! What's your full name?
|
|
|
|
// - Gandalf the Grey
|
|
|
|
// - How old are you?
|
|
|
|
// - 223
|
|
|
|
// - What's your location?
|
|
|
|
// - Middle-earth
|
|
|
|
// - Full name: Gandalf the Grey
|
|
|
|
// Age: 223
|
|
|
|
// Location: Middle-earth
|
2020-02-13 18:38:05 +01:00
|
|
|
// ```
|
2022-03-26 09:57:30 +01:00
|
|
|
use teloxide::{dispatching::dialogue::InMemStorage, prelude::*};
|
2020-02-13 18:38:05 +01:00
|
|
|
|
2022-02-02 21:36:36 +01:00
|
|
|
type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
2022-03-24 12:30:36 +01:00
|
|
|
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
2020-02-12 10:55:36 +01:00
|
|
|
|
2022-06-30 22:02:45 +02:00
|
|
|
#[derive(Clone, Default)]
|
2021-12-27 16:12:31 +01:00
|
|
|
pub enum State {
|
2022-06-30 22:02:45 +02:00
|
|
|
#[default]
|
2021-12-27 16:12:31 +01:00
|
|
|
Start,
|
|
|
|
ReceiveFullName,
|
2022-06-30 22:02:45 +02:00
|
|
|
ReceiveAge {
|
|
|
|
full_name: String,
|
|
|
|
},
|
|
|
|
ReceiveLocation {
|
|
|
|
full_name: String,
|
|
|
|
age: u8,
|
|
|
|
},
|
2021-12-27 16:12:31 +01:00
|
|
|
}
|
2020-01-29 23:54:40 +01:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2022-03-21 15:53:59 +01:00
|
|
|
pretty_env_logger::init();
|
2022-04-26 21:00:08 +02:00
|
|
|
log::info!("Starting dialogue bot...");
|
2020-01-29 23:54:40 +01:00
|
|
|
|
2022-09-29 05:37:20 +02:00
|
|
|
let bot = Bot::from_env();
|
2020-02-13 18:23:22 +01:00
|
|
|
|
2022-02-04 15:09:53 +01:00
|
|
|
Dispatcher::builder(
|
2022-01-26 10:51:51 +01:00
|
|
|
bot,
|
2022-02-02 21:36:36 +01:00
|
|
|
Update::filter_message()
|
2022-02-04 15:00:21 +01:00
|
|
|
.enter_dialogue::<Message, InMemStorage<State>, State>()
|
2022-04-27 11:16:34 +02:00
|
|
|
.branch(dptree::case![State::Start].endpoint(start))
|
|
|
|
.branch(dptree::case![State::ReceiveFullName].endpoint(receive_full_name))
|
|
|
|
.branch(dptree::case![State::ReceiveAge { full_name }].endpoint(receive_age))
|
2022-03-26 09:57:30 +01:00
|
|
|
.branch(
|
2022-04-27 11:16:34 +02:00
|
|
|
dptree::case![State::ReceiveLocation { full_name, age }].endpoint(receive_location),
|
2022-03-26 09:57:30 +01:00
|
|
|
),
|
2022-01-26 10:51:51 +01:00
|
|
|
)
|
2022-02-02 21:36:36 +01:00
|
|
|
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
2022-07-21 10:36:57 +02:00
|
|
|
.enable_ctrlc_handler()
|
2022-02-01 23:51:36 +01:00
|
|
|
.build()
|
2022-01-26 10:51:51 +01:00
|
|
|
.dispatch()
|
|
|
|
.await;
|
2020-01-29 23:54:40 +01:00
|
|
|
}
|
2020-07-26 19:16:49 +02:00
|
|
|
|
2022-10-03 13:54:06 +02:00
|
|
|
async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
2022-02-04 14:45:35 +01:00
|
|
|
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
2022-02-02 10:25:23 +01:00
|
|
|
dialogue.update(State::ReceiveFullName).await?;
|
2021-12-27 16:12:31 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-03 13:54:06 +02:00
|
|
|
async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
2022-02-03 11:32:11 +01:00
|
|
|
match msg.text() {
|
|
|
|
Some(text) => {
|
2022-02-04 14:45:35 +01:00
|
|
|
bot.send_message(msg.chat.id, "How old are you?").await?;
|
2022-02-03 11:32:11 +01:00
|
|
|
dialogue.update(State::ReceiveAge { full_name: text.into() }).await?;
|
|
|
|
}
|
|
|
|
None => {
|
2022-02-04 14:45:35 +01:00
|
|
|
bot.send_message(msg.chat.id, "Send me plain text.").await?;
|
2022-02-03 11:32:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 16:12:31 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-26 09:57:30 +01:00
|
|
|
async fn receive_age(
|
2022-09-29 05:37:20 +02:00
|
|
|
bot: Bot,
|
2022-02-02 10:40:22 +01:00
|
|
|
dialogue: MyDialogue,
|
2022-04-02 21:50:53 +02:00
|
|
|
full_name: String, // Available from `State::ReceiveAge`.
|
2022-10-03 13:54:06 +02:00
|
|
|
msg: Message,
|
2022-03-24 12:30:36 +01:00
|
|
|
) -> HandlerResult {
|
2022-02-04 04:58:56 +01:00
|
|
|
match msg.text().map(|text| text.parse::<u8>()) {
|
|
|
|
Some(Ok(age)) => {
|
2022-02-04 14:45:35 +01:00
|
|
|
bot.send_message(msg.chat.id, "What's your location?").await?;
|
2022-02-04 04:58:56 +01:00
|
|
|
dialogue.update(State::ReceiveLocation { full_name, age }).await?;
|
|
|
|
}
|
|
|
|
_ => {
|
2022-02-04 14:45:35 +01:00
|
|
|
bot.send_message(msg.chat.id, "Send me a number.").await?;
|
2021-12-27 16:12:31 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-03 11:32:11 +01:00
|
|
|
|
2021-12-27 16:12:31 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-26 09:57:30 +01:00
|
|
|
async fn receive_location(
|
2022-09-29 05:37:20 +02:00
|
|
|
bot: Bot,
|
2022-02-02 10:40:22 +01:00
|
|
|
dialogue: MyDialogue,
|
2022-02-04 15:21:16 +01:00
|
|
|
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
2022-10-03 13:54:06 +02:00
|
|
|
msg: Message,
|
2022-03-24 12:30:36 +01:00
|
|
|
) -> HandlerResult {
|
2022-02-03 11:32:11 +01:00
|
|
|
match msg.text() {
|
|
|
|
Some(location) => {
|
2022-10-03 13:07:38 +02:00
|
|
|
let report = format!("Full name: {full_name}\nAge: {age}\nLocation: {location}");
|
|
|
|
bot.send_message(msg.chat.id, report).await?;
|
2022-02-03 11:32:11 +01:00
|
|
|
dialogue.exit().await?;
|
|
|
|
}
|
|
|
|
None => {
|
2022-02-04 14:45:35 +01:00
|
|
|
bot.send_message(msg.chat.id, "Send me plain text.").await?;
|
2022-02-03 11:32:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 16:12:31 +01:00
|
|
|
Ok(())
|
|
|
|
}
|