teloxide/examples/dialogue_bot/src/main.rs

57 lines
1.2 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
// ```
#![allow(clippy::trivial_regex)]
2020-05-24 13:08:40 +02:00
#![allow(dead_code)]
2020-07-25 23:10:48 +02:00
#[macro_use]
extern crate frunk;
2020-07-01 14:52:53 +02:00
mod dialogue;
2020-05-24 10:19:46 +02:00
use crate::dialogue::Dialogue;
use teloxide::prelude::*;
2020-01-29 23:54:40 +01:00
#[tokio::main]
async fn main() {
2020-02-13 09:55:46 +01:00
run().await;
}
async fn run() {
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
2020-07-31 14:47:20 +02:00
teloxide::dialogues_repl(bot, |message, dialogue| async move {
handle_message(message, dialogue).await.expect("Something wrong with the bot!")
})
.await;
2020-01-29 23:54:40 +01:00
}
2021-03-05 22:24:10 +01:00
async fn handle_message(
cx: UpdateWithCx<AutoSend<Bot>, Message>,
dialogue: Dialogue,
) -> TransitionOut<Dialogue> {
2021-03-17 20:56:41 +01:00
match cx.update.text().map(ToOwned::to_owned) {
None => {
2021-03-05 22:24:10 +01:00
cx.answer("Send me a text message.").await?;
next(dialogue)
}
Some(ans) => dialogue.react(cx, ans).await,
}
}