Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require("fs");
- const login = require("facebook-chat-api");
- const fetch = require("node-fetch");
- let stat = [];
- let text = "";
- // Simple echo bot. It will repeat everything that you say.
- // Will stop when you say '/stop'
- login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
- if(err) return console.error(err);
- api.setOptions({listenEvents: true});
- function findThread(threadID)
- {
- for( let i = 0; i < stat.length; i++ ){
- if( threadID == stat[i][0]){
- console.log(threadID + " " + stat[i][0]);
- return [true, i];
- }
- }
- return false;
- }
- function checkTime(epoch)
- {
- let ts = Math.round((new Date()).getTime() / 1000);
- console.log("epoch: " + epoch + " TS: " + ts + "dif: " + (ts - epoch));
- if( (ts - epoch) > 1800 ){
- return true;
- }else{
- return false;
- }
- }
- function sendResponse(threadID){
- api.sendMessage("Edwin is trying to sleep right now ~Robo Edwin",threadID);
- }
- var stopListening = api.listen((err, event) => {
- if(err) return console.error(err);
- api.markAsRead(event.threadID, (err) => {
- if(err) console.error(err);
- });
- switch(event.type) {
- case "message":
- let foundThread = findThread(event.threadID);
- if (foundThread[0] == true){
- let epoch = stat[foundThread[1]][2];
- if (checkTime(epoch)){
- console.log("we can message.");
- sendResponse(stat[foundThread[1]][0]);
- stat[foundThread[1]][2] = Math.round((new Date()).getTime() / 1000);
- }else
- {
- console.log("we should wait to message this person.");
- }
- }else{
- console.log("not Present");
- let ts = Math.round((new Date()).getTime() / 1000);
- stat.push([event.threadID,false, ts]);
- console.log("we have not messaged this person.");
- sendResponse(event.threadID);
- stat[stat.length - 1][1] = true;
- }
- break;
- case "event":
- console.log(event);
- break;
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement