-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
148 lines (116 loc) · 4.62 KB
/
app.js
File metadata and controls
148 lines (116 loc) · 4.62 KB
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
const fs = require('fs-extra');
const fb = require("facebook-chat-api");
const config = require('./config.json');
const escapeJSON = require('escape-json-node');
var timestamp = undefined;
var messageDefaultCount = 50, messageCount = undefined;
login();
function login() {
fb({appState: JSON.parse(fs.readFileSync(config.path.tmp + 'appstate.json', 'utf8'))}, (err, api) => {
//Error Handling
if(err) return console.error(err);
//Set options
api.setOptions({
listenEvents: true,
selfListen: true
});
//Start Listening for chats
listen(api);
//Backup old chats
// api.getThreadList(0, 1, 'inbox', (err, arr) => {
// arr.forEach(function(thread){
// getAllMessages(api, thread)
// });
// })
});
}
function getAllMessages(api, thread) {
loadNextThreadHistory(api,thread.threadID)
}
function loadNextThreadHistory(api, threadID){
api.getThreadHistory(threadID, messageDefaultCount, timestamp, (err, history) => {
if(err) return console.error(err);
messageCount = Object.keys(history).length;
if(timestamp != undefined) history.pop();
timestamp = history[0].timestamp;
history.reverse().forEach(function(message){
})
if (messageCount >= messageDefaultCount) {
loadNextThreadHistory(api,threadID);
}
});
}
function listen(api) {
var stopListening = api.listen((err, event) => {
if(err) return console.error(err);
// api.markAsRead(event.threadID, (err) => {
// if(err) console.error(err);
// });
// console.log(event.body);
console.log(event);
switch(event.type) {
case "message":
datpath = config.path.data + api.getCurrentUserID();
ensureExists(datpath, 0744, function(err) {
if (err) {
console.log(err);
} else {
filepath = datpath + '/' + event.threadID + '.json';
fs.ensureFileSync(filepath)
var data = fs.readFileSync(filepath, 'utf8');
// console.log(data);
var json = JSON.parse(data);
json.push(event);
fs.writeFileSync(filepath, JSON.stringify(json), 'utf8');
}
});
if(event.body === '/ciao-adios') {
api.sendMessage("Goodbye…", event.threadID);
return stopListening();
}
// api.sendMessage("TEST BOT: " + event.body, event.threadID);
break;
case "event":
// console.log(event);
break;
}
});
}
function ensureExists(path, mask, cb) {
if (typeof mask == 'function') { // allow the `mask` parameter to be optional
cb = mask;
mask = 0777;
}
fs.mkdir(path, mask, function(err) {
if (err) {
if (err.code == 'EEXIST') cb(null); // ignore the error if the folder already exists
else cb(err); // something else went wrong
} else cb(null); // successfully created folder
});
}
// switch(message.type) {
// case 'message':
// var attachments = message.attachments;
// if (Object.keys(attachments).length != 0) {
// attachments.forEach(function(attachment){
// switch(attachment.type) {
// case 'photo':
// console.log('[photo] ' + message.senderName + ': ' + attachment.largePreviewUrl);
// break;
// case 'sticker':
// console.log('[sticker] ' + message.senderName + ': ' + attachment.url);
// break;
// default:
// console.log(attachment);
// }
// })
// } else {
// console.log('[message] ' + message.senderName + ': ' + message.body);
// }
// break;
// case 'event':
// console.log('[event] ' + message.senderName + ': ' + message.logMessageType);
// break;
// default:
// console.log(message);
// }