From 7a932c8fd99ed94fea567d8b238e265291fa5edf Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Sat, 12 Mar 2022 14:33:38 +0600 Subject: [PATCH] Merge `examples/sqlite_remember.rs` with `redis_remember.rs` --- Cargo.toml | 8 +- .../{sqlite_remember.rs => db_remember.rs} | 27 +--- examples/redis_config.in | 7 ++ examples/redis_remember.rs | 116 ------------------ examples/sqlite_config.in | 7 ++ 5 files changed, 22 insertions(+), 143 deletions(-) rename examples/{sqlite_remember.rs => db_remember.rs} (75%) create mode 100644 examples/redis_config.in delete mode 100644 examples/redis_remember.rs create mode 100644 examples/sqlite_config.in diff --git a/Cargo.toml b/Cargo.toml index a500bb83..71b67886 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -128,17 +128,13 @@ required-features = ["sqlite-storage", "cbor-serializer", "bincode-serializer"] name = "dialogue" required-features = ["macros"] -[[example]] -name = "sqlite_remember" -required-features = ["sqlite-storage", "bincode-serializer", "redis-storage", "macros"] - [[example]] name = "simple_commands" required-features = ["macros"] [[example]] -name = "redis_remember" -required-features = ["redis-storage", "bincode-serializer", "macros"] +name = "db_remember" +required-features = ["sqlite-storage", "redis-storage", "bincode-serializer", "macros"] [[example]] name = "inline" diff --git a/examples/sqlite_remember.rs b/examples/db_remember.rs similarity index 75% rename from examples/sqlite_remember.rs rename to examples/db_remember.rs index e7a7fabd..f53bc9e1 100644 --- a/examples/sqlite_remember.rs +++ b/examples/db_remember.rs @@ -1,24 +1,9 @@ -use teloxide::{ - dispatching2::dialogue::{serializer::Json, SqliteStorage, Storage}, - macros::DialogueState, - prelude2::*, - types::Me, - utils::command::BotCommand, - RequestError, -}; -use thiserror::Error; +use teloxide::{macros::DialogueState, prelude2::*, types::Me, utils::command::BotCommand}; -type MyDialogue = Dialogue>; -type StorageError = as Storage>::Error; +// include!("redis_config.in"); +include!("sqlite_config.in"); -#[derive(Debug, Error)] -enum Error { - #[error("error from Telegram: {0}")] - TelegramError(#[from] RequestError), - - #[error("error from storage: {0}")] - StorageError(#[from] StorageError), -} +type MyDialogue = Dialogue; #[derive(DialogueState, Clone, serde::Serialize, serde::Deserialize)] #[handler_out(anyhow::Result<()>)] @@ -48,10 +33,10 @@ pub enum Command { #[tokio::main] async fn main() { let bot = Bot::from_env().auto_send(); - let storage = SqliteStorage::open("db.sqlite", Json).await.unwrap(); + let storage = open_storage().await; let handler = Update::filter_message() - .enter_dialogue::, State>() + .enter_dialogue::() .dispatch_by::(); Dispatcher::builder(bot, handler) diff --git a/examples/redis_config.in b/examples/redis_config.in new file mode 100644 index 00000000..cd70ebe5 --- /dev/null +++ b/examples/redis_config.in @@ -0,0 +1,7 @@ +use teloxide::dispatching2::dialogue::{RedisStorage, serializer::Bincode}; + +type MyStorage = RedisStorage; + +async fn open_storage() -> std::sync::Arc { + RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap() +} diff --git a/examples/redis_remember.rs b/examples/redis_remember.rs deleted file mode 100644 index 0935bcdf..00000000 --- a/examples/redis_remember.rs +++ /dev/null @@ -1,116 +0,0 @@ -use teloxide::{ - dispatching2::dialogue::{serializer::Bincode, RedisStorage, Storage}, - macros::DialogueState, - prelude2::*, - types::Me, - utils::command::BotCommand, - RequestError, -}; -use thiserror::Error; - -type MyDialogue = Dialogue>; -type StorageError = as Storage>::Error; - -#[derive(Debug, Error)] -enum Error { - #[error("error from Telegram: {0}")] - TelegramError(#[from] RequestError), - - #[error("error from storage: {0}")] - StorageError(#[from] StorageError), -} - -#[derive(DialogueState, Clone, serde::Serialize, serde::Deserialize)] -#[handler_out(anyhow::Result<()>)] -pub enum State { - #[handler(handle_start)] - Start, - - #[handler(handle_got_number)] - GotNumber(i32), -} - -impl Default for State { - fn default() -> Self { - Self::Start - } -} - -#[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, -} -#[tokio::main] -async fn main() { - let bot = Bot::from_env().auto_send(); - // You can also choose serializer::JSON or serializer::CBOR - // All serializers but JSON require enabling feature - // "serializer-", e. g. "serializer-cbor" - // or "serializer-bincode" - let storage = RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap(); - - let handler = Update::filter_message() - .enter_dialogue::, State>() - .dispatch_by::(); - - Dispatcher::builder(bot, handler) - .dependencies(dptree::deps![storage]) - .build() - .setup_ctrlc_handler() - .dispatch() - .await; -} - -async fn handle_start( - bot: AutoSend, - 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, - msg: Message, - dialogue: MyDialogue, - num: i32, - me: Me, -) -> anyhow::Result<()> { - let ans = msg.text().unwrap(); - let bot_name = me.user.username.unwrap(); - - 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?; - } - } - - Ok(()) -} diff --git a/examples/sqlite_config.in b/examples/sqlite_config.in new file mode 100644 index 00000000..33d028e3 --- /dev/null +++ b/examples/sqlite_config.in @@ -0,0 +1,7 @@ +use teloxide::dispatching2::dialogue::{SqliteStorage, serializer::Json}; + +type MyStorage = SqliteStorage; + +async fn open_storage() -> std::sync::Arc { + SqliteStorage::open("db.sqlite", Json).await.unwrap() +}