-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbot.js
89 lines (72 loc) · 2.07 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
var HTTPS = require('https');
var HTTP = require('http');
var botID = process.env.BOT_ID;
var apiKey = process.env.API_KEY;
function respond() {
var request = JSON.parse(this.req.chunks[0]),
giphyCommand = '/giphy';
if(request.text &&
request.text.length > giphyCommand.length &&
request.text.substring(0, giphyCommand.length) === giphyCommand) {
this.res.writeHead(200);
searchGiphy(request.text.substring(giphyCommand.length + 1));
this.res.end();
} else {
this.res.writeHead(200);
this.res.end();
}
}
function searchGiphy(giphyToSearch) {
var options = {
host: 'api.giphy.com',
path: '/v1/gifs/search?q=' + encodeQuery(giphyToSearch) + '&api_key=' + apiKey
};
var callback = function(response) {
var str = '';
response.on('data', function(chunck){
str += chunck;
});
response.on('end', function() {
if (!(str && JSON.parse(str).data[0])) {
postMessage('Couldn\'t find a gif 💩');
} else {
var id = JSON.parse(str).data[0].id;
var giphyURL = 'http://i.giphy.com/' + id + '.gif';
postMessage(giphyURL);
}
});
};
HTTP.request(options, callback).end();
}
function encodeQuery(query) {
return query.replace(/\s/g, '+');;
}
function postMessage(message) {
var botResponse, options, body, botReq;
botResponse = message;
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('202 response');
} else {
console.log('rejecting bad status code from groupme:' + 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;