use std::sync::Arc;
use crate::{
dispatching::{
dialogue::{Dialogue, GetChatId, Storage},
DpHandlerDescription,
},
types::{Me, Message},
utils::command::BotCommands,
};
use dptree::{di::DependencyMap, Handler};
#[allow(deprecated)]
use crate::dispatching::HandlerFactory;
use std::fmt::Debug;
pub trait HandlerExt<Output> {
#[must_use]
fn filter_command<C>(self) -> Self
where
C: BotCommands + Send + Sync + 'static;
#[must_use]
fn enter_dialogue<Upd, S, D>(self) -> Self
where
S: Storage<D> + ?Sized + Send + Sync + 'static,
<S as Storage<D>>::Error: Debug + Send,
D: Default + Send + Sync + 'static,
Upd: GetChatId + Clone + Send + Sync + 'static;
#[must_use]
#[deprecated(note = "Use the teloxide::handler! API")]
#[allow(deprecated)]
fn dispatch_by<F>(self) -> Self
where
F: HandlerFactory<Out = Output>;
}
impl<Output> HandlerExt<Output> for Handler<'static, DependencyMap, Output, DpHandlerDescription>
where
Output: Send + Sync + 'static,
{
fn filter_command<C>(self) -> Self
where
C: BotCommands + Send + Sync + 'static,
{
self.chain(dptree::filter_map(move |message: Message, me: Me| {
let bot_name = me.user.username.expect("Bots must have a username");
message.text().and_then(|text| C::parse(text, bot_name).ok())
}))
}
fn enter_dialogue<Upd, S, D>(self) -> Self
where
S: Storage<D> + ?Sized + Send + Sync + 'static,
<S as Storage<D>>::Error: Debug + Send,
D: Default + Send + Sync + 'static,
Upd: GetChatId + Clone + Send + Sync + 'static,
{
self.chain(dptree::filter_map(|storage: Arc<S>, upd: Upd| {
let chat_id = upd.chat_id()?;
Some(Dialogue::new(storage, chat_id))
}))
.chain(dptree::filter_map_async(|dialogue: Dialogue<D, S>| async move {
match dialogue.get_or_default().await {
Ok(dialogue) => Some(dialogue),
Err(err) => {
log::error!("dialogue.get_or_default() failed: {:?}", err);
None
}
}
}))
}
#[allow(deprecated)]
fn dispatch_by<F>(self) -> Self
where
F: HandlerFactory<Out = Output>,
{
self.chain(F::handler())
}
}