-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
489 lines (406 loc) · 18.1 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
* A Bot for Slack!
*/
var fs = require('fs');
require('dotenv').load();
const WebSocket = require('ws');
const http = require('http');
var botData = {
companyList: [],
challenger: '',
challengee: '',
countdownTimer: {
year: '',
month: '',
day: '',
hour: '',
minute: ''
}
};
var yearPicker = [2018, 2019, 2020];
var monthPicker = ["January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
var dayPicker = [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];
var hourPicker = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var meridianPicker = ['am', 'pm'];
var minutePicker = [0, 15, 30, 45];
function saveData() {
fs.writeFile("./data.json", JSON.stringify(botData), function (err) {
if (err) {
return console.log(err);
}
console.log("Successfully saved the file");
});
}
function loadData() {
fs.readFile("./data.json", function read(err, data) {
if (err) {
throw err;
}
console.log('DATA: ' + data);
if (data.length > 0) {
botData = JSON.parse(data);
}
});
}
/**
* Define a function for initiating a conversation on installation
* With custom integrations, we don't have a way to find out who installed us, so we can't message them :(
*/
function onInstallation(bot, installer) {
if (installer) {
bot.startPrivateConversation({
user: installer
}, function (err, convo) {
if (err) {
console.log(err);
} else {
convo.say('I am a bot that has just joined your team');
convo.say('You must now /invite me to a channel so that I can be of use!');
}
});
}
}
function displayArray(arr) {
i = 0;
returnvalue = '';
arr.forEach(function (item) {
returnvalue += i + ': ' + item + '\n';
i++;
})
return returnvalue;
}
/**
* Configure the persistence options
*/
var config = {};
if (process.env.MONGOLAB_URI) {
var BotkitStorage = require('botkit-storage-mongo');
config = {
storage: BotkitStorage({
mongoUri: process.env.MONGOLAB_URI
}),
};
} else {
config = {
json_file_store: ((process.env.TOKEN) ? './db_slack_bot_ci/' : './db_slack_bot_a/'), //use a different name if an app or CI
};
}
/**
* Are being run as an app or a custom integration? The initialization will differ, depending
*/
if (process.env.TOKEN || process.env.SLACK_TOKEN) {
//Treat this as a custom integration
var customIntegration = require('./lib/custom_integrations');
var token = (process.env.TOKEN) ? process.env.TOKEN : process.env.SLACK_TOKEN;
var controller = customIntegration.configure(token, config, onInstallation);
} else if (process.env.CLIENT_ID && process.env.CLIENT_SECRET && process.env.PORT) {
//Treat this as an app - production uses this configuration
var app = require('./lib/apps');
var controller = app.configure(process.env.PORT, process.env.CLIENT_ID, process.env.CLIENT_SECRET, config, onInstallation);
} else {
console.log('Error: If this is a custom integration, please specify TOKEN in the environment. If this is an app, please specify CLIENTID, CLIENTSECRET, and PORT in the environment');
process.exit(1);
}
/**
* A demonstration for how to handle websocket events. In this case, just log when we have and have not
* been disconnected from the websocket. In the future, it would be super awesome to be able to specify
* a reconnect policy, and do reconnections automatically. In the meantime, we aren't going to attempt reconnects,
* WHICH IS A B0RKED WAY TO HANDLE BEING DISCONNECTED. So we need to fix this.
*
* TODO: fixed b0rked reconnect behavior
*/
// Handle events related to the websocket connection to Slack
controller.on('rtm_open', function (bot) {
console.log('** The RTM api just connected!');
});
controller.on('rtm_close', function (bot) {
console.log('** The RTM api just closed');
// you may want to attempt to re-open
});
/**
* Core bot logic goes here!
*/
// BEGIN EDITING HERE!
// read data from json file if it exists
loadData();
controller.on('bot_channel_join', function (bot, message) {
bot.reply(message, "I'm here!")
});
//********************************************
// Companies Section
//********************************************
controller.hears(
['add company'], ['direct_message', 'direct_mention'],
function (bot, message) {
bot.startConversation(message, function (err, convo) {
convo.ask('What company do you want to add?', function (answer, convo) {
botData.companyList.push(answer.text);
saveData();
// do something with this answer!
// storeTacoType(convo.context.user, taco_type);
convo.say("I have added " + answer.text + " to the list."); // add another reply
convo.next(); // continue with conversation
});
});
});
controller.hears(
['list companies', 'list company'], ['direct_mention', 'direct_message'],
function (bot, message) {
response = 'The companies participating in the Guantlet Challenge are: \n';
botData.companyList.forEach(function (item) {
response += item + '\n';
})
bot.reply(message, response);
});
controller.hears(
['drop companies', 'delete company'], ['direct_mention', 'direct_message'],
function (bot, message) {
response = 'The companies participating in the Guantlet Challenge are: \n';
botData.companyList.forEach(function (item) {
response += item + '\n';
})
bot.reply(message, response);
});
//************************************************
// Challenger Section
//************************************************
controller.hears(
['register challenger'], ['direct_message', 'direct_mention'],
function (bot, message) {
bot.startConversation(message, function (err, convo) {
question = 'Please type the number of the company that will become the challenger:\n';
question += displayArray(botData.companyList);
convo.ask(question, function (answer, convo) {
index = parseInt(answer.text);
if ((typeof index == "number") &&
(index <= botData.companyList.length) &&
(index >= 0)
) {
botData.challenger = botData.companyList[index];
saveData();
convo.say(botData.challenger + " is now the challenger.");
} else {
convo.say("Nice try funny guy \"" + answer.text + "\" is not a valid answer");
}
convo.next(); // continue with conversation
});
});
});
controller.hears(
['who is the challenger', 'list challenger', 'challenger'], ['direct_mention', 'direct_message'],
function (bot, message) {
response = 'The challenger is: ' + botData.challenger;
bot.reply(message, response);
});
//************************************************
// START CHALLENGE
//************************************************
controller.hears(
['Challenge', 'challenge'], ['direct_message', 'direct_mention'],
function (bot, message) {
bot.startConversation(message, function (err, convo) {
convo.say('Oh boy, challenge time!');
question = 'The gauntlet will be dropped\nPlease pick a number for the company you want to challenge:\n';
i = 0;
botData.companyList.forEach(function (item) {
question += i + ': ' + item + '\n';
i++;
})
convo.ask(question, function (answer, convo) {
index = parseInt(answer.text);
if ((typeof index == "number") &&
(index <= botData.companyList.length) &&
(index >= 0)
) {
if (botData.companyList[index] == botData.challenger) {
convo.say("You can't challenge yourself now. Wait until you're alone tonight");
} else {
botData.challengee = botData.companyList[index];
saveData();
convo.say("THE CHALLENGE IS SET");
convo.say(":boom:It's " + botData.challenger + ' versus ' + botData.challengee + ":boom:");
}
} else {
convo.say("Nice try funny guy \"" + answer.text + "\" is not a valid answer");
}
convo.next(); // continue with conversation
});
});
});
//************************************************
// Set Countdown
//************************************************
controller.hears(
['set timer', 'set countdown'], ['direct_message', 'direct_mention'],
function (bot, message) {
bot.startConversation(message, function (err, convo) {
question = 'Please type the number of the year:\n';
question += displayArray(yearPicker);
convo.ask(question, function (answer, convo) {
index = parseInt(answer.text);
if ((typeof index == "number") &&
(index <= yearPicker.length) &&
(index >= 0)
) {
botData.countdownTimer.year = yearPicker[index];
question2 = 'Please type the number of the month:\n';
question2 += displayArray(monthPicker);
convo.ask(question2, function (answer, convo) {
index = parseInt(answer.text);
if ((typeof index == "number") &&
(index <= monthPicker.length) &&
(index >= 0)
) {
botData.countdownTimer.month = monthPicker[index];
question3 = 'Day?:\n';
question3 += displayArray(dayPicker);
convo.ask(question3, function (answer, convo) {
index = parseInt(answer.text);
if ((typeof index == "number") &&
(index <= dayPicker.length) &&
(index >= 0)
) {
botData.countdownTimer.day = dayPicker[index];
question4 = 'Hour?\n';
question4 += displayArray(hourPicker);
convo.ask(question4, function (answer, convo) {
index = parseInt(answer.text);
if ((typeof index == "number") &&
(index <= hourPicker.length) &&
(index >= 0)
) {
botData.countdownTimer.hour = hourPicker[index];
question5 = 'AM or PM?\n';
question5 += displayArray(meridianPicker);
convo.ask(question5, function (answer, convo) {
index = parseInt(answer.text);
if ((typeof index == "number") &&
(index <= meridianPicker.length) &&
(index >= 0)
) {
if (index == 1) {
botData.countdownTimer.hour += 12;
}
question6 = 'Minute?\n';
question6 += displayArray(minutePicker);
convo.ask(question6, function (answer, convo) {
index = parseInt(answer.text);
if ((typeof index == "number") &&
(index <= minutePicker.length) &&
(index >= 0)
) {
botData.countdownTimer.minute = minutePicker[index];
saveData();
convo.say("The timer is now set for: " + botData.countdownTimer.year +
"-" + botData.countdownTimer.month +
"-" + botData.countdownTimer.day +
" " + botData.countdownTimer.hour +
":" + botData.countdownTimer.minute);
} else {
convo.say("Nice try funny guy \"" + answer.text + "\" is not a valid answer");
}
convo.next(); // continue with conversation
});
} else {
convo.say("Nice try funny guy \"" + answer.text + "\" is not a valid answer");
}
convo.next(); // continue with conversation
});
} else {
convo.say("Nice try funny guy \"" + answer.text + "\" is not a valid answer");
}
convo.next(); // continue with conversation
});
} else {
convo.say("Nice try funny guy \"" + answer.text + "\" is not a valid answer");
}
convo.next(); // continue with conversation
});
} else {
convo.say("Nice try funny guy \"" + answer.text + "\" is not a valid answer");
}
convo.next(); // continue with conversation
});
} else {
convo.say("Nice try funny guy \"" + answer.text + "\" is not a valid answer");
}
convo.next(); // continue with conversation
});
});
});
controller.hears(
['get timer'], ['direct_mention', 'direct_message'],
function (bot, message) {
bot.reply(message, "The timer is now set for: " + botData.countdownTimer.year +
"-" + botData.countdownTimer.month +
"-" + botData.countdownTimer.day +
" " + botData.countdownTimer.hour +
":" + botData.countdownTimer.minute);
});
controller.hears(
['help'], ['direct_mention', 'direct_message'],
function (bot, message) {
bot.reply(message, "Here are a list of all commands GauntletBot is currently capable of:\n" +
"*add company* - Add a new company to the Gauntlet list. Please ensure that the company has a photo and a short bio that can be added to the website.\n" +
"*list company* or *list companies* - Will provide a list of all companies currently available to challenge.\n" +
"*register challenger* - Set the current “Challenger” that must challenge another company within the deadline set.\n" +
"*who is the challenger*, *list challenger*, or *challenger* - Will display who the current challenger is.\n" +
"*challenge* - The command that the challenger will call to challenge a new company.\n" +
"*set timer* or *set countdown* - Use this command to set the time of the challenge event.\n" +
"*get timer* - Will display the current event time.");
});
//************************************************
// Stupid easter egg section
//************************************************
controller.hears(
['hello', 'hi', 'greetings'], ['direct_mention', 'direct_message'],
function (bot, message) {
bot.reply(message, 'Hello!');
});
/**
* AN example of what could be:
* Any un-handled direct mention gets a reaction and a pat response!
controller.on('direct_message,mention,direct_mention', function (bot, message) {
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'robot_face',
}, function (err) {
if (err) {
console.log(err)
}
bot.reply(message, 'Instructions Unclear. GauntletBot now stuck in ceiling fan.');
});
});
*/
//***************************************************
// WEB SOCKET STUFF
// should this be here? definitely not but i'm hacking it together so deal with it
//***************************************************
//const WebSocket = require('ws');
const server = http.createServer({});
const wss = new WebSocket.Server({
server,
port: 9999
});
wss.on('connection', function connection(ws) {
console.log('here1');
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
console.log('here2: ' + botData);
ws.send(JSON.stringify(botData));
console.log('here3');
});