Advertisement
Guest User

Untitled

a guest
May 25th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs");
  2. const login = require("facebook-chat-api");
  3. const fetch = require("node-fetch");
  4. let stat = [];
  5. let text = "";
  6. // Simple echo bot. It will repeat everything that you say.
  7. // Will stop when you say '/stop'
  8. login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
  9.     if(err) return console.error(err);
  10.     api.setOptions({listenEvents: true});
  11.     function findThread(threadID)
  12.     {
  13.         for( let i = 0; i < stat.length; i++ ){
  14.             if( threadID == stat[i][0]){
  15.                 console.log(threadID + "  " + stat[i][0]);
  16.                 return [true, i];
  17.             }
  18.         }
  19.         return false;
  20.     }
  21.     function checkTime(epoch)
  22.     {
  23.         let ts = Math.round((new Date()).getTime() / 1000);
  24.         console.log("epoch: " + epoch + " TS: " + ts + "dif: " + (ts - epoch));
  25.         if( (ts - epoch) > 1800 ){
  26.             return true;
  27.         }else{
  28.             return false;
  29.         }
  30.     }
  31.     function sendResponse(threadID){
  32.         api.sendMessage("Edwin is trying to sleep right now ~Robo Edwin",threadID);
  33.     }
  34.     var stopListening = api.listen((err, event) => {
  35.         if(err) return console.error(err);
  36.         api.markAsRead(event.threadID, (err) => {
  37.             if(err) console.error(err);
  38.         });
  39.         switch(event.type) {
  40.             case "message":
  41.                 let foundThread = findThread(event.threadID);
  42.  
  43.                 if (foundThread[0] == true){
  44.                     let epoch = stat[foundThread[1]][2];
  45.                     if (checkTime(epoch)){
  46.                         console.log("we can message.");
  47.                         sendResponse(stat[foundThread[1]][0]);
  48.                         stat[foundThread[1]][2] = Math.round((new Date()).getTime() / 1000);
  49.                     }else
  50.                     {
  51.                         console.log("we should wait to message this person.");
  52.                     }
  53.  
  54.                 }else{
  55.                     console.log("not Present");
  56.                     let ts = Math.round((new Date()).getTime() / 1000);
  57.                     stat.push([event.threadID,false, ts]);
  58.                     console.log("we have not messaged this person.");
  59.                     sendResponse(event.threadID);
  60.                     stat[stat.length - 1][1] = true;
  61.                 }
  62.                 break;
  63.             case "event":
  64.                 console.log(event);
  65.                 break;
  66.         }
  67.     });
  68. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement