teloxide/examples/dialogue.rs

111 lines
2.8 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
// ```
2022-02-02 21:36:36 +01:00
use teloxide::{dispatching2::dialogue::InMemStorage, macros::DialogueState, prelude2::*};
2022-02-02 21:36:36 +01:00
type MyDialogue = Dialogue<State, InMemStorage<State>>;
2022-02-02 21:36:36 +01:00
#[derive(DialogueState, Clone)]
#[handler_out(anyhow::Result<()>)]
2021-12-27 16:12:31 +01:00
pub enum State {
2021-12-29 12:33:51 +01:00
#[handler(handle_start)]
2021-12-27 16:12:31 +01:00
Start,
2021-12-29 12:33:51 +01:00
#[handler(handle_receive_full_name)]
2021-12-27 16:12:31 +01:00
ReceiveFullName,
2021-12-29 12:33:51 +01:00
#[handler(handle_receive_age)]
ReceiveAge { full_name: String },
2021-12-29 12:33:51 +01:00
#[handler(handle_receive_location)]
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() {
2020-02-13 18:23:22 +01:00
teloxide::enable_logging!();
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
DispatcherBuilder::new(
2022-01-26 10:51:51 +01:00
bot,
2022-02-02 21:36:36 +01:00
Update::filter_message()
.add_dialogue::<Message, InMemStorage<State>, State>()
2022-02-02 01:28:32 +01:00
.dispatch_by::<State>(),
2022-01-26 10:51:51 +01:00
)
2022-02-02 21:36:36 +01:00
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.build()
2022-01-26 10:51:51 +01:00
.dispatch()
.await;
2020-01-29 23:54:40 +01:00
}
2022-02-02 01:28:32 +01:00
async fn handle_start(
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
2022-02-02 01:28:32 +01:00
) -> anyhow::Result<()> {
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 handle_receive_full_name(
2022-02-02 01:28:32 +01:00
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
2021-12-27 16:12:31 +01:00
) -> anyhow::Result<()> {
bot.send_message(msg.chat_id(), "How old are you?").await?;
dialogue.update(State::ReceiveAge { full_name: msg.text().unwrap().into() }).await?;
2021-12-27 16:12:31 +01:00
Ok(())
}
async fn handle_receive_age(
2022-02-02 01:28:32 +01:00
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
(full_name,): (String,),
2021-12-27 16:12:31 +01:00
) -> anyhow::Result<()> {
match msg.text().unwrap().parse::<u8>() {
2021-12-27 16:12:31 +01:00
Ok(age) => {
bot.send_message(msg.chat_id(), "What's your location?").await?;
dialogue.update(State::ReceiveLocation { full_name, age }).await?;
2021-12-27 16:12:31 +01:00
}
_ => {
bot.send_message(msg.chat_id(), "Send me a number.").await?;
2021-12-27 16:12:31 +01:00
}
}
Ok(())
}
async fn handle_receive_location(
2022-02-02 01:28:32 +01:00
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
(full_name, age): (String, u8),
2021-12-27 16:12:31 +01:00
) -> anyhow::Result<()> {
let location = msg.text().unwrap();
let message = format!("Full name: {}\nAge: {}\nLocation: {}", full_name, age, location);
bot.send_message(msg.chat_id(), message).await?;
2021-12-27 16:12:31 +01:00
dialogue.exit().await?;
Ok(())
}