-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
107 lines (101 loc) · 4.78 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
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
//require modules such as the Facebook chat api
var login = require("facebook-chat-api");
var sys = require('sys')
var exec = require('child_process').exec;
var fs = require('fs');
//define a process that returns the JSON version of a string
function getStringLiteral(theFunction) {
return JSON.stringify(theFunction.toString());
}
//login to facebook
login({
email: "YOUREMAIL",
password: "YOURPASSWORD"
}, function callback(err, api) {
if (err) return console.error(err);
api.setOptions({
listenEvents: true
});
var stopListening = api.listen(function(err, event) {
if (err) return console.error(err);
switch (event.type) {
//all your message interactions go here
case "message":
//stop listening if they say "/stop"
if (event.body === '/stop') {
api.sendMessage("Goodbye...", event.threadID);
return stopListening();
}
//start music interactions
else if (event.body.toLowerCase().indexOf("music") > -1) {
//send the user what you just sent them
api.sendMessage(event.body, event.threadID);
//execute the order in the terminal
exec(event.body.replace('music', './spotify'), function(error, stdout, stderr) {
finalmsg = stdout.replace('\\u001b', '')
//send the user confirmation
api.sendMessage(finalmsg, event.threadID)
});
}
//password interactions
else if (event.body.toLowerCase().indexOf('pw') > -1) {
//get a saved password
if (event.body.toLowerCase().indexOf('get') > -1) {
fs.readFile('passwords.txt', 'utf8', function(err, data) {
if (err) throw err;
search = data.split(',');
found = false;
for (a = 0; a < search.length; a++) {
//make sure the account of the user matches the account of the person saved
if (search[a].indexOf(event.body.split(' ')[2]) > -1 && search[a].split('"')[0] == event.threadID) {
api.sendMessage(search[a].split('"')[2], event.threadID);
found = true;
}
}
if (!found) {
api.sendMessage('Sorry, you haven\'t stored a password for that account.', event.threadID)
}
});
}
//enter in a new password
else {
place = event.body.split(' ')[1];
pw = event.body.split(' ')[2];
fs.appendFile('passwords.txt', event.threadID + '"' + place + '"' + pw + ',', (err) => {
if (err) throw err;
api.sendMessage('Password saved!', event.threadID);
});
}
}
//send an iMessage
else if (event.body.toLowerCase().indexOf("msg") > -1) {
msg = event.body.replace('msg', '');
recipient = msg.split(" ")[0];
msg = msg.replace('recipient ', '');
exec('osascript sendMessage.applescript ' + recipient + ' "' + msg + '"', function(error, stdout, stderr) {
api.sendMessage("Message sent", event.threadID)
});
}
//show list of commands
else if (event.body.toLowerCase().indexOf('command') > -1) {
api.sendMessage('music play/pause\nmusic vol up/down\nmusic play list [playlist]\nmusic play [author/song]\nmusic next/prev\npw [place] [password]\npw get [place]\nmsg [recipient] [message]\n[any mac terminal command]', event.threadID)
}
//otherwise just execute the command lol
else {
exec(event.body, function(error, stdout, stderr) {
tosend = getStringLiteral(stdout)
console.log(tosend)
api.sendMessage(tosend + "", event.threadID)
});
}
//mark message as read
api.markAsRead(event.threadID, function(err) {
if (err) console.log(err);
});
break;
case "event":
console.log(event);
break;
}
});
});