2020-10-24 18:57:51 +02:00
|
|
|
use teloxide::{
|
2021-12-15 12:46:32 +01:00
|
|
|
dispatching2::dialogue::{serializer::Json, SqliteStorage, Storage},
|
2022-02-02 22:36:53 +01:00
|
|
|
macros::DialogueState,
|
2022-01-06 13:50:59 +01:00
|
|
|
prelude2::*,
|
2022-02-04 15:59:46 +01:00
|
|
|
types::Me,
|
|
|
|
utils::command::BotCommand,
|
2021-03-15 04:22:10 +01:00
|
|
|
RequestError,
|
2020-10-24 18:57:51 +02:00
|
|
|
};
|
|
|
|
use thiserror::Error;
|
|
|
|
|
2022-02-02 22:36:53 +01:00
|
|
|
type MyDialogue = Dialogue<State, SqliteStorage<Json>>;
|
|
|
|
type StorageError = <SqliteStorage<Json> as Storage<State>>::Error;
|
2020-10-24 18:57:51 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
enum Error {
|
|
|
|
#[error("error from Telegram: {0}")]
|
|
|
|
TelegramError(#[from] RequestError),
|
2022-02-02 22:36:53 +01:00
|
|
|
|
2020-10-24 18:57:51 +02:00
|
|
|
#[error("error from storage: {0}")]
|
|
|
|
StorageError(#[from] StorageError),
|
|
|
|
}
|
|
|
|
|
2022-02-02 22:36:53 +01:00
|
|
|
#[derive(DialogueState, Clone, serde::Serialize, serde::Deserialize)]
|
|
|
|
#[handler_out(anyhow::Result<()>)]
|
|
|
|
pub enum State {
|
|
|
|
#[handler(handle_start)]
|
2021-12-15 12:46:32 +01:00
|
|
|
Start,
|
2022-02-02 22:36:53 +01:00
|
|
|
|
|
|
|
#[handler(handle_got_number)]
|
2022-02-02 10:25:23 +01:00
|
|
|
GotNumber(i32),
|
2021-12-15 12:46:32 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 22:36:53 +01:00
|
|
|
impl Default for State {
|
2021-12-15 12:46:32 +01:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::Start
|
|
|
|
}
|
|
|
|
}
|
2020-10-24 18:57:51 +02:00
|
|
|
|
2022-02-04 15:59:46 +01:00
|
|
|
#[derive(BotCommand)]
|
|
|
|
#[command(rename = "lowercase", description = "These commands are supported:")]
|
|
|
|
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() {
|
2021-12-26 15:55:24 +01:00
|
|
|
let bot = Bot::from_env().auto_send();
|
2021-12-15 12:46:32 +01:00
|
|
|
let storage = SqliteStorage::open("db.sqlite", Json).await.unwrap();
|
2021-03-05 22:24:10 +01:00
|
|
|
|
2022-02-02 21:36:36 +01:00
|
|
|
let handler = Update::filter_message()
|
2022-02-04 15:00:21 +01:00
|
|
|
.enter_dialogue::<Message, SqliteStorage<Json>, State>()
|
2022-02-02 22:36:53 +01:00
|
|
|
.dispatch_by::<State>();
|
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])
|
|
|
|
.build()
|
2022-02-03 11:34:40 +01:00
|
|
|
.setup_ctrlc_handler()
|
2022-02-01 23:51:36 +01:00
|
|
|
.dispatch()
|
|
|
|
.await;
|
2020-10-24 18:57:51 +02:00
|
|
|
}
|
2022-02-02 22:36:53 +01:00
|
|
|
|
|
|
|
async fn handle_start(
|
|
|
|
bot: AutoSend<Bot>,
|
|
|
|
msg: Message,
|
|
|
|
dialogue: MyDialogue,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
match msg.text().unwrap().parse() {
|
|
|
|
Ok(number) => {
|
|
|
|
dialogue.update(State::GotNumber(number)).await?;
|
|
|
|
bot.send_message(
|
|
|
|
msg.chat.id,
|
|
|
|
format!("Remembered number {}. Now use /get or /reset", number),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bot.send_message(msg.chat.id, "Please, send me a number").await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_got_number(
|
|
|
|
bot: AutoSend<Bot>,
|
|
|
|
msg: Message,
|
|
|
|
dialogue: MyDialogue,
|
|
|
|
num: i32,
|
2022-02-04 15:59:46 +01:00
|
|
|
me: Me,
|
2022-02-02 22:36:53 +01:00
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
let ans = msg.text().unwrap();
|
2022-02-04 15:59:46 +01:00
|
|
|
let bot_name = me.user.username.unwrap();
|
2022-02-02 22:36:53 +01:00
|
|
|
|
2022-02-04 15:59:46 +01:00
|
|
|
match Command::parse(ans, bot_name) {
|
|
|
|
Ok(cmd) => match cmd {
|
|
|
|
Command::Get => {
|
|
|
|
bot.send_message(msg.chat.id, format!("Here is your number: {}", num)).await?;
|
|
|
|
}
|
|
|
|
Command::Reset => {
|
|
|
|
dialogue.reset().await?;
|
|
|
|
bot.send_message(msg.chat.id, "Number resetted").await?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(_) => {
|
|
|
|
bot.send_message(msg.chat.id, "Please, send /get or /reset").await?;
|
|
|
|
}
|
2022-02-02 22:36:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|