-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
117 lines (105 loc) · 2.93 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var HTTPS = require('https');
var botID = process.env.BOT_ID;
var img1URL = process.env.IMG_1_URL;
var img2URL = process.env.IMG_2_URL;
function respond() {
var req = JSON.parse(this.req.chunks[0]);
var reHaig = /instahaig/;
var reWilly = /countdown/;
var reTechTrek = /boujee/
if (req.text) {
if (reHaig.test(req.text.toLowerCase())) {
console.log(reHaig+" TRUE");
this.res.writeHead(200);
// Message contents
var body = {
"bot_id" : botID,
"text" : "",
"attachments" : [
{
"type" : "image",
"url" : img1URL
}
]
};
postMessage(body);
this.res.end();
} else if (reWilly.test(req.text.toLowerCase())) {
console.log(reWilly+" TRUE");
this.res.writeHead(200);
var endDate = new Date("Jan 17 2017 09:00:00 GMT-0500 (EST)");
var botResponse = calculateTimeUntil(endDate);
// Message contents
var body = {
"bot_id" : botID,
"text" : botResponse
};
postMessage(body);
this.res.end();
} else if (reTechTrek.test(req.text.toLowerCase())) {
console.log(reTechTrek+" TRUE");
this.res.writeHead(200);
// Message contents
var body = {
"bot_id" : botID,
"text" : "",
"attachments" : [
{
"type" : "image",
"url" : img2URL
}
]
};
postMessage(body);
this.res.end();
} else {
console.log(reHaig + " " + reWilly + " " + reTechTrek + " FALSE");
this.res.writeHead(200);
this.res.end();
}
} else {
console.log(reHaig + " " + reWilly + " " + reTechTrek + " FALSE");
this.res.writeHead(200);
this.res.end();
}
}
function postMessage(body) {
var options, botReq;
// Application verification
options = {
hostname: 'api.groupme.com',
path: '/v3/bots/post',
method: 'POST'
};
console.log('sending image to ' + botID);
// Catch errors
botReq = HTTPS.request(options, function(res) {
if(res.statusCode != 202) {
console.log('rejecting bad status code ' + res.statusCode);
}
});
botReq.on('error', function(err) {
console.log('error: ' + JSON.stringify(err));
});
botReq.on('timeout', function(err) {
console.log('timeout: ' + JSON.stringify(err));
});
botReq.end(JSON.stringify(body));
}
function calculateTimeUntil(endDate) {
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var now = new Date();
var diff = endDate - now;
if (diff < 0) {
return "It's time yo.";
}
var days = Math.floor(diff / _day);
var hours = Math.floor((diff % _day) / _hour);
var minutes = Math.floor((diff % _hour) / _minute);
var seconds = Math.floor((diff % _minute) / _second);
return days + " days, " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds until Willy Reunion.";
}
exports.respond = respond;