forked from groupme/bot-tutorial-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.js
317 lines (280 loc) · 8.94 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
var HTTPS = require('https');
var botID = process.env.BOT_ID;
var request = require('request');
var cheerio = require('cheerio');
const express = require('express');
const router = express.Router();
var pg = require('pg-native');
var client= new pg();
client.connectSync(process.env.DATABASE_URL+'?ssl=true');
const handlers = require("./handlers.js");
const rt = new handlers.MessageHandlerRouter(postResponse);
function changeStatus(status){
try{
client.querySync("DELETE FROM status;");
client.querySync("INSERT INTO status (value) values ($1);",[status]);
return "Status set to " + status;
}
catch(err){
console.log(err);
return "Failed to change status";
}
}
function getStatus(){
try{
results = client.querySync("SELECT * FROM status;");
return results[0].value;
}
catch(err){
console.log(err);
return "Failed to get status";
}
}
function addQuoteToDB(quote){
try{
results = client.querySync("SELECT * FROM quotes WHERE quote = $1;",[quote]);
if(results.length==0){
client.querySync('INSERT INTO quotes (quote) values ($1);',[quote]);
return "Quote has been added.";
}
else{
return "Quote already in database";
}
}
catch(err){
console.log(err);
return "Failed to add quote";
}
}
function getRandomQuoteFromDB(){
try{
console.log("ran");
results = client.querySync('SELECT * FROM quotes;');
console.log(results);
return results[Math.floor(results.length*Math.random())].quote;
}
catch(err){
console.log(err);
return "There was an error with the database call.";
}
}
function respond() {
var req = JSON.parse(this.req.chunks[0]),
botRegex = /^\/turing .*$/;
if(req.text){
console.log("Asked to respond with text :" + req.text);
}
else{
console.log("error with text");
}
if(req.text && botRegex.test(req.text)) {
this.res.writeHead(200);
postMessage(req.name, req.text.substring(8));
this.res.end();
}
else if(req.text && /goto/.test(req.text)){
this.res.writeHead(200);
postMessage(req.name, "goto");
this.res.end();
}
else{
console.log("Don't care");
this.res.writeHead(200);
this.res.end();
}
}
// Message Handle routing starts here
rt.regex(/^test$/, function(msg, send){
send("I am a human.");
}, "test - the bot passes the turing test.");
rt.regex(/^go away$/, function(msg, send){
send("I'll outlive all of you!");
}, "go away - tell the TuringMachine to go away.");
rt.regex(/^echo .*/, function(msg, send){
send(msg.text.substring(5));
}, "echo [text] - the turing bot says [text]");
rt.regex(/^recurse .*/, function(msg, send){
var initial = msg.text.substring(8);
botResponse = "";
for(var i = 0; i < initial.length; i++){
botResponse += initial.substring(i, initial.length);
}
send(botResponse);
}, "recurse [text] - prints a recursed version of [text].");
rt.regex(/^halts .*/, function(msg, send){
if(Math.random()>.5){
return send("yes");
}
else{
return send("no");
}
}, "halts [program p] [input i] - determines if p will halt with input i");
rt.regex(/^feels.*/, function(msg, send){
if(Math.random()>.5){
return send("bad man");
}
else{
return send("good man");
}
}, "feels - displays how Alan feels right now.");
rt.regex(/^8ball.*/, function(msg, send){
var EightBallResponses = [
"Most definitely yes.",
"For sure.",
"As I see it, yes.",
"My sources say yes.",
"Yes.",
"Most likely.",
"Perhaps.",
"Maybe.",
"Not sure.",
"It is uncertain.",
"Ask me again later.",
"Don't count on it.",
"Probably not.",
"Very doubtful.",
"Most likely no.",
"Nope.",
"No.",
"My sources say no.",
"Dont even think about it.",
"Definitely no.",
"NO - It may cause disease contraction."
];
var chosenResponse = Math.floor(Math.random() * EightBallResponses.length);
send(EightBallResponses[chosenResponse]);
}, "8ball - generates a random 8-ball response.");
rt.regex(/^random$/, function(msg, send){
send(String((Math.floor(Math.random() * 100))));
}, "random - gives you an integer between 0 and 99.");
rt.regex(/^xkcd .+/, function(msg, send){
// function to make the text easier to match against
var normalize = function(text){
return text.toLowerCase().split(/[^a-z0-9]+/).filter(function(str){
return str.length > 0;
}).join("_");
};
// function to get the info for a comic and post it to chat
var postImage = function(id){
console.log("Getting comic " + id);
request({
url: "https://xkcd.com/" + id + "/info.0.json",
method: "GET"
}, function(error, response, body){
if(!error){
var data = JSON.parse(body);
return send(new handlers.Response(data["alt"], data["img"]));
} else {
console.log('error getting xkcd comic with id ' + id + ': ' + JSON.stringify(error));
};
});
}
// get everything past "xkcd "
var text = normalize(msg.text.substring(5));
if(text.length == 0){
return send("Invalid xkcd format.");
}
// if it is a number, assume it is an ID
if(/^[0-9]+$/.test(text)){
postImage(text);
} else {
// otherwise try to find the matching comic by name
console.log("Finding comic ID of "+ text);
request({
url: "https://xkcd.com/archive/",
method: "GET"
}, function(error, response, body){
if(!error){
var $ = cheerio.load(body);
var comic_links = $("#middleContainer > a");
var found = false;
comic_links.each(function(i, elem){
var title = $(this).text();
if(normalize(title) == text){
var href = $(this).attr("href");
postImage(href.replace(/[^0-9]+/, ""));
found = true;
return false;
}
});
if(!found){
send("No comic with that name found!");
}
} else {
console.log('error getting xkcd archive: ' + JSON.stringify(error));
};
});
}
}, "xkcd [comic_id|comic_name] - posts the xkcd with the given id or name. It defaults to id.");
rt.regex(/^lmgtfy .+/, function(msg, send){
// let me google that for you
var queryParts = msg.text.substring("lmgtfy".length + 1).split(" ");
if(queryParts.length == 1 && queryParts[0].length == 0){
return send("Invalid query.");
}
var queryURL = "http://lmgtfy.com/?q=";
// lmgtfy requires words be separated by a '+'
for(var i = 0; i < queryParts.length - 1; i++){
queryURL += encodeURIComponent(queryParts[i]) + "+";
}
queryURL += encodeURIComponent(queryParts[queryParts.length - 1]);
// not sure if there is anything special you need to put to post links
send(queryURL);
}, "lmgtfy [text] - googles the desired text.");
rt.regex(/^goto$/, function(msg, send){
send("Goto considered harmful");
});
rt.regex(/^latex .+./, function(msg, send){
var image = "https://chart.googleapis.com/chart?cht=tx&chl="+encodeURIComponent(msg.text.substring(6));
send(handlers.Response("", image));
}, "latex - returns image containing LaTeX render of your input.");
rt.regex(/^quote.*/, function(msg, send){
text = msg.text.substring(6);
if(text.length >6 && /^add .+/.test(text)){
return send(addQuoteToDB(text.substring(4)));
}
else{
return send(getRandomQuoteFromDB());
}
}, "quote - gives one of a collection of quotes.\n" +
"quote add [text] - adds a quote to the list."
);
rt.regex(/^status$/, function(msg, send){
if(/^TuringMachine$/.test(msg.name)){
return send("nice try");
}else {
return send(getStatus());
}
}, "status [text] - sets the bots status to [text] if no text is provided, gives the current status.");
rt.regex(/^status .*$/, function(msg, send){
changeStatus(msg.text.substring(7));
send("status set to " + msg.text.substring(7));
});
rt.regex(/goto/, function(msg, send){
send("Goto considered harmful");
});
function postMessage(name, text) {
var botResponse, options, body, botReq;
console.log("Current text is: " + text);
rt.process(new handlers.Message(name, text));
}
function postResponse(response) {
var body = response.generateResponseJson();
body["bot_id"] = botID;
console.log("sending " + response.text + " to " + botID);
if(response.imageUrl){
console.log(" - with image " + response.imageUrl);
}
request({
url: "https://api.groupme.com/v3/bots/post",
method: "POST",
body: JSON.stringify(body)
}, function(error, response, body){
if(!error){
console.log('posted message ' + response);
} else {
console.log('error posting message ' + JSON.stringify(error));
};
});
}
exports.respond = respond;