-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (59 loc) · 1.44 KB
/
index.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
'use latest';
'use strict';
const _ = require('lodash');
const request = require('request');
function roll(data, req, res){
request({
baseUrl: 'https://slack.com/api/',
url: '/chat.postMessage',
method: 'POST',
json: true,
form: {
token: data.slack_bot_token,
as_user: true,
channel: data.channel_id,
text: `@${data.user_name} rolled ${_.random(0, 100)}`
}
}, function(err, response, body){
if(err || !body.ok){
console.log('fail', err, body);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Failed to send');
return;
}
res.end();
});
}
function ping(data, req, res){
res.end('Pong! :100:');
}
function help(data, req, res){
const msg = (`Currently available commands:
* ping - pongs back (private)
* roll - rolls a 100-sided die (public)
`);
res.end(msg);
}
function task({ data }, req, res){
if(data.token !== data.slack_command_token){
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Nope!');
return;
}
const command = (data.text.split(' ')[0] || '').toLowerCase();
switch(command){
case 'ping':
ping(data, req, res);
break;
case 'roll':
roll(data, req, res);
break;
case 'commands':
case 'help':
help(data, req, res);
break;
default:
res.end(`No command for '${command}' yet. Type '/icedbot help' for all commands.`);
}
}
module.exports = task;