-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
71 lines (63 loc) · 2.12 KB
/
index.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
import login from 'facebook-chat-api';
import readlineSync from 'readline-sync';
import credentials from './credentials';
import moment from 'moment';
const googleTranslate = require('google-translate')(credentials.translateAPIKey);
async function getInput(){
let userInput = await readlineSync.question('> ');
return userInput;
}
async function translate(fromLanguage, toLanguage, input){
return new Promise(async function(resolve, reject){
try{
await googleTranslate.translate(input, fromLanguage, toLanguage, function(err, translation){
if(err){
throw new Error(err);
}
resolve(translation.translatedText);
})
} catch(error){
reject(error);
}
})
}
login({
email: credentials.fbEmail,
password: credentials.fbPassword,
loginOptions:{
forceLogin: true
}
}, function callback(err, api){
if(err) return console.error(err);
api.setOptions({
listenEvents: true,
logLevel: 'error'
});
let friendsList = [];
api.getFriendsList(function(err, data) {
if(err) return console.error(err);
friendsList = data;
});
var stopListening = api.listen(async function(err, event) {
if(err) return console.error(err);
switch(event.type) {
case "message":
// if(event.body === '/stop') {
// api.sendMessage("Goodbye...", event.threadID);
// return stopListening();
// }
api.markAsRead(event.threadID, function(err) {
if(err) console.log(err);
});
console.log(`${friendsList.find((friend) => friend.userID == event.senderID).fullName} : ${moment().format('hh:mm:ss')}`)
console.log(`Original: ${event.body}`);
console.log(`Translation: ${await translate(credentials.fromLanguage, credentials.toLanguage, event.body)}`);
let response = await getInput();
api.sendMessage(await translate(credentials.toLanguage, credentials.fromLanguage, response), event.threadID);
break;
case "event":
console.log(event);
break;
}
});
});