-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (77 loc) · 2.7 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//Setup
require('dotenv').config();
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var port = process.env.PORT || 5000;
var io = require('socket.io')(app.listen(port));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//Logic
var callList = {};
var handleSlackEndpoint = function (req, res) {
var token = req.body.token;
var query = req.body.text.split(' ');
var command = query[0] || 'help';
//Check the slack token in the call
if(token !== process.env.SLACK_TOKEN){
res.json({
error: 'Token Mismatch'
});
return;
}
//Standard response format
var response = {
text: "Lorem Ipsum",
response_type: "ephemeral",
};
//Command List
switch(command){
case 'on':
callList[req.body.user_id] = req.body.user_name;
response.text = req.body.user_name + " is going on call! :phone: :zipper_mouth_face:";
response.response_type = "in_channel";
break;
case 'off':
if(typeof callList[req.body.user_id] == "undefined"){
response.text = "You weren't on a call...";
} else {
delete callList[req.body.user_id];
response.text = req.body.user_name + " is done with their call! :raised_hands:";
response.response_type = "in_channel";
}
break;
case 'who':
var people = "";
for(var index in callList) {
people += callList[index] + ", ";
}
if(Object.keys(callList).length > 0)
response.text = "The following people are on call: " + people;
else
response.text = "No one is currently on call!";
break;
case 'status':
if(Object.keys(callList).length > 0)
response.text = "Call in progress! Keep quiet~";
else
response.text = "No one is currently on call!";
break;
case 'reset':
callList = {};
response.text = "Call list has been reset!";
break;
case 'help':
default:
response.text = "Use `on`/`off` to toggle current call status. `who` will return a list of people on call. `status` will give you a quick status update!";
break;
}
io.sockets.emit('status', Object.keys(callList).length);
res.json(response);
};
io.on('connection', function(socket){
socket.emit('status', Object.keys(callList).length);
});
//Routes
app.use(express.static(__dirname + '/public'));
app.post('/endpoint', handleSlackEndpoint);