-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpager.js
115 lines (100 loc) · 4.48 KB
/
pager.js
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
const path = require('path');
require('dotenv').config({path: path.resolve(__dirname, '.env')});
const fs = require('fs');
const fb = require('facebook-chat-api');
const chalk = require('chalk');
const { showNotification } = require('./notifications');
const appStateFile = path.format({dir: __dirname, base: 'appstate.json'});
const botMessage = process.argv[2];
const options = {
userAgent: 'Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Mobile Safari/537.36',
listenEvents: true
}
let username;
let repliedTo = [];
console.log(chalk.magenta.bold('Starting Facebook Pager...'));
const appLogFile = path.format({dir: __dirname, base: `${Date.now()}.txt`});
const appLog = fs.createWriteStream(appLogFile, {flags: 'a+'});
console.log(chalk`{magenta.bold Log file created in ${appLogFile}}`);
if( !fs.existsSync(appStateFile) ){
console.log(chalk.yellowBright.bold('appstate.json file not found. Logging in with .env file credentials...'));
fb({email: process.env.FB_EMAIL, password: process.env.FB_PWD}, options, (err, api) => {
if( err ){
appLog.write(err.error);
appLog.end();
return console.log(chalk.red.bold(err.error));
}
fs.writeFileSync(appStateFile, JSON.stringify(api.getAppState()));
console.log(chalk.yellowBright.bold('appstate.json file created!'));
console.log(chalk.magenta.bold('Listening for incoming messages...'));
api.listenMqtt( (err, event) => {
if( err ){
appLog.write(err.error);
appLog.end();
return console.log(chalk.red.bold(err.error));
}
if( event.type === 'message' ){
api.getUserInfo(event.senderID, (err, user) => {
if( err ){
appLog.write(err.error);
appLog.end();
return console.log(chalk.red.bold(err.error));
}
for( var prop in user ){
username = user[prop].name;
}
if( !repliedTo.includes(event.threadID) ){
repliedTo.push(event.threadID);
api.sendMessage(`Facebook Pager v1.3\n${botMessage}`, event.threadID);
}
showNotification(username, event.body);
console.log(chalk`{magenta.bold New message from ${username}:}\n${event.body}`);
appLog.write(`${username}:\n`);
appLog.write(`${event.body}\n`);
});
}
});
});
}else{
console.log(chalk.yellowBright.bold('Loading appstate.json file!'));
fb({appState: JSON.parse(fs.readFileSync(appStateFile))}, (err, api) => {
if( err ){
appLog.write(err.error);
appLog.end();
return console.log(chalk.red.bold(err.error));
}
api.setOptions({
listenEvents: true
});
fs.writeFileSync(appStateFile, JSON.stringify(api.getAppState()));
console.log(chalk.yellowBright.bold('appstate.json file updated!'));
console.log(chalk.magenta.bold('Listening for incoming messages...'));
api.listenMqtt( (err, event) => {
if( err ){
appLog.write(err.error);
appLog.end();
return console.log(chalk.red.bold(err.error));
}
if( event.type === 'message' ){
api.getUserInfo(event.senderID, (err, user) => {
if( err ){
appLog.write(err.error);
appLog.end();
return console.log(chalk.red.bold(err.error));
}
for(var prop in user){
username = user[prop].name;
}
if( !repliedTo.includes(event.threadID) ){
repliedTo.push(event.threadID);
api.sendMessage(`Facebook Pager v1.3\n${botMessage}`, event.threadID);
}
showNotification(username, event.body);
console.log(chalk`{magenta.bold New message from ${username}:}\n${event.body}`);
appLog.write(`${username}:\n`);
appLog.write(`${event.body}\n`);
});
}
});
});
}