-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandupbot.js
892 lines (736 loc) · 28.6 KB
/
standupbot.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
/**
* Standup Slack Bot
* Author: fplaras
* Last Updated: 5/9/2018
*/
/**
* Required node modules
*/
var Botkit = require('botkit');
var request = require('request');
var moment = require('moment');
var moment = require('moment-timezone');
/**
* API URLS
*/
var apiUrl = process.env.api;
//Define Controller
var controller = Botkit.slackbot({
debug: false,
interactive_replies: false
}).configureSlackApp(
{
clientId: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
scopes: ['bot','incoming-webhook'],
}
);
var bot = controller.spawn({
token: process.env.SLACK_BOT
}).startRTM();
/**
* Slack API Methods
*/
var users = bot.api.users.list({
token: bot.token
}, function (err, response) {
users = response.members;
});
var privateChannels = bot.api.groups.list({
token: bot.token
}, function (err, response) {
privateChannels = response.groups;
});
var publicChannels = bot.api.channels.list({
token: bot.token
}, function (err, response) {
publicChannels = response.channels;
});
/**
* User Array
*/
var standupUserDetails = [];
var pendingStandupUserDetails = [];
var endOfStandupMinutes = 15;
var botPocChannelName = process.env.testchannel;
/**
* Timers
*/
var StandupExpirationChecker = setInterval(function () {
//console.log("StandupExpirationChecker");
//Mark expired those users who have passed the expiration threshold
standupUserDetails.forEach(function(element){
if(moment().tz("America/New_York").isSameOrAfter(moment(element.ExpireTime)) && element.StandupExpired == false){
element.StandupExpired = true;
var standupToSave = [];
//Report to user
reportToUsers(element);
//Report to channel
reportToStandupChannel(element);
//Save to database
standupToSave.push(element);
saveToDb(standupToSave);
}
});
}, 15000);
var StandupReportChecker = setInterval(function () {
//console.log("StandupReportChecker");
//Report to user and channel those who have expired standups
standupUserDetails.forEach(function(element, index, object){
if(element.StandupExpired){
var standupCompleted = element;
//Remove from list
object.splice(index,1);
//Start queued standup for user
startQueuedStandup(standupCompleted.SlackUserId);
}
})
},30000);
/**
* Bot Controllers
*/
controller.hears(['((^|, )(^start |^Start))'], 'direct_message', function (bot, message) {
//get list of authorized user standups to start
var options = {
method: 'get',
json: true,
url: apiUrl + process.env.ApiRootGetStandups,
qs:{slackUserId: message.user}
}
request(options, function (err, res, body) {
if (err) {
reportErrorToChannel(err);
} else {
if(res.statusCode != 200){
reportErrorToChannel(body.message);
}else{
var standupChoices = [];
body.forEach(function(element){
var choice = {
text: element.StandupName,
value: element.StandupId
}
standupChoices.push(choice);
});
bot.reply(message, {
"attachments": [
{
"text": "Choose a standup to start",
"fallback": "Uh oh...",
"color": "#3AA3E3",
"attachment_type": "default",
"callback_id": "standup_selection",
"actions": [
{
"name": "standup_list",
"text": "Pick a standup...",
"type": "select",
"options": standupChoices
}
]
}
]
});
}
}
});
});
controller.hears(['((^|, )(^edit |^Edit))'], 'direct_message', function (bot, message) {
var answerChoices = [];
standupUserDetails.forEach(function(element, index, object){
if(element.SlackUserId == message.user){
element.QuestionAnswerList.forEach(function(question, answerIndex, questionObject){
var choice = {
text: question.Answer,
value: index + "|" + element.DmChannelId + "|" + element.SlackUserId + "|" + answerIndex
}
answerChoices.push(choice);
});
}
});
bot.reply(message, {
"attachments": [
{
"text": "Choose a an entry to edit",
"fallback": "Uh oh...",
"color": "#3AA3E3",
"attachment_type": "default",
"callback_id": "edit_selection",
"actions": [
{
"name": "question_answer_list",
"text": "Pick an entry...",
"type": "select",
"options": answerChoices
}
]
}
]
});
});
controller.hears(['((^|, )(^y |^Y |^yesterday |^Yesterday))'], ['direct_message'], function (bot, message) {
if (hasActiveStandup(message.user)) {
if (!hasUserSessionExpired(message.user)) {
var indexForUser = getIndexForUser(message.user);
standupUserDetails[indexForUser].QuestionAnswerList.push({
QuestionText: 'Yesterday',
Answer: message.text.substring(message.text.indexOf(" "))
});
bot.reply(message, ":trophy: Got it! If you need help to continue, type `help`");
} else {
bot.reply(message, ":no_entry: Your standup session has expired");
}
} else {
bot.reply(message, ":no_entry: You are not shceduled for a standup or session has expired");
}
});
controller.hears(['((^|, )(^o |^O |^obstacle |^Obstacle))'], ['direct_message'], function (bot, message) {
if (hasActiveStandup(message.user)) {
if (!hasUserSessionExpired(message.user)) {
var indexForUser = getIndexForUser(message.user);
standupUserDetails[indexForUser].QuestionAnswerList.push({
QuestionText: 'Obstacle',
Answer: message.text.substring(message.text.indexOf(" "))
});
bot.reply(message, ":red-light-blinker: Got it! If you need help to continue, type `help`");
} else {
bot.reply(message, ":no_entry: You standup session has expired");
}
} else {
bot.reply(message, ":no_entry: You are not scheduled for a standup or session has expired");
}
});
controller.hears(['((^|, )(^t |^T |^today |^Today))'], ['direct_message'], function (bot, message) {
if (hasActiveStandup(message.user)) {
if (!hasUserSessionExpired(message.user)) {
var indexForUser = getIndexForUser(message.user);
standupUserDetails[indexForUser].QuestionAnswerList.push({
QuestionText: 'Today',
Answer: message.text.substring(message.text.indexOf(" "))
});
bot.reply(message, ":date: Got it! If you need help to continue, type `help`");
} else {
bot.reply(message, ":no_entry: You standup session has expired");
}
} else {
bot.reply(message, ":no_entry: You are not shceduled for a standup or session has expired");
}
});
controller.hears(['((^|, )(^help |^Help))'], ['direct_message'], function (bot, message) {
if (hasActiveStandup(message.user)) {
if (!hasUserSessionExpired(message.user)) {
var reply_with_attachement = [
{
"color": "#77BED1",
"mrkdwn_in": ["text", "pretext","fields"],
"pretext": "",
"title": "Instructions",
"text": "",
"fields": [
{
"title": ":trophy: Add something you accomplished yesterday",
"value": ">>>Type `y` or yesterday` before each entry to save an accomplishment \n *Example*: `yesterday One thing i did`",
"short": false
},
{
"title": ":calendar: Add something you plan to accomplish today",
"value": ">>>Type `t` or today` before each entry to save what you plan on doing \n *Example*: `today One thing i plan on doing`",
"short": false
},
{
"title": ":red-light-blinker: Add an obstacle that is preventing you from completing your task",
"value": ">>>Type `o` or `obstacle` before each entry to save an obstacle to your task \n *Example*: `obstacle One of the obstacles` \n If you have *no* obstacles dont add anything",
"short": false
},
{
"title": "To edit an entry type `edit` select the entry to edit and update the text",
"value": "",
"short": false
}
]
}];
postMessageWithAttachement(message.channel,null, reply_with_attachement)
} else {
bot.reply(message, "Your standup session has expired");
}
} else {
bot.reply(message, ":no_entry: You are not shceduled for a standup or session has expired");
}
});
/**
* WEBSERVER TO HANDLE BUTTON RESPONSES FROM SLACK
*/
controller.setupWebserver(process.env.PORT,function(err,webserver) {
controller.createWebhookEndpoints(controller.webserver);
controller.createOauthEndpoints(controller.webserver,function(err,req,res) {
if (err) {
res.status(500).send('ERROR: ' + err);
} else {
res.send('Success!');
}
});
//handle gets
webserver.get('/',function(req,res) {
console.log('get');
});
//handle post
webserver.post('/',function(req,res) {
console.log('post');
var requestPayload = JSON.parse(req.body.payload);
switch (requestPayload.callback_id){
case 'standup_selection':{
res.send('Standup started.');
var options = {
method: 'get',
json: true,
url: apiUrl + process.env.ApiRootGetStandupUsers,
qs:{standupId: requestPayload.actions[0].selected_options[0].value}
}
request(options, function (err, resp, body) {
if (err) {
reportErrorToChannel(err);
} else {
if(resp.statusCode != 200){
reportErrorToChannel(body.message);
}else{
body.forEach(function (element) {
//for each user open an IM channel
bot.api.im.open({
token: bot.token,
user: element.USER_ID,//From the list of users retrieve the ID
return_im: false
}, function (err, response) {
if (err) {
reportErrorToChannel(err);
} else {
var userDetails = {
StandupId: '',
UserId: '',
StandupExpired: false,
DmChannelId: '',
StandupName:'',
ReportChannel:'',
StartTime:'',
ExpireTime:'',
QuestionAnswerList: []
}
userDetails.StandupId = element.STANDUP_ID;
userDetails.SlackUserId = element.USER_ID;
userDetails.StandupName = element.STANDUP_NAME;
userDetails.ReportChannel = element.STANDUP_REPORT_CHANNEL;
userDetails.StandupExpired = false;
userDetails.StartTime = moment().tz("America/New_York").format('MMM DD YYYY hh:mm:ss a');
userDetails.ExpireTime = moment().tz("America/New_York").add(endOfStandupMinutes,'minutes');
userDetails.DmChannelId = response.channel.id;
//check if user has already been added to a standup
if(hasActiveStandup(element.USER_ID)){
if(!inPendingStandupList(userDetails)){
pendingStandupUserDetails.push(userDetails);
postMessageWithAttachement(response.channel.id, ":information_source: The standup for `" + element.STANDUP_NAME + "` has been queued for you! :information_source:")
}
}else{
standupUserDetails.push(userDetails);
postNewStandupMessageToUser(userDetails);
}
}
});
}, this);
}
}
});
}
break;
case "edit_selection":{
res.send('Retrieving your entries...');
var editDetails = requestPayload.actions[0].selected_options[0].value.split("|");
//index|channel|userId|answerIndex
var message = {
user: editDetails[2]
};
bot.startPrivateConversation(message, function(err, convo) {
convo.say("Editing: " + getAnswerForUserByIndex(editDetails[0],editDetails[3]));
convo.ask('Enter the text to replace the selection',
function(response, convo) {
standupUserDetails[editDetails[0]].QuestionAnswerList[editDetails[3]].Answer = response.text;
convo.next();
});
convo.say("Updated");
});
}
break;
default:{
}
break;
}
});
});
/**
* HELPER FUNCTIONS
*/
var getAnswerForUserByIndex = function(userIndex, answerIndex){
var text = '';
text = standupUserDetails[userIndex].QuestionAnswerList[answerIndex].Answer;
return text;
};
var reportChannelUsersInStandup = function(standupId){
var users = '';
var reportChannel = '';
standupUserDetails.forEach(function(element){
if(element.StandupId == standupId){
if(users == ''){
users = getUserName(element.SlackUserId);
}else{
users += " | " + getUserName(element.SlackUserId);
}
if(!reportChannel){
reportChannel = element.ReportChannel;
}
}
});
pendingStandupUserDetails.forEach(function(element){
if(element.StandupId == standupId){
if(users == ''){
users = getUserName(element.SlackUserId);
}else{
users += " | " + getUserName(element.SlackUserId);
}
}
});
postMessageWithAttachement(getPrivateChannelId(reportChannel), "Users in standup: " + users);
};
var hasActiveStandup = function (userId) {
var hasActiveStandup = false;
for (var i = 0; i < standupUserDetails.length; i++) {
if (standupUserDetails[i].SlackUserId == userId) {
hasActiveStandup = true;
}
}
return hasActiveStandup;
};
var inPendingStandupList = function (userDetails) {
var inList = false;
pendingStandupUserDetails.forEach(function(element){
if(element.SlackUserId == userDetails.SlackUserId && element.StandupId == userDetails.StandupId){
inList = true;
}
});
return inList;
};
var postNewStandupMessageToUser = function (userDetails) {
var reply_with_attachement = [
{
"color": "#77BED1",
"mrkdwn_in": ["text", "pretext","fields"],
"pretext": "This standup is for `"+ userDetails.StandupName +"` available for the next `" + endOfStandupMinutes +" minutes` and will end at `"+moment().tz("America/New_York").add(endOfStandupMinutes,'minutes').format('hh:mm:ss a')+ "`",
"title": "",
"text": "*For instructions type `help`*"
}
];
postMessageWithAttachement(userDetails.DmChannelId, null, reply_with_attachement)
};
var postMessageWithAttachement = function (channelId, message, messageAttachments) {
bot.api.chat.postMessage({
token: bot.token,
channel: channelId,
as_user: true,
text: message,
attachments: messageAttachments
}, function (err, response) {
if (err) {
reportErrorToChannel(err, messageAttachments);
}
});
};
var reportToUsers = function (userDetails) {
var answersForUser = getAnswersForUser(userDetails);
postMessageWithAttachement(userDetails.DmChannelId, 'Responses have been posted for Standup *' + userDetails.StandupName + '*. Below is your summary', answersForUser);
};
var reportToStandupChannel = function (userDetails) {
var username = getUserName(userDetails.SlackUserId);
var answersForUser = getAnswersForUserForReportChannel(userDetails.SlackUserId);
var attachement = [
{
"color": "#54D45C",
"mrkdwn_in": ["text", "pretext","fields"],
"author_name": '<@'+userDetails.SlackUserId+'>',
"author_link": "<@"+userDetails.SlackUserId+">",
"author_icon": "https://cdn.iconscout.com/public/images/icon/free/png-512/child-parent-free-ngo-avatar-male-person-30c4bd7e29ce3473-512x512.png",
"pretext": "*Standup Responses for * `" + userDetails.StandupName + "` started at: `"+ userDetails.StartTime + "`",
"title": "",
"text": "-------------------------------",
"fields": answersForUser
}
];
postMessageWithAttachement(getPrivateChannelId(userDetails.ReportChannel), null, attachement);
};
var reportErrorToChannel = function (message, messageAttachments) {
if (messageAttachments) {
bot.api.chat.postMessage({
token: bot.token,
channel: getPrivateChannelId(botPocChannelName),
as_user: true,
text: "Error: " + message,
attachments: messageAttachments
}, function (err, response) {
});
} else {
bot.api.chat.postMessage({
token: bot.token,
channel: getPrivateChannelId(botPocChannelName),
as_user: true,
text: "Error: " + message
}, function (err, response) {
});
}
};
var getAnswersForUserForReportChannel = function (userId) {
var yesterdayItems = [];
var todayItems = [];
var obstacleItems = [];
for (var i = 0; i < standupUserDetails.length; i++) {
if (standupUserDetails[i].SlackUserId == userId) {
standupUserDetails[i].QuestionAnswerList.forEach(function (question) {
switch (question.QuestionText) {
case 'Yesterday': {
yesterdayItems.push("• " + question.Answer);
}
break;
case 'Today': {
todayItems.push("• " + question.Answer);
}
break;
case 'Obstacle': {
if(question.Answer != ''){
obstacleItems.push("• " + question.Answer);
}
}
break;
default:
break;
}
});
var answers;
if(obstacleItems.length > 0){
answers = [
{
"title": ":trophy: Yesterday Items",
"value": "",
"short": false
},
{
"title": ":date: Today Items",
"value": "",
"short": false
},
{
"title": ":red-light-blinker: Obstacle Items",
"value": "",
"short": false
}
];
}else{
answers = [
{
"title": ":trophy: Yesterday Items",
"value": "",
"short": false
},
{
"title": ":date: Today Items",
"value": "",
"short": false
}
];
}
yesterdayItems.forEach(function (item) {
answers[0].value += item + "\n";
});
todayItems.forEach(function (item) {
answers[1].value += item + "\n";
});
obstacleItems.forEach(function (item) {
answers[2].value += item + "\n";
});
}
}
return answers;
};
var getAnswersForUser = function (userDetails) {
var yesterdayItems = [];
var todayItems = [];
var obstacleItems = [];
userDetails.QuestionAnswerList.forEach(function(question){
switch (question.QuestionText) {
case 'Yesterday': {
yesterdayItems.push("• " + question.Answer);
}
break;
case 'Today': {
todayItems.push("• " + question.Answer);
}
break;
case 'Obstacle': {
if(question.Answer != ''){
obstacleItems.push("• " + question.Answer);
}
}
break;
default:
break;
}
});
var answers;
if(obstacleItems.length > 0){
answers = [{
"color": "#D4D454",
"title": ":trophy: Yesterday Items",
"pretext": "",
"text": "",
"mrkdwn_in": ["text", "pretext"]
}, {
"color": "#5484D4",
"title": ":date: Today Items",
"pretext": "",
"text": "",
"mrkdwn_in": ["text", "pretext"]
}, {
"color": "#D45454",
"title": ":red-light-blinker: Obstacle Items",
"pretext": "",
"text": "",
"mrkdwn_in": ["text", "pretext"]
}];
}else{
answers = [{
"color": "#D4D454",
"title": ":trophy: Yesterday Items",
"pretext": "",
"text": "",
"mrkdwn_in": ["text", "pretext"]
}, {
"color": "#5484D4",
"title": ":date: Today Items",
"pretext": "",
"text": "",
"mrkdwn_in": ["text", "pretext"]
}];
}
yesterdayItems.forEach(function (item) {
answers[0].text += item + "\n";
});
todayItems.forEach(function (item) {
answers[1].text += item + "\n";
});
obstacleItems.forEach(function (item) {
answers[2].text += item + "\n";
});
return answers;
};
var startQueuedStandup = function (slackUser) {
//check if user has pending standup
pendingStandupUserDetails.forEach(function(element, index, object){
if(element.SlackUserId == slackUser){
//has pending standup
//Check that user is not in an active standup
if(!hasActiveStandup(slackUser)){
element.ExpireTime = moment().tz("America/New_York").add(endOfStandupMinutes,'minutes');
//add to active standup list
standupUserDetails.push(element);
//post standup to user
postNewStandupMessageToUser(element);
//remove this element from pending
object.splice(index, 1);
}
}
});
};
var getUserName = function (userId) {
var username = '';
for (var i = 0; i < users.length; i++) {
if (users[i].id == userId) {
username = users[i].name;
return username;
}
}
return username;
};
var getPrivateChannelId = function (channelName) {
//standup_results
var channelId = '';
for (var i = 0; i < privateChannels.length; i++) {
if (privateChannels[i].name == channelName) {
channelId = privateChannels[i].id;
return channelId;
}
}
return channelId;
};
var getPublicChannelId = function (channelName) {
//standup_results
var channelId = '';
for (var i = 0; i < publicChannels.length; i++) {
if (publicChannels[i].name == channelName) {
channelId = publicChannels[i].id;
return channelId;
}
}
return channelId;
};
var saveToDb = function (standupToSave) {
postData(standupToSave, 'Standup');
};
var getIndexForUser = function (userId) {
var userIndex = -1;
standupUserDetails.forEach(function(element, index, object){
if(element.SlackUserId == userId){
userIndex = index;
return userIndex;
}
});
return userIndex;
};
var hasUserSessionExpired = function (userId) {
var sessionExpired = true;
standupUserDetails.forEach(function(element, index, object){
if(element.SlackUserId == userId && !element.StandupExpired){
sessionExpired = false;
}
});
return sessionExpired;
};
/**
* POST / GET Methods
*/
var postData = function (jsonObj, apiName) {
var options = {
method: 'post',
body: jsonObj,
json: true,
url: apiUrl + apiName
}
request(options, function (err, res) {
if (err) {
reportErrorToChannel(err);
} else {
return (res);
}
});
};
var getData = function (jsonObj, apiName) {
var options = {
method: 'get',
data: jsonObj,
json: true,
url: apiUrl + apiName
}
request(options, function (err, res, body) {
if (err) {
reportErrorToChannel(err);
} else {
if (body.Message)
reportErrorToChannel(body.Message);
else
return (body);
}
});
};