SHARE
TWEET

Untitled

a guest Jul 16th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::fs::{File, OpenOptions, remove_file};
  2.  
  3. use image::Luma;
  4.  
  5. use whatsappweb::connection::*;
  6. use whatsappweb::message::{Direction, Peer, ChatMessageContent, ChatMessage};
  7.  
  8. mod message;
  9.  
  10. const SESSION_FILENAME: &str = "session.bin";
  11.  
  12. struct Handler {}
  13.  
  14. impl WhatsappWebHandler for Handler {
  15.     fn on_state_changed(&self, _connection: &WhatsappWebConnection<Handler>, state: State) {
  16.         println!("new state: {:?}", state);
  17.     }
  18.  
  19.     fn on_persistent_session_data_changed(&self, persistent_session: PersistentSession) {
  20.         bincode::serialize_into(OpenOptions::new().create(true).write(true).open(SESSION_FILENAME).unwrap(), &persistent_session).unwrap();
  21.     }
  22.     fn on_user_data_changed(&self, _connection: &WhatsappWebConnection<Handler>, user_data: UserData) {
  23.         println!("userdata changed: {:?}", user_data);
  24.     }
  25.     fn on_disconnect(&self, reason: whatsappweb::connection::DisconnectReason) {
  26.         println!("disconnected");
  27.         match reason {
  28.             whatsappweb::connection::DisconnectReason::Removed => {
  29.                 remove_file(SESSION_FILENAME).unwrap();
  30.             }
  31.             _ => {}
  32.         }
  33.     }
  34.     fn on_message(&self, connection: &WhatsappWebConnection<Handler>, message_new: bool, message_box: Box<ChatMessage>) {
  35.         if !message_new {
  36.             return;
  37.         }
  38.         let message = *message_box;
  39.  
  40.         let peer = match message.direction {
  41.             Direction::Receiving(peer) => peer,
  42.             _ => return
  43.         };
  44.  
  45.         let sender_jid = match &peer {
  46.             &Peer::Individual(ref jid) => jid,
  47.             _ => return
  48.         };
  49.        
  50.         connection.send_message_read(message.id.clone(), peer.clone());
  51.  
  52.         match sender_jid.is_group {
  53.             false => {
  54.                 match message.content {
  55.                     ChatMessageContent::Text(text) => {
  56.                         message::on_text_message(text, sender_jid);
  57.                     }
  58.                     _ => {
  59.                         message::on_media_message(message.content, sender_jid);
  60.                     }
  61.                 }
  62.             }
  63.             _ => {}
  64.         }
  65.     }
  66. }
  67.  
  68. pub fn start() {
  69.     let handler = Handler {};
  70.  
  71.     if let Ok(file) = File::open(SESSION_FILENAME) {
  72.         let (_, join_handle) = whatsappweb::connection::with_persistent_session(bincode::deserialize_from(file).unwrap(), handler);
  73.         join_handle.join().unwrap();
  74.     } else {
  75.         let (_, join_handle) = whatsappweb::connection::new(|qr| { qr.render::<Luma<u8>>().module_dimensions(10, 10).build().save("login_qr.png").unwrap(); }, handler);
  76.         join_handle.join().unwrap();
  77.     }
  78. }
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
 
Top