1use std::collections::HashMap;
2use std::sync::Arc;
3
4use crate::api::BotApi;
5use crate::types::Update;
6use crate::vision::*;
7
8#[derive(Clone)]
17pub struct Listener {
18 text_handler: Option<Arc<Box<dyn Fn((BotApi, VTextMessage)) + Send + Sync + 'static>>>,
19 update_handler: Option<Arc<Box<dyn Fn((BotApi, Update)) + Send + Sync + 'static>>>,
20 audio_handler: Option<Arc<Box<dyn Fn((BotApi, VAudioMessage)) + Send + Sync + 'static>>>,
21 document_handler: Option<Arc<Box<dyn Fn((BotApi, VDocumentMessage)) + Send + Sync + 'static>>>,
22 photo_handler: Option<Arc<Box<dyn Fn((BotApi, VPhotoMessage)) + Send + Sync + 'static>>>,
23 sticker_handler: Option<Arc<Box<dyn Fn((BotApi, VStickerMessage)) + Send + Sync + 'static>>>,
24 video_handler: Option<Arc<Box<dyn Fn((BotApi, VVideoMessage)) + Send + Sync + 'static>>>,
25 voice_handler: Option<Arc<Box<dyn Fn((BotApi, VVoiceMessage)) + Send + Sync + 'static>>>,
26 video_note_handler: Option<Arc<Box<dyn Fn((BotApi, VVideoNoteMessage)) + Send + Sync + 'static>>>,
27 contact_handler: Option<Arc<Box<dyn Fn((BotApi, VContactMessage)) + Send + Sync + 'static>>>,
28 location_handler: Option<Arc<Box<dyn Fn((BotApi, VLocationMessage)) + Send + Sync + 'static>>>,
29 venue_handler: Option<Arc<Box<dyn Fn((BotApi, VVenueMessage)) + Send + Sync + 'static>>>,
30 new_chat_members_handler: Option<Arc<Box<dyn Fn((BotApi, VNewChatMembersMessage)) + Send + Sync + 'static>>>,
31 left_chat_member_handler: Option<Arc<Box<dyn Fn((BotApi, VLeftChatMemberMessage)) + Send + Sync + 'static>>>,
32 chat_title_handler: Option<Arc<Box<dyn Fn((BotApi, VChatTitleMessage)) + Send + Sync + 'static>>>,
33 chat_photo_handler: Option<Arc<Box<dyn Fn((BotApi, VChatPhotoMessage)) + Send + Sync + 'static>>>,
34 delete_chat_photo_handler: Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>>,
35 group_chat_created_handler: Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>>,
36 supergroup_chat_created_handler: Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>>,
37 channel_chat_created_handler: Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>>,
38 migrate_to_chat_id_handler: Option<Arc<Box<dyn Fn((BotApi, VMigrateToChatIdMessage)) + Send + Sync + 'static>>>,
39 migrate_from_chat_id_handler: Option<Arc<Box<dyn Fn((BotApi, VMigrateFromChatIdMessage)) + Send + Sync + 'static>>>,
40 pinned_message_handler: Option<Arc<Box<dyn Fn((BotApi, VPinnedMessageMessage)) + Send + Sync + 'static>>>,
41
42 callback_query_handler: Option<Arc<Box<dyn Fn((BotApi, VCallbackQuery)) + Send + Sync + 'static>>>,
43 error_handler: Option<Arc<Box<dyn Fn((BotApi, String)) + Send + Sync + 'static>>>,
44
45 command_handler: HashMap<&'static str, Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>>,
46 precommand_handler: Option<Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>>,
47 none_command_handler: Option<Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>>,
48 incoming_handler: Option<Arc<Box<dyn Fn((BotApi, Incoming)) -> bool + Send + Sync + 'static>>>,
49}
50
51impl Default for Listener {
52 fn default() -> Self {
53 Listener {
54 text_handler: None,
55 update_handler: None,
56 audio_handler: None,
57 document_handler: None,
58 photo_handler: None,
59 sticker_handler: None,
60 video_handler: None,
61 voice_handler: None,
62 video_note_handler: None,
63 contact_handler: None,
64 location_handler: None,
65 venue_handler: None,
66 new_chat_members_handler: None,
67 left_chat_member_handler: None,
68 chat_title_handler: None,
69 chat_photo_handler: None,
70 delete_chat_photo_handler: None,
71 group_chat_created_handler: None,
72 supergroup_chat_created_handler: None,
73 channel_chat_created_handler: None,
74 migrate_to_chat_id_handler: None,
75 migrate_from_chat_id_handler: None,
76 pinned_message_handler: None,
77 callback_query_handler: None,
78 error_handler: None,
79 command_handler: HashMap::new(),
80 precommand_handler: None,
81 none_command_handler: None,
82 incoming_handler: None
83 }
84 }
85}
86
87
88impl Listener {
89 pub fn on_update<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Update)) + Send + Sync + 'static {
90 self.update_handler = Some(Arc::new(Box::new(fnc)));
91 self
92 }
93
94 pub fn on_callback_query<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VCallbackQuery)) + Send + Sync + 'static {
95 self.callback_query_handler = Some(Arc::new(Box::new(fnc)));
96 self
97 }
98
99 pub fn on_error<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, String)) + Send + Sync + 'static {
100 self.error_handler = Some(Arc::new(Box::new(fnc)));
101 self
102 }
103
104
105 pub fn on_command<S, F>(&mut self, command: S, fnc: F) -> &mut Self where
106 S: AsRef<str> + 'static,
107 F: Fn((BotApi, VCommand)) + Send + Sync + 'static {
108 let mut command = command.as_ref();
109 if command.starts_with("/") {
110 command = &command[1..];
111 }
112 let command: &'static str = Box::leak(command.to_string().into_boxed_str());
113 self.command_handler.insert(command, Arc::new(Box::new(fnc)));
114 self
115 }
116
117 pub fn on_incoming<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Incoming)) -> bool + Send + Sync + 'static {
118 self.incoming_handler = Some(Arc::new(Box::new(fnc)));
119 self
120 }
121
122 pub fn on_precommand<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VCommand)) + Send + Sync + 'static {
123 self.precommand_handler = Some(Arc::new(Box::new(fnc)));
124 self
125 }
126
127 pub fn on_none_command<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VCommand)) + Send + Sync + 'static {
128 self.none_command_handler = Some(Arc::new(Box::new(fnc)));
129 self
130 }
131
132 pub fn on_text<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VTextMessage)) + Send + Sync + 'static {
133 self.text_handler = Some(Arc::new(Box::new(fnc)));
134 self
135 }
136
137 pub fn on_audio<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VAudioMessage)) + Send + Sync + 'static {
138 self.audio_handler = Some(Arc::new(Box::new(fnc)));
139 self
140 }
141
142
143 pub fn on_document<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VDocumentMessage)) + Send + Sync + 'static {
144 self.document_handler = Some(Arc::new(Box::new(fnc)));
145 self
146 }
147
148 pub fn on_photo<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VPhotoMessage)) + Send + Sync + 'static {
149 self.photo_handler = Some(Arc::new(Box::new(fnc)));
150 self
151 }
152
153 pub fn on_sticker<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VStickerMessage)) + Send + Sync + 'static {
154 self.sticker_handler = Some(Arc::new(Box::new(fnc)));
155 self
156 }
157
158 pub fn on_video<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VVideoMessage)) + Send + Sync + 'static {
159 self.video_handler = Some(Arc::new(Box::new(fnc)));
160 self
161 }
162
163 pub fn on_voice<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VVoiceMessage)) + Send + Sync + 'static {
164 self.voice_handler = Some(Arc::new(Box::new(fnc)));
165 self
166 }
167
168 pub fn on_video_note<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VVideoNoteMessage)) + Send + Sync + 'static {
169 self.video_note_handler = Some(Arc::new(Box::new(fnc)));
170 self
171 }
172
173 pub fn on_contact<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VContactMessage)) + Send + Sync + 'static {
174 self.contact_handler = Some(Arc::new(Box::new(fnc)));
175 self
176 }
177
178 pub fn on_location<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VLocationMessage)) + Send + Sync + 'static {
179 self.location_handler = Some(Arc::new(Box::new(fnc)));
180 self
181 }
182
183 pub fn on_venue<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VVenueMessage)) + Send + Sync + 'static {
184 self.venue_handler = Some(Arc::new(Box::new(fnc)));
185 self
186 }
187
188 pub fn on_new_chat_members<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VNewChatMembersMessage)) + Send + Sync + 'static {
189 self.new_chat_members_handler = Some(Arc::new(Box::new(fnc)));
190 self
191 }
192
193 pub fn on_left_chat_member<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VLeftChatMemberMessage)) + Send + Sync + 'static {
194 self.left_chat_member_handler = Some(Arc::new(Box::new(fnc)));
195 self
196 }
197
198 pub fn on_new_chat_title<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VChatTitleMessage)) + Send + Sync + 'static {
199 self.chat_title_handler = Some(Arc::new(Box::new(fnc)));
200 self
201 }
202
203 pub fn on_new_chat_photo<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VChatPhotoMessage)) + Send + Sync + 'static {
204 self.chat_photo_handler = Some(Arc::new(Box::new(fnc)));
205 self
206 }
207
208 pub fn on_delete_chat_photo<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Message)) + Send + Sync + 'static {
209 self.delete_chat_photo_handler = Some(Arc::new(Box::new(fnc)));
210 self
211 }
212
213 pub fn on_group_chat_created<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Message)) + Send + Sync + 'static {
214 self.group_chat_created_handler = Some(Arc::new(Box::new(fnc)));
215 self
216 }
217
218 pub fn on_supergroup_chat_created<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Message)) + Send + Sync + 'static {
219 self.supergroup_chat_created_handler = Some(Arc::new(Box::new(fnc)));
220 self
221 }
222
223 pub fn on_channel_chat_create<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Message)) + Send + Sync + 'static {
224 self.channel_chat_created_handler = Some(Arc::new(Box::new(fnc)));
225 self
226 }
227
228 pub fn on_migrate_to_chat<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VMigrateToChatIdMessage)) + Send + Sync + 'static {
229 self.migrate_to_chat_id_handler = Some(Arc::new(Box::new(fnc)));
230 self
231 }
232
233 pub fn on_migrate_from_chat<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VMigrateFromChatIdMessage)) + Send + Sync + 'static {
234 self.migrate_from_chat_id_handler = Some(Arc::new(Box::new(fnc)));
235 self
236 }
237
238 pub fn on_pinned<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VPinnedMessageMessage)) + Send + Sync + 'static {
239 self.pinned_message_handler = Some(Arc::new(Box::new(fnc)));
240 self
241 }
242}
243
244pub struct Lout {
245 listener: Listener
246}
247
248impl Lout {
249 pub fn new(listener: Listener) -> Self {
250 Lout { listener }
251 }
252
253 pub fn listen_precommand(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>> {
254 &self.listener.precommand_handler
255 }
256
257 pub fn listen_none_command(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>> {
258 &self.listener.none_command_handler
259 }
260
261 pub fn listen_incoming(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Incoming)) -> bool + Send + Sync + 'static>>> {
262 &self.listener.incoming_handler
263 }
264
265 pub fn listen_command(&self) -> &HashMap<&'static str, Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>> {
266 &self.listener.command_handler
267 }
268
269 pub fn listen_error(&self) -> &Option<Arc<Box<dyn Fn((BotApi, String)) + Send + Sync + 'static>>> {
270 &self.listener.error_handler
271 }
272
273 pub fn listen_callback_query(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VCallbackQuery)) + Send + Sync + 'static>>> {
274 &self.listener.callback_query_handler
275 }
276
277 pub fn listen_update(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Update)) + Send + Sync + 'static>>> {
278 &self.listener.update_handler
279 }
280
281 pub fn listen_text(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VTextMessage)) + Send + Sync + 'static>>> {
282 &self.listener.text_handler
283 }
284
285 pub fn listen_audio(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VAudioMessage)) + Send + Sync + 'static>>> {
286 &self.listener.audio_handler
287 }
288
289 pub fn listen_document(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VDocumentMessage)) + Send + Sync + 'static>>> {
290 &self.listener.document_handler
291 }
292
293 pub fn listen_photo(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VPhotoMessage)) + Send + Sync + 'static>>> {
294 &self.listener.photo_handler
295 }
296
297 pub fn listen_sticker(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VStickerMessage)) + Send + Sync + 'static>>> {
298 &self.listener.sticker_handler
299 }
300
301 pub fn listen_video(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VVideoMessage)) + Send + Sync + 'static>>> {
302 &self.listener.video_handler
303 }
304
305 pub fn listen_voice(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VVoiceMessage)) + Send + Sync + 'static>>> {
306 &self.listener.voice_handler
307 }
308
309 pub fn listen_video_note(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VVideoNoteMessage)) + Send + Sync + 'static>>> {
310 &self.listener.video_note_handler
311 }
312
313 pub fn listen_contact(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VContactMessage)) + Send + Sync + 'static>>> {
314 &self.listener.contact_handler
315 }
316
317 pub fn listen_location(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VLocationMessage)) + Send + Sync + 'static>>> {
318 &self.listener.location_handler
319 }
320
321 pub fn listen_venue(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VVenueMessage)) + Send + Sync + 'static>>> {
322 &self.listener.venue_handler
323 }
324
325 pub fn listen_new_chat_members(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VNewChatMembersMessage)) + Send + Sync + 'static>>> {
326 &self.listener.new_chat_members_handler
327 }
328
329 pub fn listen_left_chat_member(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VLeftChatMemberMessage)) + Send + Sync + 'static>>> {
330 &self.listener.left_chat_member_handler
331 }
332
333 pub fn listen_new_chat_title(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VChatTitleMessage)) + Send + Sync + 'static>>> {
334 &self.listener.chat_title_handler
335 }
336
337 pub fn listen_new_chat_photo(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VChatPhotoMessage)) + Send + Sync + 'static>>> {
338 &self.listener.chat_photo_handler
339 }
340
341 pub fn listen_delete_chat_photo(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>> {
342 &self.listener.delete_chat_photo_handler
343 }
344
345 pub fn listen_group_chat_created(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>> {
346 &self.listener.group_chat_created_handler
347 }
348
349 pub fn listen_supergroup_chat_created(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>> {
350 &self.listener.supergroup_chat_created_handler
351 }
352
353 pub fn listen_channel_chat_create(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>> {
354 &self.listener.channel_chat_created_handler
355 }
356
357 pub fn listen_migrate_to_chat(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VMigrateToChatIdMessage)) + Send + Sync + 'static>>> {
358 &self.listener.migrate_to_chat_id_handler
359 }
360
361 pub fn listen_migrate_from_chat(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VMigrateFromChatIdMessage)) + Send + Sync + 'static>>> {
362 &self.listener.migrate_from_chat_id_handler
363 }
364
365 pub fn listen_pinned(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VPinnedMessageMessage)) + Send + Sync + 'static>>> {
366 &self.listener.pinned_message_handler
367 }
368}