-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
executable file
·99 lines (82 loc) · 2.21 KB
/
bot.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
var HTTPS = require('https');
var cool = require('cool-ascii-faces');
var botID = process.env.BOT_ID;
//var diversity = [/shri/i, /lenkala/i, /keyshawn/i, /keybanks/i, /kebanks/i, /ebanks/i, /@Shriyans Lenkala/, /@Keyshawn Ebanks/], kyle = /kyle/i, davis = /davis/i;
var diversity = [/dlfkjdkfljlkdjf/, /jhgfjhgfhf/];
var hasRun = false;
var name = "", resp;
//take response, find keyword
function respond() {
var request = JSON.parse(this.req.chunks[0]);
name = request.name.toString();
resp = request.text;
hasRun = true;
//checks if response has keyword
if(request.text && testResponse(request.text, diversity) && hasRun) {
hasRun = false;
this.res.writeHead(200);
postMessage();
this.res.end();
} else {
console.log("don't care");
this.res.writeHead(200);
this.res.end();
}
}
//checks if response has keyword
function testResponse (res, keyword) {
// for (var i=0; i<keyword.length; i++) {
// if (keyword[i].test(res)) {
// return true;
// }
// }
// if (kyle.test(res) || davis.test(res)) {
// return true;
// }
// if (name==="Bebe Ballo" || name==="Shriyans Lenkala" || name==="Keyshawn Ebanks") {
// return true;
// }
return false;
}
//determines response
function saying() {
if (name==="Bebe Ballo") {
return "stfu Ballo";
}
else if (kyle.test(resp) || davis.test(resp)) {
return cool();
}
else {
return "#diversityfrat";
}
}
//posts
function postMessage() {
var botResponse, options, body, botReq;
botResponse = saying();
options = {
hostname: 'api.groupme.com',
path: '/v3/bots/post',
method: 'POST'
};
body = {
"bot_id" : botID,
"text" : botResponse
};
console.log('sending ' + botResponse + ' to ' + botID);
botReq = HTTPS.request(options, function(res) {
if(res.statusCode == 202) {
//neat
} else {
console.log('rejecting bad status code ' + res.statusCode);
}
});
botReq.on('error', function(err) {
console.log('error posting message ' + JSON.stringify(err));
});
botReq.on('timeout', function(err) {
console.log('timeout posting message ' + JSON.stringify(err));
});
botReq.end(JSON.stringify(body));
}
exports.respond = respond;