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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use std::collections::HashMap;
use std::sync::Arc;

use crate::api::BotApi;
use crate::types::Update;
use crate::vision::*;

//#[derive(Debug, Eq, PartialEq, Hash)]
//pub enum ListenerType {
//  Text,
//  Command,
//  CallbackQuery,
//  Update,
//}

#[derive(Clone)]
pub struct Listener {
  text_handler: Option<Arc<Box<dyn Fn((BotApi, VTextMessage)) + Send + Sync + 'static>>>,
  update_handler: Option<Arc<Box<dyn Fn((BotApi, Update)) + Send + Sync + 'static>>>,
  audio_handler: Option<Arc<Box<dyn Fn((BotApi, VAudioMessage)) + Send + Sync + 'static>>>,
  document_handler: Option<Arc<Box<dyn Fn((BotApi, VDocumentMessage)) + Send + Sync + 'static>>>,
  photo_handler: Option<Arc<Box<dyn Fn((BotApi, VPhotoMessage)) + Send + Sync + 'static>>>,
  sticker_handler: Option<Arc<Box<dyn Fn((BotApi, VStickerMessage)) + Send + Sync + 'static>>>,
  video_handler: Option<Arc<Box<dyn Fn((BotApi, VVideoMessage)) + Send + Sync + 'static>>>,
  voice_handler: Option<Arc<Box<dyn Fn((BotApi, VVoiceMessage)) + Send + Sync + 'static>>>,
  video_note_handler: Option<Arc<Box<dyn Fn((BotApi, VVideoNoteMessage)) + Send + Sync + 'static>>>,
  contact_handler: Option<Arc<Box<dyn Fn((BotApi, VContactMessage)) + Send + Sync + 'static>>>,
  location_handler: Option<Arc<Box<dyn Fn((BotApi, VLocationMessage)) + Send + Sync + 'static>>>,
  venue_handler: Option<Arc<Box<dyn Fn((BotApi, VVenueMessage)) + Send + Sync + 'static>>>,
  new_chat_members_handler: Option<Arc<Box<dyn Fn((BotApi, VNewChatMembersMessage)) + Send + Sync + 'static>>>,
  left_chat_member_handler: Option<Arc<Box<dyn Fn((BotApi, VLeftChatMemberMessage)) + Send + Sync + 'static>>>,
  chat_title_handler: Option<Arc<Box<dyn Fn((BotApi, VChatTitleMessage)) + Send + Sync + 'static>>>,
  chat_photo_handler: Option<Arc<Box<dyn Fn((BotApi, VChatPhotoMessage)) + Send + Sync + 'static>>>,
  delete_chat_photo_handler: Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>>,
  group_chat_created_handler: Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>>,
  supergroup_chat_created_handler: Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>>,
  channel_chat_created_handler: Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>>,
  migrate_to_chat_id_handler: Option<Arc<Box<dyn Fn((BotApi, VMigrateToChatIdMessage)) + Send + Sync + 'static>>>,
  migrate_from_chat_id_handler: Option<Arc<Box<dyn Fn((BotApi, VMigrateFromChatIdMessage)) + Send + Sync + 'static>>>,
  pinned_message_handler: Option<Arc<Box<dyn Fn((BotApi, VPinnedMessageMessage)) + Send + Sync + 'static>>>,

  callback_query_handler: Option<Arc<Box<dyn Fn((BotApi, VCallbackQuery)) + Send + Sync + 'static>>>,
  error_handler: Option<Arc<Box<dyn Fn((BotApi, String)) + Send + Sync + 'static>>>,

  command_handler: HashMap<&'static str, Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>>,
  precommand_handler: Option<Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>>,
  none_command_handler: Option<Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>>,
  incoming_handler: Option<Arc<Box<dyn Fn((BotApi, Incoming)) -> bool + Send + Sync + 'static>>>,
}

