mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-09 19:49:19 +01:00
Merge pull request #546 from teloxide/merge-redis-sqlite-examples
Merge `examples/sqlite_remember.rs` with `redis_remember.rs`
This commit is contained in:
commit
e26adcf688
8 changed files with 83 additions and 203 deletions
|
@ -8,8 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- The `Storage::erase` default function that returns `ErasedStorage`.
|
- The `Storage::erase` default function that returns `Arc<ErasedStorage>`.
|
||||||
- `ErasedStorage`, a storage with an erased error type.
|
- `ErasedStorage`, a storage with an erased error type.
|
||||||
|
- Allow the storage generic `S` be `?Sized` in `Dialogue` and `HandlerExt::enter_dialogue`.
|
||||||
|
|
||||||
## 0.7.1 - 2022-03-09
|
## 0.7.1 - 2022-03-09
|
||||||
|
|
||||||
|
|
|
@ -128,17 +128,13 @@ required-features = ["sqlite-storage", "cbor-serializer", "bincode-serializer"]
|
||||||
name = "dialogue"
|
name = "dialogue"
|
||||||
required-features = ["macros"]
|
required-features = ["macros"]
|
||||||
|
|
||||||
[[example]]
|
|
||||||
name = "sqlite_remember"
|
|
||||||
required-features = ["sqlite-storage", "bincode-serializer", "redis-storage", "macros"]
|
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "simple_commands"
|
name = "simple_commands"
|
||||||
required-features = ["macros"]
|
required-features = ["macros"]
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "redis_remember"
|
name = "db_remember"
|
||||||
required-features = ["redis-storage", "bincode-serializer", "macros"]
|
required-features = ["sqlite-storage", "redis-storage", "bincode-serializer", "macros"]
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "inline"
|
name = "inline"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
```
|
```
|
||||||
$ cargo run --example <example name> --features="<features required by the example>"
|
$ cargo run --features="full" --example <example-name>
|
||||||
```
|
```
|
||||||
|
|
||||||
Don't forget to initialise the `TELOXIDE_TOKEN` environmental variable.
|
Don't forget to initialise the `TELOXIDE_TOKEN` environmental variable.
|
||||||
|
|
|
@ -1,27 +1,23 @@
|
||||||
|
// Set the `DB_REMEMBER_REDIS` environmental variable if you want to use Redis.
|
||||||
|
// Otherwise, the default is Sqlite.
|
||||||
|
|
||||||
use teloxide::{
|
use teloxide::{
|
||||||
dispatching2::dialogue::{serializer::Json, SqliteStorage, Storage},
|
dispatching2::dialogue::{
|
||||||
|
serializer::{Bincode, Json},
|
||||||
|
ErasedStorage, RedisStorage, SqliteStorage, Storage,
|
||||||
|
},
|
||||||
macros::DialogueState,
|
macros::DialogueState,
|
||||||
prelude2::*,
|
prelude2::*,
|
||||||
types::Me,
|
types::Me,
|
||||||
utils::command::BotCommand,
|
utils::command::BotCommand,
|
||||||
RequestError,
|
|
||||||
};
|
};
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
type MyDialogue = Dialogue<State, SqliteStorage<Json>>;
|
type MyDialogue = Dialogue<State, ErasedStorage<State>>;
|
||||||
type StorageError = <SqliteStorage<Json> as Storage<State>>::Error;
|
type MyStorage = std::sync::Arc<ErasedStorage<State>>;
|
||||||
|
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
||||||
#[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)]
|
#[derive(DialogueState, Clone, serde::Serialize, serde::Deserialize)]
|
||||||
#[handler_out(anyhow::Result<()>)]
|
#[handler_out(HandlerResult)]
|
||||||
pub enum State {
|
pub enum State {
|
||||||
#[handler(handle_start)]
|
#[handler(handle_start)]
|
||||||
Start,
|
Start,
|
||||||
|
@ -48,10 +44,15 @@ pub enum Command {
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let bot = Bot::from_env().auto_send();
|
let bot = Bot::from_env().auto_send();
|
||||||
let storage = SqliteStorage::open("db.sqlite", Json).await.unwrap();
|
|
||||||
|
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()
|
||||||
|
};
|
||||||
|
|
||||||
let handler = Update::filter_message()
|
let handler = Update::filter_message()
|
||||||
.enter_dialogue::<Message, SqliteStorage<Json>, State>()
|
.enter_dialogue::<Message, ErasedStorage<State>, State>()
|
||||||
.dispatch_by::<State>();
|
.dispatch_by::<State>();
|
||||||
|
|
||||||
Dispatcher::builder(bot, handler)
|
Dispatcher::builder(bot, handler)
|
||||||
|
@ -62,11 +63,7 @@ async fn main() {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_start(
|
async fn handle_start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||||
bot: AutoSend<Bot>,
|
|
||||||
msg: Message,
|
|
||||||
dialogue: MyDialogue,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
match msg.text().unwrap().parse() {
|
match msg.text().unwrap().parse() {
|
||||||
Ok(number) => {
|
Ok(number) => {
|
||||||
dialogue.update(State::GotNumber(number)).await?;
|
dialogue.update(State::GotNumber(number)).await?;
|
||||||
|
@ -90,7 +87,7 @@ async fn handle_got_number(
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
num: i32,
|
num: i32,
|
||||||
me: Me,
|
me: Me,
|
||||||
) -> anyhow::Result<()> {
|
) -> HandlerResult {
|
||||||
let ans = msg.text().unwrap();
|
let ans = msg.text().unwrap();
|
||||||
let bot_name = me.user.username.unwrap();
|
let bot_name = me.user.username.unwrap();
|
||||||
|
|
|
@ -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<State, RedisStorage<Bincode>>;
|
|
||||||
type StorageError = <RedisStorage<Bincode> as Storage<State>>::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-<name>", 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::<Message, RedisStorage<Bincode>, State>()
|
|
||||||
.dispatch_by::<State>();
|
|
||||||
|
|
||||||
Dispatcher::builder(bot, handler)
|
|
||||||
.dependencies(dptree::deps![storage])
|
|
||||||
.build()
|
|
||||||
.setup_ctrlc_handler()
|
|
||||||
.dispatch()
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
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(())
|
|
||||||
}
|
|
|
@ -24,6 +24,10 @@ use std::sync::Arc;
|
||||||
#[cfg(feature = "sqlite-storage")]
|
#[cfg(feature = "sqlite-storage")]
|
||||||
pub use sqlite_storage::{SqliteStorage, SqliteStorageError};
|
pub use sqlite_storage::{SqliteStorage, SqliteStorageError};
|
||||||
|
|
||||||
|
/// A storage with an erased error type.
|
||||||
|
pub type ErasedStorage<D> =
|
||||||
|
dyn Storage<D, Error = Box<dyn std::error::Error + Send + Sync>> + Send + Sync;
|
||||||
|
|
||||||
/// A storage of dialogues.
|
/// A storage of dialogues.
|
||||||
///
|
///
|
||||||
/// You can implement this trait for a structure that communicates with a DB and
|
/// You can implement this trait for a structure that communicates with a DB and
|
||||||
|
@ -74,30 +78,32 @@ pub trait Storage<D> {
|
||||||
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>;
|
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>;
|
||||||
|
|
||||||
/// Erases [`Self::Error`] to [`std::error::Error`].
|
/// Erases [`Self::Error`] to [`std::error::Error`].
|
||||||
fn erase(self: Arc<Self>) -> ErasedStorage<D>
|
#[must_use]
|
||||||
|
fn erase(self: Arc<Self>) -> Arc<ErasedStorage<D>>
|
||||||
where
|
where
|
||||||
Self: Sized + Send + Sync + 'static,
|
Self: Sized + Send + Sync + 'static,
|
||||||
Self::Error: std::error::Error + 'static,
|
Self::Error: std::error::Error + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
struct Eraser<S>(Arc<S>);
|
Arc::new(Eraser(self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<D, S> Storage<D> for Eraser<S>
|
struct Eraser<S>(Arc<S>);
|
||||||
where
|
|
||||||
|
impl<D, S> Storage<D> for Eraser<S>
|
||||||
|
where
|
||||||
S: Storage<D> + Send + Sync + 'static,
|
S: Storage<D> + Send + Sync + 'static,
|
||||||
S::Error: std::error::Error + 'static,
|
S::Error: std::error::Error + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
type Error = Box<dyn std::error::Error>;
|
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
|
|
||||||
fn remove_dialogue(
|
fn remove_dialogue(self: Arc<Self>, chat_id: i64) -> BoxFuture<'static, Result<(), Self::Error>>
|
||||||
self: Arc<Self>,
|
|
||||||
chat_id: i64,
|
|
||||||
) -> BoxFuture<'static, Result<(), Self::Error>>
|
|
||||||
where
|
where
|
||||||
D: Send + 'static,
|
D: Send + 'static,
|
||||||
{
|
{
|
||||||
Box::pin(async move {
|
Box::pin(
|
||||||
Arc::clone(&self.0).remove_dialogue(chat_id).await.map_err(|e| e.into())
|
async move { Arc::clone(&self.0).remove_dialogue(chat_id).await.map_err(|e| e.into()) },
|
||||||
})
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_dialogue(
|
fn update_dialogue(
|
||||||
|
@ -109,10 +115,7 @@ pub trait Storage<D> {
|
||||||
D: Send + 'static,
|
D: Send + 'static,
|
||||||
{
|
{
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
Arc::clone(&self.0)
|
Arc::clone(&self.0).update_dialogue(chat_id, dialogue).await.map_err(|e| e.into())
|
||||||
.update_dialogue(chat_id, dialogue)
|
|
||||||
.await
|
|
||||||
.map_err(|e| e.into())
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,19 +123,12 @@ pub trait Storage<D> {
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
chat_id: i64,
|
chat_id: i64,
|
||||||
) -> BoxFuture<'static, Result<Option<D>, Self::Error>> {
|
) -> BoxFuture<'static, Result<Option<D>, Self::Error>> {
|
||||||
Box::pin(async move {
|
Box::pin(
|
||||||
Arc::clone(&self.0).get_dialogue(chat_id).await.map_err(|e| e.into())
|
async move { Arc::clone(&self.0).get_dialogue(chat_id).await.map_err(|e| e.into()) },
|
||||||
})
|
)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Arc::new(Eraser(self))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A storage with an erased error type.
|
|
||||||
pub type ErasedStorage<D> = Arc<dyn Storage<D, Error = Box<dyn std::error::Error>>>;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -102,7 +102,10 @@ mod get_chat_id;
|
||||||
|
|
||||||
/// A handle for controlling dialogue state.
|
/// A handle for controlling dialogue state.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Dialogue<D, S> {
|
pub struct Dialogue<D, S>
|
||||||
|
where
|
||||||
|
S: ?Sized,
|
||||||
|
{
|
||||||
storage: Arc<S>,
|
storage: Arc<S>,
|
||||||
chat_id: i64,
|
chat_id: i64,
|
||||||
_phantom: PhantomData<D>,
|
_phantom: PhantomData<D>,
|
||||||
|
@ -110,7 +113,10 @@ pub struct Dialogue<D, S> {
|
||||||
|
|
||||||
// `#[derive]` requires generics to implement `Clone`, but `S` is wrapped around
|
// `#[derive]` requires generics to implement `Clone`, but `S` is wrapped around
|
||||||
// `Arc`, and `D` is wrapped around PhantomData.
|
// `Arc`, and `D` is wrapped around PhantomData.
|
||||||
impl<D, S> Clone for Dialogue<D, S> {
|
impl<D, S> Clone for Dialogue<D, S>
|
||||||
|
where
|
||||||
|
S: ?Sized,
|
||||||
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Dialogue { storage: self.storage.clone(), chat_id: self.chat_id, _phantom: PhantomData }
|
Dialogue { storage: self.storage.clone(), chat_id: self.chat_id, _phantom: PhantomData }
|
||||||
}
|
}
|
||||||
|
@ -119,7 +125,7 @@ impl<D, S> Clone for Dialogue<D, S> {
|
||||||
impl<D, S> Dialogue<D, S>
|
impl<D, S> Dialogue<D, S>
|
||||||
where
|
where
|
||||||
D: Send + 'static,
|
D: Send + 'static,
|
||||||
S: Storage<D>,
|
S: Storage<D> + ?Sized,
|
||||||
{
|
{
|
||||||
/// Constructs a new dialogue with `storage` (where dialogues are stored)
|
/// Constructs a new dialogue with `storage` (where dialogues are stored)
|
||||||
/// and `chat_id` of a current dialogue.
|
/// and `chat_id` of a current dialogue.
|
||||||
|
|
|
@ -45,7 +45,7 @@ pub trait HandlerExt<Output> {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn enter_dialogue<Upd, S, D>(self) -> Self
|
fn enter_dialogue<Upd, S, D>(self) -> Self
|
||||||
where
|
where
|
||||||
S: Storage<D> + Send + Sync + 'static,
|
S: Storage<D> + ?Sized + Send + Sync + 'static,
|
||||||
<S as Storage<D>>::Error: Debug + Send,
|
<S as Storage<D>>::Error: Debug + Send,
|
||||||
D: Default + Send + Sync + 'static,
|
D: Default + Send + Sync + 'static,
|
||||||
Upd: GetChatId + Clone + Send + Sync + 'static;
|
Upd: GetChatId + Clone + Send + Sync + 'static;
|
||||||
|
@ -72,7 +72,7 @@ where
|
||||||
|
|
||||||
fn enter_dialogue<Upd, S, D>(self) -> Self
|
fn enter_dialogue<Upd, S, D>(self) -> Self
|
||||||
where
|
where
|
||||||
S: Storage<D> + Send + Sync + 'static,
|
S: Storage<D> + ?Sized + Send + Sync + 'static,
|
||||||
<S as Storage<D>>::Error: Debug + Send,
|
<S as Storage<D>>::Error: Debug + Send,
|
||||||
D: Default + Send + Sync + 'static,
|
D: Default + Send + Sync + 'static,
|
||||||
Upd: GetChatId + Clone + Send + Sync + 'static,
|
Upd: GetChatId + Clone + Send + Sync + 'static,
|
||||||
|
|
Loading…
Reference in a new issue