mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-08 11:23:54 +01:00
60df0a5cf8
Remove useless stuff, some cleanup, u know.
88 lines
2.6 KiB
Rust
88 lines
2.6 KiB
Rust
use teloxide::{
|
|
dispatching2::dialogue::{serializer::Json, SqliteStorage, Storage},
|
|
prelude2::*,
|
|
RequestError,
|
|
};
|
|
use thiserror::Error;
|
|
|
|
type Store = SqliteStorage<Json>;
|
|
// FIXME: naming
|
|
type MyDialogue = Dialogue<BotDialogue, Store>;
|
|
type StorageError = <SqliteStorage<Json> as Storage<BotDialogue>>::Error;
|
|
|
|
#[derive(Debug, Error)]
|
|
enum Error {
|
|
#[error("error from Telegram: {0}")]
|
|
TelegramError(#[from] RequestError),
|
|
#[error("error from storage: {0}")]
|
|
StorageError(#[from] StorageError),
|
|
}
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize)]
|
|
pub enum BotDialogue {
|
|
Start,
|
|
GotNumber(i32),
|
|
}
|
|
|
|
impl Default for BotDialogue {
|
|
fn default() -> Self {
|
|
Self::Start
|
|
}
|
|
}
|
|
|
|
async fn handle_message(
|
|
bot: AutoSend<Bot>,
|
|
mes: Message,
|
|
dialogue: MyDialogue,
|
|
) -> Result<(), Error> {
|
|
match mes.text() {
|
|
None => {
|
|
bot.send_message(mes.chat.id, "Send me a text message.").await?;
|
|
}
|
|
Some(ans) => {
|
|
let state = dialogue.get_or_default().await?;
|
|
match state {
|
|
BotDialogue::Start => {
|
|
if let Ok(number) = ans.parse() {
|
|
dialogue.update(BotDialogue::GotNumber(number)).await?;
|
|
bot.send_message(
|
|
mes.chat.id,
|
|
format!("Remembered number {}. Now use /get or /reset", number),
|
|
)
|
|
.await?;
|
|
} else {
|
|
bot.send_message(mes.chat.id, "Please, send me a number").await?;
|
|
}
|
|
}
|
|
BotDialogue::GotNumber(num) => {
|
|
if ans.starts_with("/get") {
|
|
bot.send_message(mes.chat.id, format!("Here is your number: {}", num))
|
|
.await?;
|
|
} else if ans.starts_with("/reset") {
|
|
dialogue.reset().await?;
|
|
bot.send_message(mes.chat.id, "Resetted number").await?;
|
|
} else {
|
|
bot.send_message(mes.chat.id, "Please, send /get or /reset").await?;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
let bot = Bot::from_env().auto_send();
|
|
let storage = SqliteStorage::open("db.sqlite", Json).await.unwrap();
|
|
|
|
let handler = dptree::entry()
|
|
.add_dialogue::<Message, Store, BotDialogue>()
|
|
.branch(dptree::endpoint(handle_message));
|
|
|
|
DispatcherBuilder::new(bot, handler)
|
|
.dependencies(dptree::deps![storage])
|
|
.build()
|
|
.dispatch()
|
|
.await;
|
|
}
|