-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
314 lines (277 loc) · 9.05 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
'use strict';
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const SlackClient = require('@slack/client');
const aaa = require("adjective-adjective-animal");
const webClient = new SlackClient.WebClient(process.env.OAUTH_ACCESS_TOKEN);
console.log("OAUTH TOKEN: ", process.env.OAUTH_ACCESS_TOKEN);
const bot = new SlackClient.WebClient(process.env.BOT_ACCESS_TOKEN);
console.log("BOT TOKEN: ", process.env.BOT_ACCESS_TOKEN);
const membersChannel = process.env.ANNOUNCEMENT_CHANNEL;
console.log("Members Channel: ", membersChannel);
const members = process.env.MEMBERS.split(' ');
console.log("Members: ", members);
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
let convos = [];
let requests = [];
app.post('/', (req, res) => {
if (req.body.command == '/anon-chat') {
res.json({
text: 'A new anonymous chat will begin with you shortly...'
});
let user = {
id: req.body.user_id
};
let topic = req.body.text;
startAnonConvo(user, topic);
}
else if (req.body.command == '/new-chats') {
if (!members.includes(req.body.user_name)) {
return res.json({
text: "Sorry, this command is only for committee members"
});
}
else {
let listOfRequests = '';
for (let openReq of requests) {
if (openReq.member.id == undefined) {
listOfRequests += '> '+openReq.user.codename + ' - ' + openReq.topic + '\n\n';
}
}
console.log("Requests: ", listOfRequests);
return res.json({
text: listOfRequests + '\n\n type `/open-chat {codename}` to take one of these available convos.'
});
}
}
else if (req.body.command == '/open-chat') {
if (!members.includes(req.body.user_name)) {
return res.json({
text: "Sorry, this command is only for committee members"
});
}
else if (req.body.text == '') {
return res.json({
text: "Please specify what convo you want to open"
});
}
else {
let foundReq = undefined;
for (let reqIndex in requests) {
let convo = requests[reqIndex];
if (convo.user.codename == req.body.text) {
requests.splice(reqIndex, 1);
convo.member.id = req.body.user_id;
convo.member.username = req.body.user_name;
convos.push(convo);
foundReq = convo;
break;
}
}
res.json({
text: foundReq.user.codename + " is all yours!"
});
webClient.groups.create(foundReq.member.channel.name)
.then(function(group){
console.log("Group: ", group);
foundReq.member.channel.id = group.group.id;
console.log("Member Channel: ", foundReq.member.channel.id);
return webClient.groups.invite(foundReq.member.channel.id, appData.selfId);
})
.then(function(invite){
console.log("Invite: ", invite);
return bot.chat.postMessage(foundReq.member.channel.id, "Hey, type here and we're relay your messages.");
});
}
}
else if (req.body.command == '/reveal-chat') {
console.log("Command: ", req.body);
for (let convo of convos) {
if (convo.user.channel.id == req.body.channel_id) {
convo.user.reveal = true;
bot.chat.postMessage(convo.member.channel.id, "*_"+convo.user.codename+" has revealed themselves as "+convo.user.username+"_*");
return res.json({
text: "*_You have revealed your username to "+convo.member.username+"_*"
});
}
}
res.json({
text: "Hmmm, looks like you're not in an anonymous channel. Try switching to an anonymous channels and running `/reveal-chat` again to reveal your username"
});
}
else if (req.body.command == '/close-chat') {
res.json({
text: "Closing anonymous chat..."
});
for (let convoIndex in convos) {
let convo = convos[convoIndex];
if (convo.user.channel.id == req.body.channel_id || convo.member.channel.id == req.body.channel_id) {
convos.splice(convoIndex, 1);
bot.chat.postMessage(convo.member.channel.id, "*_Anonymous chat closed._*");
bot.chat.postMessage(convo.user.channel.id, "*_Anonymous chat closed._*");
}
}
}
else {
res.json({
text: "Sorry, I dont know that command :("
});
}
});
const server = app.listen(3000, () => {
console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);
});
// Cache of data
const appData = {};
// Initialize the RTM client with the recommended settings. Using the defaults for these
// settings is deprecated.
const rtm = new SlackClient.RtmClient(process.env.BOT_ACCESS_TOKEN, {
dataStore: false,
useRtmConnect: true,
});
// The client will emit an RTM.AUTHENTICATED event on when the connection data is available
// (before the connection is open)
rtm.on(SlackClient.CLIENT_EVENTS.RTM.AUTHENTICATED, (connectData) => {
// Cache the data necessary for this app in memory
appData.selfId = connectData.self.id;
console.log(`Logged in as ${appData.selfId} of team ${connectData.team.id}`);
});
// The client will emit an RTM.RTM_CONNECTION_OPENED the connection is ready for
// sending and receiving messages
rtm.on(SlackClient.CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, () => {
console.log(`Ready`);
});
rtm.on(SlackClient.RTM_EVENTS.MESSAGE, (message) => {
// For structure of `message`, see https://api.slack.com/events/message
// Skip messages that are from a bot or my own user ID or some other thing we dont care about
if ( (message.subtype && message.subtype === 'bot_message') ||
(!message.subtype && message.user === appData.selfId) ||
(message.subtype && message.subtype === 'group_join') ||
(message.subtype && message.subtype === 'group_archive') ||
(message.subtype && message.subtype === 'channel_join') ) {
return;
}
let fromChannel = message.channel;
let toChannel = '';
for (let convo of convos) {
if (convo.user.channel.id == fromChannel) {
toChannel = convo.member.channel.id;
message.text = '*'+(convo.user.reveal ? convo.user.username : convo.user.codename)+':* ' + message.text;
break;
}
else if (convo.member.channel.id == fromChannel) {
toChannel = convo.user.channel.id;
message.text = '*'+convo.member.username+':* ' + message.text;
break;
}
}
if (toChannel) {
bot.chat.postMessage(toChannel, message.text);
}
else if (message.text && message.text.toLowerCase().includes('help')) {
bot.chat.postMessage(fromChannel, 'Hey! It looks like you\'re asking for help. A new anonymous chat will begin with you shortly...');
startAnonConvo({id: message.user});
}
else {
bot.chat.postMessage(fromChannel, ':( Sorry, I don\'t quite understand what you\'re saying. You can type "help" if you\'d like to speak with someone anonymously');
}
});
console.log("Starting RTM...");
// Start the connecting process
rtm.start();
function startAnonConvo(user, topic) {
let convo = {};
newConvoConfig(user, topic)
.then(function(config){
console.log("Config: ", config);
convo = config;
return webClient.groups.create(convo.user.channel.name);
})
.then(function(group){
console.log("Group: ", group);
convo.user.channel.id = group.group.id;
console.log("User Channel: ", convo.user.channel.id);
return webClient.groups.invite(convo.user.channel.id, appData.selfId);
})
.then(function(invite){
console.log("Invite: ", invite);
return bot.chat.postMessage(convo.user.channel.id, "Just hold tight, we've notified the committee you'd like to chat anonymously and will let you know when someone responds!");
})
.then(function(msg){
console.log("Message: ", msg);
return aaa(1);
})
.then(function(codename){
convo.user.codename = codename;
requests.push(convo);
console.log("Requests Object: ", requests);
notifyCommittee('Hey Committee! `'+codename+'` would like to chat! Type `/new-chats` to find out more');
});
}
function notifyCommittee(message) {
console.log("Fetching channels...");
bot.channels.list()
.then(function(channels){
console.log("Channels: ", channels);
for (let channel of channels.channels) {
if (channel.name == membersChannel) {
return bot.chat.postMessage(channel.id, message);
}
}
});
}
function newConvoConfig(user, topic) {
return new Promise(function(resolve, reject){
let time = new Date().getTime();
let userChannel = 'Anonify-' + time;
let memberChannel = 'Anon-' + time;
let member = getRandom(members);
let config = {};
config.topic = topic || "No topic";
config.member = {
channel: {
name: memberChannel
}
};
getUser(user)
.then(function(userInfo){
console.log("User Info: ", userInfo);
config.user = {
id: userInfo.id,
username: userInfo.name,
reveal: false,
channel: {
name: userChannel
}
};
return resolve(config);
});
});
}
function getUser(q) {
return new Promise(function(resolve, reject){
bot.users.list()
.then(function(users){
for (let user of users.members) {
// TODO: dynamically match properties of q to properties of user
if (q.username != undefined) {
if (user.name == q.username) {
return resolve(user);
}
}
else if (q.id != undefined) {
if (user.id == q.id) {
return resolve(user);
}
}
}
return reject('User not found');
});
});
}
function getRandom(list) {
return list[Math.floor(Math.random()*list.length)];
}