impl Default for Listener {
  fn default() -> Self {
    Listener {
      text_handler: None,
      update_handler: None,
      audio_handler: None,
      document_handler: None,
      photo_handler: None,
      sticker_handler: None,
      video_handler: None,
      voice_handler: None,
      video_note_handler: None,
      contact_handler: None,
      location_handler: None,
      venue_handler: None,
      new_chat_members_handler: None,
      left_chat_member_handler: None,
      chat_title_handler: None,
      chat_photo_handler: None,
      delete_chat_photo_handler: None,
      group_chat_created_handler: None,
      supergroup_chat_created_handler: None,
      channel_chat_created_handler: None,
      migrate_to_chat_id_handler: None,
      migrate_from_chat_id_handler: None,
      pinned_message_handler: None,
      callback_query_handler: None,
      error_handler: None,
      command_handler: HashMap::new(),
      precommand_handler: None,
      none_command_handler: None,
      incoming_handler: None
    }
  }
}


impl Listener {
  pub fn on_update<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Update)) + Send + Sync + 'static {
    self.update_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_callback_query<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VCallbackQuery)) + Send + Sync + 'static {
    self.callback_query_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_error<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, String)) + Send + Sync + 'static {
    self.error_handler = Some(Arc::new(Box::new(fnc)));
    self
  }


  pub fn on_command<S, F>(&mut self, command: S, fnc: F) -> &mut Self where
    S: AsRef<str> + 'static,
    F: Fn((BotApi, VCommand)) + Send + Sync + 'static {
    let mut command = command.as_ref();
    if command.starts_with("/") {
      command = &command[1..];
    }
    let command: &'static str = Box::leak(command.to_string().into_boxed_str());
    self.command_handler.insert(command, Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_incoming<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Incoming)) -> bool + Send + Sync + 'static {
    self.incoming_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_precommand<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VCommand)) + Send + Sync + 'static {
    self.precommand_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_none_command<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VCommand)) + Send + Sync + 'static {
    self.none_command_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_text<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VTextMessage)) + Send + Sync + 'static {
    self.text_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_audio<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VAudioMessage)) + Send + Sync + 'static {
    self.audio_handler = Some(Arc::new(Box::new(fnc)));
    self
  }


  pub fn on_document<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VDocumentMessage)) + Send + Sync + 'static {
    self.document_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_photo<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VPhotoMessage)) + Send + Sync + 'static {
    self.photo_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_sticker<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VStickerMessage)) + Send + Sync + 'static {
    self.sticker_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_video<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VVideoMessage)) + Send + Sync + 'static {
    self.video_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_voice<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VVoiceMessage)) + Send + Sync + 'static {
    self.voice_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_video_note<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VVideoNoteMessage)) + Send + Sync + 'static {
    self.video_note_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_contact<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VContactMessage)) + Send + Sync + 'static {
    self.contact_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_location<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VLocationMessage)) + Send + Sync + 'static {
    self.location_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_venue<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VVenueMessage)) + Send + Sync + 'static {
    self.venue_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_new_chat_members<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VNewChatMembersMessage)) + Send + Sync + 'static {
    self.new_chat_members_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_left_chat_member<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VLeftChatMemberMessage)) + Send + Sync + 'static {
    self.left_chat_member_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_new_chat_title<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VChatTitleMessage)) + Send + Sync + 'static {
    self.chat_title_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_new_chat_photo<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VChatPhotoMessage)) + Send + Sync + 'static {
    self.chat_photo_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_delete_chat_photo<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Message)) + Send + Sync + 'static {
    self.delete_chat_photo_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_group_chat_created<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Message)) + Send + Sync + 'static {
    self.group_chat_created_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_supergroup_chat_created<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Message)) + Send + Sync + 'static {
    self.supergroup_chat_created_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_channel_chat_create<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, Message)) + Send + Sync + 'static {
    self.channel_chat_created_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_migrate_to_chat<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VMigrateToChatIdMessage)) + Send + Sync + 'static {
    self.migrate_to_chat_id_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_migrate_from_chat<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VMigrateFromChatIdMessage)) + Send + Sync + 'static {
    self.migrate_from_chat_id_handler = Some(Arc::new(Box::new(fnc)));
    self
  }

  pub fn on_pinned<F>(&mut self, fnc: F) -> &mut Self where F: Fn((BotApi, VPinnedMessageMessage)) + Send + Sync + 'static {
    self.pinned_message_handler = Some(Arc::new(Box::new(fnc)));
    self
  }
}

pub struct Lout {
  listener: Listener
}

impl Lout {
  pub fn new(listener: Listener) -> Self {
    Lout { listener }
  }

