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