2022-03-21 20:06:32 +06:00
|
|
|
// Set the `DB_REMEMBER_REDIS` environmental variable if you want to use Redis.
|
|
|
|
// Otherwise, the default is Sqlite.
|
2020-10-24 19:57:51 +03:00
|
|
|
|
2022-03-21 20:06:32 +06:00
|
|
|
use teloxide::{
|
2022-03-24 17:25:42 +06:00
|
|
|
dispatching::dialogue::{
|
2022-03-21 20:06:32 +06:00
|
|
|
serializer::{Bincode, Json},
|
|
|
|
ErasedStorage, RedisStorage, SqliteStorage, Storage,
|
|
|
|
},
|
2022-03-24 17:25:42 +06:00
|
|
|
prelude::*,
|
2022-03-13 19:37:11 +04:00
|
|
|
utils::command::BotCommands,
|
2022-03-21 20:06:32 +06:00
|
|
|
};
|
2020-10-24 19:57:51 +03:00
|
|
|
|
2022-03-21 20:06:32 +06:00
|
|
|
type MyDialogue = Dialogue<State, ErasedStorage<State>>;
|
|
|
|
type MyStorage = std::sync::Arc<ErasedStorage<State>>;
|
|
|
|
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
2020-10-24 19:57:51 +03:00
|
|
|
|
2022-07-01 02:02:45 +06:00
|
|
|
#[derive(Clone, Default, serde::Serialize, serde::Deserialize)]
|
2022-02-03 03:36:53 +06:00
|
|
|
pub enum State {
|
2022-07-01 02:02:45 +06:00
|
|
|
#[default]
|
2021-12-15 13:46:32 +02:00
|
|
|
Start,
|
2022-02-02 15:25:23 +06:00
|
|
|
GotNumber(i32),
|
2021-12-15 13:46:32 +02:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:41:04 +06:00
|
|
|
#[derive(Clone, BotCommands)]
|
2022-10-05 10:08:50 +04:00
|
|
|
#[command(rename_rule = "lowercase", description = "These commands are supported:")]
|
2022-02-04 20:59:46 +06:00
|
|
|
pub enum Command {
|
|
|
|
#[command(description = "get your number.")]
|
|
|
|
Get,
|
|
|
|
#[command(description = "reset your number.")]
|
|
|
|
Reset,
|
|
|
|
}
|
|
|
|
|
2020-10-24 19:57:51 +03:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2022-03-21 18:53:59 +04:00
|
|
|
pretty_env_logger::init();
|
2022-04-27 01:00:08 +06:00
|
|
|
log::info!("Starting DB remember bot...");
|
2022-03-21 18:53:59 +04:00
|
|
|
|
2022-09-29 09:37:20 +06:00
|
|
|
let bot = Bot::from_env();
|
2021-03-06 03:24:10 +06:00
|
|
|
|
2022-03-21 20:06:32 +06:00
|
|
|
let storage: MyStorage = if std::env::var("DB_REMEMBER_REDIS").is_ok() {
|
|
|
|
RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap().erase()
|
|
|
|
} else {
|
|
|
|
SqliteStorage::open("db.sqlite", Json).await.unwrap().erase()
|
|
|
|
};
|
|
|
|
|
2022-02-03 02:36:36 +06:00
|
|
|
let handler = Update::filter_message()
|
2022-03-21 20:06:32 +06:00
|
|
|
.enter_dialogue::<Message, ErasedStorage<State>, State>()
|
2022-04-27 15:16:34 +06:00
|
|
|
.branch(dptree::case![State::Start].endpoint(start))
|
2022-03-26 14:57:30 +06:00
|
|
|
.branch(
|
2022-04-27 15:16:34 +06:00
|
|
|
dptree::case![State::GotNumber(n)]
|
2022-03-26 14:57:30 +06:00
|
|
|
.branch(dptree::entry().filter_command::<Command>().endpoint(got_number))
|
|
|
|
.branch(dptree::endpoint(invalid_command)),
|
|
|
|
);
|
2022-01-26 15:51:51 +06:00
|
|
|
|
2022-02-04 20:09:53 +06:00
|
|
|
Dispatcher::builder(bot, handler)
|
2022-02-02 04:51:36 +06:00
|
|
|
.dependencies(dptree::deps![storage])
|
2022-07-21 12:36:57 +04:00
|
|
|
.enable_ctrlc_handler()
|
2022-02-02 04:51:36 +06:00
|
|
|
.build()
|
|
|
|
.dispatch()
|
|
|
|
.await;
|
2020-10-24 19:57:51 +03:00
|
|
|
}
|
2022-02-03 03:36:53 +06:00
|
|
|
|
2022-10-03 17:54:06 +06:00
|
|
|
async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
2022-03-26 14:57:30 +06:00
|
|
|
match msg.text().map(|text| text.parse::<i32>()) {
|
|
|
|
Some(Ok(n)) => {
|
|
|
|
dialogue.update(State::GotNumber(n)).await?;
|
2022-02-03 03:36:53 +06:00
|
|
|
bot.send_message(
|
|
|
|
msg.chat.id,
|
2022-04-03 16:06:44 +06:00
|
|
|
format!("Remembered number {n}. Now use /get or /reset."),
|
2022-02-03 03:36:53 +06:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
_ => {
|
2022-03-26 14:57:30 +06:00
|
|
|
bot.send_message(msg.chat.id, "Please, send me a number.").await?;
|
2022-02-03 03:36:53 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-26 14:57:30 +06:00
|
|
|
async fn got_number(
|
2022-09-29 09:37:20 +06:00
|
|
|
bot: Bot,
|
2022-02-03 03:36:53 +06:00
|
|
|
dialogue: MyDialogue,
|
2022-10-03 17:54:06 +06:00
|
|
|
num: i32, // Available from `State::GotNumber`.
|
|
|
|
msg: Message,
|
2022-03-26 14:57:30 +06:00
|
|
|
cmd: Command,
|
2022-03-21 20:06:32 +06:00
|
|
|
) -> HandlerResult {
|
2022-03-26 14:57:30 +06:00
|
|
|
match cmd {
|
|
|
|
Command::Get => {
|
2022-04-03 16:06:44 +06:00
|
|
|
bot.send_message(msg.chat.id, format!("Here is your number: {num}.")).await?;
|
2022-03-26 14:57:30 +06:00
|
|
|
}
|
|
|
|
Command::Reset => {
|
|
|
|
dialogue.reset().await?;
|
|
|
|
bot.send_message(msg.chat.id, "Number resetted.").await?;
|
2022-02-04 20:59:46 +06:00
|
|
|
}
|
2022-02-03 03:36:53 +06:00
|
|
|
}
|
2022-03-26 14:57:30 +06:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-03 03:36:53 +06:00
|
|
|
|
2022-09-29 09:37:20 +06:00
|
|
|
async fn invalid_command(bot: Bot, msg: Message) -> HandlerResult {
|
2022-03-26 14:57:30 +06:00
|
|
|
bot.send_message(msg.chat.id, "Please, send /get or /reset.").await?;
|
2022-02-03 03:36:53 +06:00
|
|
|
Ok(())
|
|
|
|
}
|