// This is a bot that asks you three questions, e.g. a simple test. // // # Example // ``` // - 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::*}; type MyDialogue = Dialogue>; type HandlerResult = Result<(), Box>; #[derive(Clone)] pub enum State { Start, ReceiveFullName, ReceiveAge { full_name: String }, ReceiveLocation { full_name: String, age: u8 }, } impl Default for State { fn default() -> Self { Self::Start } } #[tokio::main] async fn main() { pretty_env_logger::init(); log::info!("Starting dialogue_bot..."); let bot = Bot::from_env().auto_send(); Dispatcher::builder( bot, Update::filter_message() .enter_dialogue::, 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), ), ) .dependencies(dptree::deps![InMemStorage::::new()]) .build() .setup_ctrlc_handler() .dispatch() .await; } async fn start(bot: AutoSend, msg: Message, dialogue: MyDialogue) -> HandlerResult { bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?; dialogue.update(State::ReceiveFullName).await?; Ok(()) } async fn receive_full_name( bot: AutoSend, msg: Message, dialogue: MyDialogue, ) -> HandlerResult { match msg.text() { Some(text) => { bot.send_message(msg.chat.id, "How old are you?").await?; dialogue.update(State::ReceiveAge { full_name: text.into() }).await?; } None => { bot.send_message(msg.chat.id, "Send me plain text.").await?; } } Ok(()) } async fn receive_age( bot: AutoSend, msg: Message, dialogue: MyDialogue, full_name: String, // Available from `State::ReceiveAge`. ) -> HandlerResult { match msg.text().map(|text| text.parse::()) { Some(Ok(age)) => { bot.send_message(msg.chat.id, "What's your location?").await?; dialogue.update(State::ReceiveLocation { full_name, age }).await?; } _ => { bot.send_message(msg.chat.id, "Send me a number.").await?; } } Ok(()) } async fn receive_location( bot: AutoSend, 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}"); bot.send_message(msg.chat.id, message).await?; dialogue.exit().await?; } None => { bot.send_message(msg.chat.id, "Send me plain text.").await?; } } Ok(()) }