1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::{
dispatching::{
dialogue::{GetChatId, Storage},
DpHandlerDescription,
},
types::{Me, Message},
utils::command::BotCommands,
};
use dptree::{di::DependencyMap, Handler};
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;
}
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(filter_command::<C, Output>())
}
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(super::dialogue::enter::<Upd, S, D, Output>())
}
}
#[must_use]
pub fn filter_command<C, Output>() -> Handler<'static, DependencyMap, Output, DpHandlerDescription>
where
C: BotCommands + Send + Sync + 'static,
Output: Send + Sync + 'static,
{
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())
})
}