Fix dialogue examples

This commit is contained in:
Hirrolot 2022-02-03 02:36:36 +06:00
parent 62934f29bd
commit 45293e3168
6 changed files with 19 additions and 26 deletions

View file

@ -60,7 +60,7 @@ full = [
teloxide-core = { version = "0.3.3", default-features = false } teloxide-core = { version = "0.3.3", default-features = false }
#teloxide-core = { git = "https://github.com/teloxide/teloxide-core.git", rev = "...", default-features = false } #teloxide-core = { git = "https://github.com/teloxide/teloxide-core.git", rev = "...", default-features = false }
#teloxide-macros = { version = "0.4", optional = true } #teloxide-macros = { version = "0.4", optional = true }
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros", branch = "dispatching2", optional = true } teloxide-macros = { path = "../teloxide-macros", optional = true }
serde_json = "1.0" serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
@ -123,7 +123,7 @@ required-features = ["sqlite-storage", "cbor-serializer", "bincode-serializer"]
[[example]] [[example]]
name = "dialogue" name = "dialogue"
required-features = ["macros", "sqlite-storage"] required-features = ["macros"]
[[example]] [[example]]
name = "sqlite_remember" name = "sqlite_remember"

View file

@ -13,15 +13,11 @@
// Age: 223 // Age: 223
// Location: Middle-earth // Location: Middle-earth
// ``` // ```
use teloxide::{ use teloxide::{dispatching2::dialogue::InMemStorage, macros::DialogueState, prelude2::*};
dispatching2::dialogue::{serializer::Json, SqliteStorage},
macros::DialogueState,
prelude2::*,
};
type MyDialogue = Dialogue<State, SqliteStorage<Json>>; type MyDialogue = Dialogue<State, InMemStorage<State>>;
#[derive(DialogueState, Clone, serde::Serialize, serde::Deserialize)] #[derive(DialogueState, Clone)]
#[handler_out(anyhow::Result<()>)] #[handler_out(anyhow::Result<()>)]
pub enum State { pub enum State {
#[handler(handle_start)] #[handler(handle_start)]
@ -37,7 +33,7 @@ pub enum State {
ReceiveLocation(ReceiveLocation), ReceiveLocation(ReceiveLocation),
} }
#[derive(Clone, serde::Serialize, serde::Deserialize)] #[derive(Clone)]
pub struct ReceiveLocation { pub struct ReceiveLocation {
full_name: String, full_name: String,
age: u8, age: u8,
@ -55,15 +51,14 @@ async fn main() {
log::info!("Starting dialogue_bot..."); log::info!("Starting dialogue_bot...");
let bot = Bot::from_env().auto_send(); let bot = Bot::from_env().auto_send();
let storage = SqliteStorage::open("db.sqlite", Json).await.unwrap();
DispatcherBuilder::new( DispatcherBuilder::new(
bot, bot,
dptree::entry() Update::filter_message()
.add_dialogue::<Message, SqliteStorage<Json>, State>() .add_dialogue::<Message, InMemStorage<State>, State>()
.dispatch_by::<State>(), .dispatch_by::<State>(),
) )
.dependencies(dptree::deps![storage]) .dependencies(dptree::deps![InMemStorage::<State>::new()])
.build() .build()
.dispatch() .dispatch()
.await; .await;

View file

@ -78,7 +78,7 @@ async fn main() {
// or "serializer-bincode" // or "serializer-bincode"
let storage = RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap(); let storage = RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap();
let handler = dptree::entry() let handler = Update::filter_message()
.add_dialogue::<Message, RedisStorage<Bincode>, DialogueState>() .add_dialogue::<Message, RedisStorage<Bincode>, DialogueState>()
.branch(dptree::endpoint(handle_message)); .branch(dptree::endpoint(handle_message));

View file

@ -74,7 +74,7 @@ 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 = SqliteStorage::open("db.sqlite", Json).await.unwrap();
let handler = dptree::entry() let handler = Update::filter_message()
.add_dialogue::<Message, SqliteStorage<Json>, DialogueState>() .add_dialogue::<Message, SqliteStorage<Json>, DialogueState>()
.branch(dptree::endpoint(handle_message)); .branch(dptree::endpoint(handle_message));

View file

@ -18,20 +18,17 @@ where
{ {
fn add_dialogue<Upd, S, D>(self) -> Self fn add_dialogue<Upd, S, D>(self) -> Self
where where
// FIXME: some of this requirements are useless.
S: Storage<D> + Send + Sync + 'static, S: Storage<D> + Send + Sync + 'static,
<S as Storage<D>>::Error: Send, <S as Storage<D>>::Error: Send,
D: Default + Send + Sync + 'static, D: Default + Send + Sync + 'static,
Upd: GetChatId + Clone + Send + Sync + 'static, Upd: GetChatId + Clone + Send + Sync + 'static,
{ {
self.chain( self.chain(dptree::filter_map(|storage: Arc<S>, upd: Upd| async move {
dptree::filter_map(|storage: Arc<S>, upd: Upd| async move { let chat_id = upd.chat_id()?;
let chat_id = upd.chat_id()?; Some(Dialogue::new(storage, chat_id))
Some(Dialogue::new(storage, chat_id)) }))
}) .chain(dptree::filter_map(|dialogue: Dialogue<D, S>| async move {
.chain(dptree::filter_map(|dialogue: Dialogue<D, S>| async move { dialogue.get_or_default().await.ok()
dialogue.get_or_default().await.ok() }))
})),
)
} }
} }

View file

@ -92,6 +92,7 @@ pub struct Dispatcher<R, Err> {
handler: UpdateHandler<Err>, handler: UpdateHandler<Err>,
default_handler: DefaultHandler, default_handler: DefaultHandler,
error_handler: Arc<dyn ErrorHandler<Err>>, error_handler: Arc<dyn ErrorHandler<Err>>,
// TODO: respect allowed_udpates
allowed_updates: HashSet<AllowedUpdate>, allowed_updates: HashSet<AllowedUpdate>,
state: Arc<DispatcherState>, state: Arc<DispatcherState>,