forked from odevine/catfacts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
64 lines (50 loc) · 1.48 KB
/
Copy pathbot.js
File metadata and controls
64 lines (50 loc) · 1.48 KB
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
var HTTPS = require('https'),
botID = process.env.BOT_ID,
myArray = require('./catfactarray.js');
function respond() {
var request = JSON.parse(this.req.chunks[0]),
botRegex = /catfacts/i;
if(request.text && botRegex.test(request.text)) {
this.res.writeHead(200);
// wait at least 500ms before posting
setTimeout(function() {
postMessage();
console.log('posted!');
}, 500);
this.res.end();
}
else {
console.log("not a valid message to respond to");
this.res.writeHead(200);
this.res.end();
}
}
function postMessage() {
var rand, options, body, botReq;
rand = myArray[Math.floor(Math.random() * myArray.length)];
options = {
hostname: 'api.groupme.com',
path: '/v3/bots/post',
method: 'POST'
};
body = {
"bot_id": botID,
"text": rand
};
console.log('sending ' + rand + ' 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;