-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
48 lines (40 loc) · 1.41 KB
/
main.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
var http = require('http');
var HTTPS = require('https');
var botID = "Put Bot ID here";
var promptRegEx = /\?prompt/i;
var commandRegEx = {
say: / -say /
};
server = http.createServer(function (req, res) {
req.on('data', function(data) {
parsedData = JSON.parse(data);
console.log("recieved data \n \n" + data + "\n\n" + parsedData.sender_type);
if (parsedData.text.search(promptRegEx) != -1 && parsedData.sender_type != "bot"){
if(parsedData.text.search(commandRegEx.say) != -1){
var location = parsedData.text.search(commandRegEx.say);
postMessage(parsedData.text.slice(location + 6));
}
else {
console.log("string contains ?prompt = true");
postMessage("Possible commands are: \n \n ?prompt : prints this text \n \n prompt -say TEXT : prints the text that follows -say \n \n More command coming soon!");
}
}
});
});
function postMessage(message){
var options = {
hostname: 'api.groupme.com',
path: '/v3/bots/post',
method: 'POST'
};
var body = {
"bot_id": botID,
"text" : message
}
console.log ("Sending "+ message + " to " + botID);
var botReq = HTTPS.request(options, function(res){
console.log("statusCode: " + res.statusCode);
});
botReq.end(JSON.stringify(body));
}
server.listen(5000);