-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
342 lines (296 loc) · 9.19 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
var request = require('request');
var HTTPS = require('https');
var weather = require('weather-js');
var quote = require('forismatic-node')();
var cron = require('cron').CronJob;
var moment = require('moment');
const catfact = require('cat-facts');
const fs = require('fs');
shitposts = require('./shitposts.js')
eightball = require('./eightball.js')
//groupme ID to PM
const adminID = "32906498";
const botID = "cd3299b98f2143984d6f16e649";
const sourceLink = "https://github.com/ReticulatedSpline/Groupme-Chatbot";
var e = "Trigger detected: ";
function parseResponse() {
var request = JSON.parse(this.req.chunks[0]),
//callback trigger probably shouldn't be in the response...
botRegex = /\@Bo/;
if (request.text &&
botRegex.test(request.text) &&
request.sender_type != "bot") {
switch (true) {
case (/source/i.test(request.text)):
console.log(e + "sourcecode");
postMessage(sourceLink);
break;
case (/about|help|(who are)|(who dis)/i.test(request.text)):
postMessage(buildAbout());
break;
case (/flip.*coin/i.test(request.text)):
postMessage(buildCoinFlip());
break;
case (/.*number.*between|from/i.test(request.text)):
postMessage(buildNumPick(request.text));
break;
case (/weather|forecast/i.test(request.text)):
buildWeather(request.text);
break;
case (/quote|inspir/i.test(request.text)):
buildQuote();
break;
case (/remind\sme/i.test(request.text)):
buildReminder(request.text, request.name);
break;
case (/reddit|\/r\//i.test(request.text)):
buildReddit(request.text);
break;
case (/cat fact|catfact/i.test(request.text)):
postMessage(catfact.random());
break;
case (/request/i.test(request.text)):
buildRequest(request.text, request.name);
break;
case (/odds|probability|chances|magic\s8/i.test(request.text)):
buildEightBall();
break;
default:
postMessage("My responses are limited. You can see a list of valid" +
" queries with `@Bo help`.");
}
this.res.end();
}
}
function buildAbout() {
console.log(e + "about");
return fs.readFileSync(__dirname + '/about.md', 'utf-8');
}
function buildCoinFlip() {
console.log(e + "coinflip");
var result = Math.floor(Math.random() * 2);
return result > 0 ? "The coin was heads!" :
"The coin was tails.";
}
function buildNumPick(req) {
console.log(e + "randnum");
var nums = req.match(/\d+/g);
if (nums.length != 2) {
return "You'll have to provide exactly two numbers.";
}
var max = Math.max.apply(null, nums);
var min = Math.min.apply(null, nums);
return "I choose " + (Math.floor(Math.random() * max) + min) + "!";
}
function buildWeather(req) {
console.log(e + "weather");
//@Bo weather in duluth, mn
req = req.toLowerCase();
var city = req.match(/[a-z]+(?=\,)/);
var state = req.match(/(?:,\s)(.*)/);
if (!city || !state) {
console.log("no|invalid arguments. defaulting...")
city = "duluth";
state = "mn";
} else {
city = city[0];
state = state[1];
}
console.log("Querying " + city + ", " + state + "...");
var query = 'select * from weather.forecast where ' +
'woeid in (select woeid from geo.places(1) ' +
'where text="' + city + ', ' + state + '")';
weather.find({
search: city + ', ' + state,
degreeType: 'F'
},
function(err, res) {
if (err) {
console.log(err);
return "Sorry, the request could not be completed";
} else {
if (req.indexOf('forecast') > -1) {
postMessage("Conditions: " + res[0].current.skytext.toLowerCase() + " and " +
res[0].current.temperature + " degrees." +
" Tomorrow will be " +
res[0].forecast[2].skytextday.toLowerCase() +
" with a high of " +
res[0].forecast[2].high + " degrees. " +
res[0].forecast[3].day + " will be " +
res[0].forecast[3].skytextday.toLowerCase() + " and " +
res[0].forecast[3].high + " degrees.");
} else {
postMessage("Conditions: " + res[0].current.skytext + " and " +
res[0].current.temperature + " degrees.");
}
}
});
}
function buildQuote() {
quote.getQuote(function(error, quote) {
if (!error) {
postMessage(quote.quoteText + " -" + quote.quoteAuthor);
} else {
postMessage("Sorry, but I couldn't fetch a quote.")
console.error(error);
}
});
}
function buildReminder(req, name) {
console.log(e + "reminder")
req = req.toLowerCase().trim();
req = req.replace("@bo", "");
var quant = req.match(/\d(?=\s(minutes?|hours?|days?))/)[0];
var unit = req.match(/minute|hour|day/)[0];
if (quant && unit) {
var res = /(?:me\s)(?:to)?(?:that)?(.*)(?=\sin)/g.exec(req)[1];
res = name + "\'s reminder: " + res;
console.log("setting cron job for " + quant + " " + unit + "(s) from now.");
console.log("statement is \'" + res + "\'.");
var date = moment();
date.add(quant, unit);
console.log(date);
postMessage("Okay, I'll remind you on " + date.format("MMM Do, YYYY") +
" at " + date.format("h:mma"));
var reminder = new cron(date.toDate(), function() {
postMessage(res);
reminder.stop();
}, function() {
console.log("ending cron job")
}, true, 'America/Chicago');
} else {
postMessage("Sorry, I couldn't understand that format.")
}
}
function buildReddit(req) {
req = req.toLowerCase();
var url = "http://www.reddit.com";
var sub = "";
if (/\/r\//.test(req)) {
sub += req.match(/(?:\/r\/)(\w+)/)[1];
} else {
sub += "all"
}
url += "/r/" + sub + ".json"
console.log(e + "reddit " + sub);
console.log(url);
request(url, function(error, response, body) {
if (error) {
console.log(error);
postMessage("Sorry, something went wrong.");
} else {
var page = JSON.parse(body);
var i = 0;
while (page.data.children[i].data.stickied) {
i = i + 1 ;
console.log("stickies: ", i);
}
var post = page.data.children[i].data;
postMessage("In r/" + sub + ", user \'" + post.author + "\' posted " +
(post.is_self ? "text: " : "a link: \'") + post.title + '\'\n' +
post.permalink);
}
});
}
function buildEightBall() {
console.log(e + "Magic 8");
postMessage(eightball[Math.floor(Math.random() * 18)]);
}
function buildRequest(text, fromUser) {
console.log(e + "feature request");
var req = /(?:request )(.*)/.exec(text)[1];
postMessage("Okay " + fromUser + ", I'll pass \'" + req + "\' on to Ben.");
req = fromUser + " requests " + req;
directMessage(adminID, req);
}
function postMessage(botResponse) {
var botResponse, options, body, botReq;
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) {
console.log('Response code: ' + res.statusCode);
} else {
console.log('Response 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));
}
/** W I P
function directMessage(userID, text) {
var botResponse, options, body, botReq;
options = {
hostname: 'api.groupme.com',
path: '/v3/direct_messages',
method: 'POST',
X-Access-Token: "vVdZWaS7b1N0p4oJ1EowZozkONo0hlZ7ZsAnTuXO"
};
body = {
"direct_message": {
"source_guid": "GUID",
"recipient_id": adminID,
"text": text
}
}
console.log('Sending: \'' + text + '\' to ' + userID + "...");
botReq = HTTPS.request(options, function(res) {
if (res.statusCode == 202) {
console.log('DM Response code: ' + res.statusCode);
} else {
console.log('DM Response code: ' + res.statusCode);
}
});
botReq.on('error', function(err) {
console.log('Error posting DM ' + JSON.stringify(err));
});
botReq.on('timeout', function(err) {
console.log('Timeout posting DM ' + JSON.stringify(err));
});
botReq.end(JSON.stringify(body));
}
**/
function spontanious() {
console.log(e + "spontanious");
var time = moment();
if (time.day() == 5 || time.day() == 6) {
//TODO: weekend prompt
} else {
//TODO: weekday prompt
}
//random interval from 50 - 250 hours (~2-10 days)
var interval = Math.floor(Math.random() * 200) + 50;
console.log("Scheduling next spontanious post in " + interval + " hours.\n");
time.add(interval, 'hours');
switch(Math.floor(Math.random() * 3)) {
case 1:
postMessage(shitposts[Math.floor(Math.random() * 18)]);
break;
case 2:
postMessage(catfact.random());
break;
case 3:
buildReddit("/r/all");
break;
}
}
var job = new cron(time.toDate(), function() {
}, function() {
spontanious();
}, true, 'America/Chicago');
}
exports.respond = parseResponse;
exports.spontanious = spontanious;