teloxide/examples/dialogue_bot/src/main.rs

117 lines
2.9 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
// ```
2021-12-27 16:12:31 +01:00
use teloxide::{
dispatching2::dialogue::{serializer::Json, SqliteStorage},
2021-12-29 12:33:51 +01:00
macros::DialogueState,
2021-12-27 16:12:31 +01:00
prelude::*,
};
2021-12-27 16:12:31 +01:00
// FIXME: naming
type MyBot = AutoSend<Bot>;
type Store = SqliteStorage<Json>;
type BotDialogue = Dialogue<State, Store>;
2021-12-29 12:33:51 +01:00
#[derive(DialogueState, Clone, serde::Serialize, serde::Deserialize)]
#[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)]
2021-12-28 17:24:56 +01:00
ReceiveAge(String),
2021-12-29 12:33:51 +01:00
#[handler(handle_receive_location)]
2021-12-28 17:24:56 +01:00
ReceiveLocation(ReceiveLocation),
}
#[derive(Clone, serde::Serialize, serde::Deserialize)]
2021-12-29 12:33:51 +01:00
pub struct 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();
2021-12-27 16:12:31 +01:00
let storage = SqliteStorage::open("db.sqlite", Json).await.unwrap();
2020-02-13 18:23:22 +01:00
2021-12-27 16:12:31 +01:00
Dispatcher::new(bot)
.dependencies(dptree::deps![storage])
2021-12-29 12:33:51 +01:00
.messages_handler(|h| h.add_dialogue::<Message, Store, State>().dispatch_by::<State>())
2021-12-27 16:12:31 +01:00
.dispatch()
.await;
2020-01-29 23:54:40 +01:00
}
2021-12-27 16:12:31 +01:00
async fn handle_start(bot: MyBot, mes: Message, dialogue: BotDialogue) -> anyhow::Result<()> {
bot.send_message(mes.chat_id(), "Let's start! What's your full name?").await?;
dialogue.next(State::ReceiveFullName).await?;
Ok(())
}
async fn handle_receive_full_name(
bot: MyBot,
mes: Message,
dialogue: BotDialogue,
) -> anyhow::Result<()> {
bot.send_message(mes.chat_id(), "How old are you?").await?;
2021-12-28 17:24:56 +01:00
dialogue.next(State::ReceiveAge(mes.text().unwrap().into())).await?;
2021-12-27 16:12:31 +01:00
Ok(())
}
async fn handle_receive_age(
bot: MyBot,
mes: Message,
dialogue: BotDialogue,
full_name: String,
) -> anyhow::Result<()> {
match mes.text().unwrap().parse::<u8>() {
Ok(age) => {
bot.send_message(mes.chat_id(), "What's your location?").await?;
2021-12-28 17:24:56 +01:00
dialogue.next(State::ReceiveLocation(ReceiveLocation { full_name, age })).await?;
2021-12-27 16:12:31 +01:00
}
_ => {
bot.send_message(mes.chat_id(), "Send me a number.").await?;
}
}
Ok(())
}
async fn handle_receive_location(
bot: MyBot,
mes: Message,
dialogue: BotDialogue,
2021-12-29 12:33:51 +01:00
state: ReceiveLocation,
2021-12-27 16:12:31 +01:00
) -> anyhow::Result<()> {
let location = mes.text().unwrap();
2021-12-29 12:33:51 +01:00
let message =
format!("Full name: {}\nAge: {}\nLocation: {}", state.full_name, state.age, location);
2021-12-27 16:12:31 +01:00
bot.send_message(mes.chat_id(), message).await?;
dialogue.exit().await?;
Ok(())
}