mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-03 17:52:12 +01:00
Fix dialogue examples
This commit is contained in:
parent
62934f29bd
commit
45293e3168
6 changed files with 19 additions and 26 deletions
|
@ -60,7 +60,7 @@ full = [
|
|||
teloxide-core = { version = "0.3.3", 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 = { git = "https://github.com/teloxide/teloxide-macros", branch = "dispatching2", optional = true }
|
||||
teloxide-macros = { path = "../teloxide-macros", optional = true }
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
||||
|
@ -123,7 +123,7 @@ required-features = ["sqlite-storage", "cbor-serializer", "bincode-serializer"]
|
|||
|
||||
[[example]]
|
||||
name = "dialogue"
|
||||
required-features = ["macros", "sqlite-storage"]
|
||||
required-features = ["macros"]
|
||||
|
||||
[[example]]
|
||||
name = "sqlite_remember"
|
||||
|
|
|
@ -13,15 +13,11 @@
|
|||
// Age: 223
|
||||
// Location: Middle-earth
|
||||
// ```
|
||||
use teloxide::{
|
||||
dispatching2::dialogue::{serializer::Json, SqliteStorage},
|
||||
macros::DialogueState,
|
||||
prelude2::*,
|
||||
};
|
||||
use teloxide::{dispatching2::dialogue::InMemStorage, 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<()>)]
|
||||
pub enum State {
|
||||
#[handler(handle_start)]
|
||||
|
@ -37,7 +33,7 @@ pub enum State {
|
|||
ReceiveLocation(ReceiveLocation),
|
||||
}
|
||||
|
||||
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
||||
#[derive(Clone)]
|
||||
pub struct ReceiveLocation {
|
||||
full_name: String,
|
||||
age: u8,
|
||||
|
@ -55,15 +51,14 @@ async fn main() {
|
|||
log::info!("Starting dialogue_bot...");
|
||||
|
||||
let bot = Bot::from_env().auto_send();
|
||||
let storage = SqliteStorage::open("db.sqlite", Json).await.unwrap();
|
||||
|
||||
DispatcherBuilder::new(
|
||||
bot,
|
||||
dptree::entry()
|
||||
.add_dialogue::<Message, SqliteStorage<Json>, State>()
|
||||
Update::filter_message()
|
||||
.add_dialogue::<Message, InMemStorage<State>, State>()
|
||||
.dispatch_by::<State>(),
|
||||
)
|
||||
.dependencies(dptree::deps![storage])
|
||||
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
||||
.build()
|
||||
.dispatch()
|
||||
.await;
|
||||
|
|
|
@ -78,7 +78,7 @@ async fn main() {
|
|||
// or "serializer-bincode"
|
||||
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>()
|
||||
.branch(dptree::endpoint(handle_message));
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@ async fn main() {
|
|||
let bot = Bot::from_env().auto_send();
|
||||
let storage = SqliteStorage::open("db.sqlite", Json).await.unwrap();
|
||||
|
||||
let handler = dptree::entry()
|
||||
let handler = Update::filter_message()
|
||||
.add_dialogue::<Message, SqliteStorage<Json>, DialogueState>()
|
||||
.branch(dptree::endpoint(handle_message));
|
||||
|
||||
|
|
|
@ -18,20 +18,17 @@ where
|
|||
{
|
||||
fn add_dialogue<Upd, S, D>(self) -> Self
|
||||
where
|
||||
// FIXME: some of this requirements are useless.
|
||||
S: Storage<D> + Send + Sync + 'static,
|
||||
<S as Storage<D>>::Error: Send,
|
||||
D: Default + Send + Sync + 'static,
|
||||
Upd: GetChatId + Clone + Send + Sync + 'static,
|
||||
{
|
||||
self.chain(
|
||||
dptree::filter_map(|storage: Arc<S>, upd: Upd| async move {
|
||||
let chat_id = upd.chat_id()?;
|
||||
Some(Dialogue::new(storage, chat_id))
|
||||
})
|
||||
.chain(dptree::filter_map(|dialogue: Dialogue<D, S>| async move {
|
||||
dialogue.get_or_default().await.ok()
|
||||
})),
|
||||
)
|
||||
self.chain(dptree::filter_map(|storage: Arc<S>, upd: Upd| async move {
|
||||
let chat_id = upd.chat_id()?;
|
||||
Some(Dialogue::new(storage, chat_id))
|
||||
}))
|
||||
.chain(dptree::filter_map(|dialogue: Dialogue<D, S>| async move {
|
||||
dialogue.get_or_default().await.ok()
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,6 +92,7 @@ pub struct Dispatcher<R, Err> {
|
|||
handler: UpdateHandler<Err>,
|
||||
default_handler: DefaultHandler,
|
||||
error_handler: Arc<dyn ErrorHandler<Err>>,
|
||||
// TODO: respect allowed_udpates
|
||||
allowed_updates: HashSet<AllowedUpdate>,
|
||||
|
||||
state: Arc<DispatcherState>,
|
||||
|
|
Loading…
Reference in a new issue