-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·253 lines (194 loc) · 8.28 KB
/
server.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
const express = require("express");
const https = require("https");
const qs = require("querystring");
const app = express();
const port = 8080;
const botToken = require("./secrets")["bot-token"];
const events = require("./events");
const Messaging = require("./messaging");
const Game = require("./game");
app.use(express.json());
app.use(express.urlencoded());
app.post("/events", function(req, res) {
console.log("received event:", req.body);
//Check if this is a verification message, ie if it has a challenge
if(req.body.challenge) {
res.send(req.body.challenge);
} else {
//Just regular event, dispatch
res.send(null);
events.executeCallbacks(req.body.event);
}
});
//Function gets called when we get a start game command from slack
app.post("/cmd/startgame", function (req,res) {
console.log("startgame command");
res.send("startgame");
console.log(req.body);
/*
IDEAS
- use other slack API to get list of all users currently in the channel
- pass this list to game management code
- game management code will assign roles and call back out to code that uses slack API to DM them
- also need to message entire channel that the game has started, rn it will only send replies to the user that did the command
- need to think about timing and how that works (? - do we even want a timing feature?)
*/
let channelID = req.body["channel_id"];
//Set this channel ID as the default channel ID that the game is running in
Messaging.setDefaultChannelID(channelID);
//Step 1: Grab list of every user in the chat so we can check if they are active
const encodedData = qs.stringify({
token: botToken,
channel: channelID,
});
const options = {
hostname: "slack.com",
path: "/api/conversations.members?"+encodedData
};
//console.log("about to send request to", options.path);
https.get(options, function(res) {
let fullData = "";
res.on("data", function(data) {
fullData += data;
});
res.on("error", function(err) {
console.log("Error with request to list people in the channel:", err);
});
res.on("end", function() {
//We have all the data, now do stuff with it
console.log(botToken);
console.log(fullData);
let usersData = JSON.parse(fullData);
identifyActiveUsers(usersData.members, function(activeUsers) {
console.log("the following users are active:", activeUsers);
//Assign roles for the game from the list of active users
Game.assignRoles(activeUsers);
});
});
});
});
//Takes the list of all users we got from slack and calls the callback with a list of only the active ones.
function identifyActiveUsers(allUsers, cb) {
let userPromises = [];
for(let i=0; i<allUsers.length; i++) {
//Setup a promise for fetching the data
userPromises.push(new Promise(function (resolve, reject) {
const requestData = qs.stringify({
token: botToken,
user: allUsers[i]
});
const options = {
hostname: "slack.com",
path: "/api/users.getPresence?"+requestData
};
https.get(options, function(res) {
let fullData = "";
res.on("data", function(data) {
fullData += data;
});
res.on("end", function() {
resolve({user: allUsers[i], data: JSON.parse(fullData)});
});
res.on("error", function(err) {
reject(err);
});
});
}));
}
let activeUsers = [];
Promise.all(userPromises).then(function(result) {
//console.log("result from promise:", result);
for(let i=0; i<result.length; i++) {
if(result[i].data["presence"] === "active") {
activeUsers.push(result[i].user);
}
}
}).then(function() {
cb(activeUsers);
});
}
//Function gets called when we get a getrules comamnd from slack
app.post("/cmd/getrules", function (req,res) {
console.log("getrules command");
res.send("setrules");
});
//Function gets called when we get an accuse command from slack
app.post("/cmd/accuse", function (req, res) {
console.log("accuse command");
console.log(req.body);
res.send(null);
//Need to extract username from body and resolve to actual user ID
//remove the @ to get the username, then we can resolve
let accusedName = req.body.text.slice(1,req.body.text.length);
console.log("ACCUSED NAME IS:", accusedName);
//Setup request to users.list endpoint so we can return all the users and associate the name to an ID
const reqData = qs.stringify({
token: botToken,
limit: 100 //we probably won't ever have that many people playing anyway...
});
const options = {
hostname: "slack.com",
path: "/api/users.list?"+reqData
};
//console.log("making get request for username resolution");
https.get(options, function(res) {
let fullData = "";
res.on("data", function(data) {
fullData += data;
});
res.on("end", function() {
//We have all the data, go through it and find the relevant thing
let parsedData = JSON.parse(fullData);
//console.log("PARSED DATA:"+JSON.stringify(parsedData, null, 4));
for(let i=0; i<parsedData.members.length; i++) {
//console.log("checking", parsedData.members[i]);
if(parsedData.members[i].name === accusedName) {
//todo to propagate errors back to slack, registerAccusation needs to have a callback with an error
//if error exists, we make a request to the responseUrl from the req.body to send them back an error
Game.registerAccusation(parsedData.members[i].id, req.body.user_id, function(err) {
//If we get called at all, we did get called with an error, so we need to propagate that back to slack
//Construct request to slack api response url
let reqBody = JSON.stringify({
response_type: "ephemeral",
text: err
});
//https://hooks.slack.com/commands/TDR5818TG/466397563253/t3ypCQb1naw4XiyQX9RVEp9R'
let properPath = req.body.response_url.split(".com")[1];
const options = {
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(reqBody)
},
hostname: "hooks.slack.com",
path: properPath,
method: "POST"
};
console.log("ABOUT TO MAKE REQUEST TO:",options.hostname + options.path);
let postReq = https.request(options, function(res) {
res.on("error", function(err) {
console.log("RESPONSE URL ON ERROR!", err);
});
res.on("data", function(data) {
console.log("RESPONSE URL ON DATA!");
});
res.on("end", function() {
console.log("RESPONSE URL ON END!!");
})
}); //We don't need a callback, don't care if it goes through or not.
postReq.write(reqBody);
postReq.end();
});
break;
}
}
});
res.on("error", function(err) {
console.log("Error with request to users.list to resolve username to ID:", err);
});
});
});
app.post("/cmd/endgame", function(req, res) {
console.log("end game command");
res.send("end game");
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));