teloxide/examples/dialogue.rs

123 lines
3.3 KiB
Rust
Raw Normal View History

// This is a bot that asks you three questions, e.g. a simple test.
//
// # 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
// ```
use teloxide::{dispatching::dialogue::InMemStorage, prelude::*};
2022-02-02 21:36:36 +01:00
type MyDialogue = Dialogue<State, InMemStorage<State>>;
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
#[derive(Clone)]
2021-12-27 16:12:31 +01:00
pub enum State {
Start,
ReceiveFullName,
ReceiveAge { full_name: String },
ReceiveLocation { full_name: String, age: u8 },
2021-12-27 16:12:31 +01:00
}
2020-05-24 10:19:46 +02:00
2021-12-27 16:12:31 +01:00
impl Default for State {
fn default() -> Self {
Self::Start
}
}
2020-01-29 23:54:40 +01:00
#[tokio::main]
async fn main() {
pretty_env_logger::init();
log::info!("Starting dialogue_bot...");
2020-01-29 23:54:40 +01:00
2021-03-05 22:24:10 +01:00
let bot = Bot::from_env().auto_send();
2020-02-13 18:23:22 +01:00
Dispatcher::builder(
2022-01-26 10:51:51 +01:00
bot,
2022-02-02 21:36:36 +01:00
Update::filter_message()
.enter_dialogue::<Message, InMemStorage<State>, State>()
.branch(teloxide::handler![State::Start].endpoint(start))
.branch(teloxide::handler![State::ReceiveFullName].endpoint(receive_full_name))
.branch(teloxide::handler![State::ReceiveAge { full_name }].endpoint(receive_age))
.branch(
teloxide::handler![State::ReceiveLocation { full_name, age }]
.endpoint(receive_location),
),
2022-01-26 10:51:51 +01:00
)
2022-02-02 21:36:36 +01:00
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.build()
.setup_ctrlc_handler()
2022-01-26 10:51:51 +01:00
.dispatch()
.await;
2020-01-29 23:54:40 +01:00
}
async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
2022-02-04 14:45:35 +01:00
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
dialogue.update(State::ReceiveFullName).await?;
2021-12-27 16:12:31 +01:00
Ok(())
}
async fn receive_full_name(
2022-02-02 01:28:32 +01:00
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
) -> HandlerResult {
match msg.text() {
Some(text) => {
2022-02-04 14:45:35 +01:00
bot.send_message(msg.chat.id, "How old are you?").await?;
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?;
}
}
2021-12-27 16:12:31 +01:00
Ok(())
}
async fn receive_age(
2022-02-02 01:28:32 +01:00
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
full_name: String, // Available from `State::ReceiveAge`.
) -> 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
}
}
2021-12-27 16:12:31 +01:00
Ok(())
}
async fn receive_location(
2022-02-02 01:28:32 +01:00
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
) -> HandlerResult {
match msg.text() {
Some(location) => {
let message = format!("Full name: {full_name}\nAge: {age}\nLocation: {location}");
2022-02-04 14:45:35 +01:00
bot.send_message(msg.chat.id, message).await?;
dialogue.exit().await?;
}
None => {
2022-02-04 14:45:35 +01:00
bot.send_message(msg.chat.id, "Send me plain text.").await?;
}
}
2021-12-27 16:12:31 +01:00
Ok(())
}