SHARE
TWEET

Untitled

a guest Jun 25th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern crate clokwerk;
  2. pub mod api;
  3.  
  4. use api::types::*;
  5. use clokwerk::{Scheduler, TimeUnits};
  6. use std::thread;
  7. use std::time::Duration;
  8.  
  9. pub struct Bot {
  10.     token: String,
  11.     refresh_time: u32,
  12. }
  13.  
  14. impl Bot {
  15.     pub fn new(token: String, refresh_time: u32) -> Bot {
  16.         println!("Creating a new bot with token: {}, refresh time: {}", token, refresh_time);
  17.        
  18.         Bot {token, refresh_time}
  19.     }
  20.  
  21.     pub fn get_token(self) -> String {
  22.         self.token
  23.     }
  24.  
  25.     pub fn start(self, handler: &Fn(Update)) {
  26.         let mut scheduler = Scheduler::new();
  27.         scheduler.every(self.refresh_time.seconds()).run(move || Bot::process_updates(&self.token, handler));
  28.         loop {
  29.             scheduler.run_pending();
  30.             thread::sleep(Duration::from_millis(200));
  31.         }
  32.     }
  33.  
  34.     fn process_updates(token: &String, handler: &Fn(Update)) {
  35.         let updates = api::get_updates(token).result;
  36.         for update in updates {
  37.             handler(update)
  38.         }
  39.     }
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top