  pub fn listen_precommand(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>> {
    &self.listener.precommand_handler
  }

  pub fn listen_none_command(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>> {
    &self.listener.none_command_handler
  }

  pub fn listen_incoming(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Incoming)) -> bool + Send + Sync + 'static>>> {
    &self.listener.incoming_handler
  }

  pub fn listen_command(&self) -> &HashMap<&'static str, Arc<Box<dyn Fn((BotApi, VCommand)) + Send + Sync + 'static>>> {
    &self.listener.command_handler
  }

  pub fn listen_error(&self) -> &Option<Arc<Box<dyn Fn((BotApi, String)) + Send + Sync + 'static>>> {
    &self.listener.error_handler
  }

  pub fn listen_callback_query(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VCallbackQuery)) + Send + Sync + 'static>>> {
    &self.listener.callback_query_handler
  }

  pub fn listen_update(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Update)) + Send + Sync + 'static>>> {
    &self.listener.update_handler
  }

  pub fn listen_text(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VTextMessage)) + Send + Sync + 'static>>> {
    &self.listener.text_handler
  }

  pub fn listen_audio(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VAudioMessage)) + Send + Sync + 'static>>> {
    &self.listener.audio_handler
  }

  pub fn listen_document(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VDocumentMessage)) + Send + Sync + 'static>>> {
    &self.listener.document_handler
  }

  pub fn listen_photo(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VPhotoMessage)) + Send + Sync + 'static>>> {
    &self.listener.photo_handler
  }

  pub fn listen_sticker(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VStickerMessage)) + Send + Sync + 'static>>> {
    &self.listener.sticker_handler
  }

  pub fn listen_video(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VVideoMessage)) + Send + Sync + 'static>>> {
    &self.listener.video_handler
  }

  pub fn listen_voice(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VVoiceMessage)) + Send + Sync + 'static>>> {
    &self.listener.voice_handler
  }

  pub fn listen_video_note(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VVideoNoteMessage)) + Send + Sync + 'static>>> {
    &self.listener.video_note_handler
  }

  pub fn listen_contact(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VContactMessage)) + Send + Sync + 'static>>> {
    &self.listener.contact_handler
  }

  pub fn listen_location(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VLocationMessage)) + Send + Sync + 'static>>> {
    &self.listener.location_handler
  }

  pub fn listen_venue(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VVenueMessage)) + Send + Sync + 'static>>> {
    &self.listener.venue_handler
  }

  pub fn listen_new_chat_members(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VNewChatMembersMessage)) + Send + Sync + 'static>>> {
    &self.listener.new_chat_members_handler
  }

  pub fn listen_left_chat_member(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VLeftChatMemberMessage)) + Send + Sync + 'static>>> {
    &self.listener.left_chat_member_handler
  }

  pub fn listen_new_chat_title(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VChatTitleMessage)) + Send + Sync + 'static>>> {
    &self.listener.chat_title_handler
  }

  pub fn listen_new_chat_photo(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VChatPhotoMessage)) + Send + Sync + 'static>>> {
    &self.listener.chat_photo_handler
  }

  pub fn listen_delete_chat_photo(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>> {
    &self.listener.delete_chat_photo_handler
  }

  pub fn listen_group_chat_created(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>> {
    &self.listener.group_chat_created_handler
  }

  pub fn listen_supergroup_chat_created(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>> {
    &self.listener.supergroup_chat_created_handler
  }

  pub fn listen_channel_chat_create(&self) -> &Option<Arc<Box<dyn Fn((BotApi, Message)) + Send + Sync + 'static>>> {
    &self.listener.channel_chat_created_handler
  }

  pub fn listen_migrate_to_chat(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VMigrateToChatIdMessage)) + Send + Sync + 'static>>> {
    &self.listener.migrate_to_chat_id_handler
  }

  pub fn listen_migrate_from_chat(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VMigrateFromChatIdMessage)) + Send + Sync + 'static>>> {
    &self.listener.migrate_from_chat_id_handler
  }

  pub fn listen_pinned(&self) -> &Option<Arc<Box<dyn Fn((BotApi, VPinnedMessageMessage)) + Send + Sync + 'static>>> {
    &self.listener.pinned_message_handler
  }
}