-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
941 lines (864 loc) · 39.3 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
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
var Botkit = require('botkit');
var os = require('os');
var Moment = require('moment-timezone');
var BeepBoop = require('beepboop-botkit');
var logger = require('morgan');
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: './db_slackbutton_slash_command/',
};
}
config.debug = true;
config.logLevel = 7;
config.retry = Infinity;
config.interactive_replies = true;
config.hostname = '0.0.0.0';
var PORT = process.env.PORT || 8080;
var controller = Botkit.slackbot(config);
var beepboop = BeepBoop.start(controller, { debug: true });
controller.setupWebserver(PORT, function (err, webserver) {
if (err) {
console.error(err);
process.exit(1);
}
webserver.use(logger('tiny'))
// Setup our slash command webhook endpoints
controller.createWebhookEndpoints(webserver);
//controller.createWebhookEndpoints(controller.webserver);
});
var raffleList = [];
controller.hears(['help'], 'direct_message,direct_mention', function (bot, message) {
bot.reply(message, "I am your Survey Bot :survey_bot:" +
"\nI can start a Survey on your command & update the channel by uploading a file when the survey finishes or when you request." +
"\nOnly authorized members in every channel can start survey." +
"\nCurrently only @ojas.gosar is authorized to start scrum (in order to avoid spam in public channels)." +
"\n`coming soon - you can automate the process of creating & updating survey's`" +
"\nTry `@survey_bot start` - to start a survey" +
"\nTry `@survey_bot status` - to find out the result of the survey");
});
controller.on('slash_command', function (slashCommand, message) {
console.log("message:", message);
console.log("slashCommand:", slashCommand);
//console.log("slashCommand.webserver.api.users.info:", slashCommand.webserver.api.users.info);
switch (message.command) {
case "/poll":
var pollText = message.text.split(os.EOL).map((it) => { return it.trim() })
//message.text.trim().split(/\r?\n/);
if (!pollText[0]) {
slashCommand.replyPrivate(message, "Please provide a question..");
break;
}
if (!(pollText.length > 1) || pollText.length > 16) {
slashCommand.replyPrivate(message, "You may only provide 15 options. Here is what you entered: " + message.text);
break;
}
var question = pollText[0];
var actions = []
for (var i = 1; i < pollText.length; i++) {
var answer = pollText[i];
actions.push({
name: 'answer',
text: answer,
type: 'button',
value: answer,
style: 'default'
})
}
var attachments = []
actions.forEach((action, num) => {
var idx = Math.floor(num / 5)
if (!attachments[idx]) {
attachments[idx] = {
text: '',
fallback: question,
callback_id: 'poll_callback',
color: '#78449b',
actions: []
}
}
attachments[idx].actions.push(action)
})
var bottomActions = [{ name: 'resurrect', text: ':arrow_double_down: Resurrect', type: 'button', value: 'resurrect', style: 'default' }]
attachments.push({
text: '',
fallback: 'move to the bottom',
callback_id: 'poll_callback',
actions: bottomActions
})
slashCommand.api.users.info({
user: message.user
}, function(err, userInfo) {
if (err) {
slashCommand.botkit.log('Failed to get channel info :(', err);
slashCommand.replyPrivate(message, "Sorry, something went wrong. Try again?");
}
else {
attachments[0].author_name = `asked by ${userInfo.user.profile.real_name || userInfo.user.name}`
attachments[0].author_icon = userInfo.user.profile.image_24
slashCommand.replyPublic(message, {
text: question,
attachments: attachments
});
}
});
break;
default:
slashCommand.replyPrivate(message, "I'm afraid I don't know how to " + message.command + " " + message.text + " yet.");
}
});
controller.hears(['start survey', 'start', 'survey'], 'direct_message,direct_mention,mention', function(bot, message) {
if (message.user == 'U2K8XK03Z' || message.user == 'U23RT8WQ4') {
//var users = [];
bot.api.channels.info({
channel: message.channel
}, function(err, info) {
if (err) {
bot.botkit.log('Failed to get channel info :(', err);
bot.reply(message,"I can't start survey outside of a channel or in a private channel." +
"\nIf you havent already invited me to a public channel then try `/invite @survey_bot`" +
"\nThen `@survey_bot start` to start a survey" +
"\nYou can also type `@survey_bot help` to find out what i can do for you..");
}
else {
var date = Moment().format("YYYYMMDD");
bot.reply(message,"Starting Survey now");
for (var i = 0; i < info.channel.members.length; i++) {
console.log(info.channel.members[i]);
bot.api.users.info({
user: info.channel.members[i]
}, function(err, userInfo) {
if(userInfo.user.is_bot == false) {
console.log("user name:" + userInfo.user.name + " user id:" + userInfo.user.id + " is_bot:" + userInfo.user.is_bot);
bot.startPrivateConversation({user: userInfo.user.id}, function(response, convo) {
console.log(convo);
convo.ask({
text: "How would you rate this BBL?",
attachments:[
{
title: 'choose your feeling..',
fallback: 'You are unable to choose the actions!',
callback_id: 'rate_bbl',
attachment_type: 'default',
actions: [
{
"name":"okay",
"text": "Okay",
"value": "it was okay",
"type": "button",
},
{
"name":"like",
"text": "Like",
"value": "i liked it",
"type": "button",
},
{
"name":"awesome",
"text": "Awesome",
"value": "it was awesome",
"type": "button",
}
]
}
]
},[
{
pattern: "it was okay",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("How would you rate this BBL?", "Okay: It was only :ok:..", "rate_bbl_okay"));
//convo.say('it was only okay?');
useSlackQuestion(reply, convo, bot);
convo.next();
// do something awesome here.
}
},
{
pattern: "i liked it",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("How would you rate this BBL?", "Like: I am glad you liked it!", "rate_bbl_like"));
//convo.say('I am glad you liked it!');
useSlackQuestion(reply, convo, bot);
convo.next();
}
},
{
pattern: "it was awesome",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("How would you rate this BBL?", "Awesome: I am flying high :rocket:", "rate_bbl_awesome"));
//convo.say('I am flying high :rocket:');
useSlackQuestion(reply, convo, bot);
convo.next();
}
},
{
default: true,
callback: function(reply, convo) {
convo.say('you chose not to click my buttons.. hmm i wonder');
convo.next();
console.log("reply:",reply);
console.log("convo:",convo);
console.log("response:",response);
}
}
],{'key': 'rate_bbl'});
convo.on('end', function(dm) {
if (dm.status == 'completed') {
controller.storage.users.get(userInfo.user.id, function(err, user) {
if (!user) {
user = {
id: userInfo.user.id,
realName: userInfo.user.name,
channels: []
}
}
feedback = "\n\nFeedback for @" + user.realName +
":\n How would you rate this BBL?\n "+dm.extractResponse('rate_bbl') +
":\n\n What do you think of Slack?\n "+dm.extractResponse('rate_slack') +
"\n\n Which bot would you want to use?\n " + dm.extractResponse('fav_bot') +
"\n\n Which of the following ideas would you like to see as bots?\n " + dm.extractResponse('otherIdeas') +
"\n\n Any other ideas/pain points/comments/feedback..?\n " + dm.extractResponse('extraComments');
user.channels.push({
id: message.channel,
surveys: [{
id: 'bbl',
text:feedback
}]
});
console.log("User Object: ", user)
controller.storage.users.save(user, function(err, id) {
console.log("User:",id);
});
})
}
else {
bot.startPrivateConversation(response.user, function(response, convo) {
convo.say('OK, this didnt go well, Sorry about that..');
});
}
});
});
}
});
}
setTimeout(function() {
//get status & upload a file
getStatusAndUpload(message, 'bbl', bot);
}, process.env.CHANNEL_SURVEY_TIMEOUT);
}
}.bind(this));
}
else {
bot.reply(message, 'Sorry <@' + message.user + '>, you are not authorized to spam :wink:');
}
});
controller.hears(['raffle'], 'direct_message,direct_mention,mention', function(bot, message) {
if (message.user == 'U2K8XK03Z' || message.user == 'U23RT8WQ4') {
//var users = [];
bot.api.channels.info({
channel: message.channel
}, function(err, info) {
if (err) {
bot.botkit.log('Failed to get channel info :(', err);
bot.reply(message,"I can't start raffle outside of a channel or in a private channel." +
"\nIf you havent already invited me to a public channel then try `/invite @survey_bot`" +
"\nThen `@survey_bot start` to start a survey" +
"\nYou can also type `@survey_bot help` to find out what i can do for you..");
}
else {
bot.reply(message,"Starting raffle now..");
for (var i = 0; i < info.channel.members.length; i++) {
console.log(info.channel.members[i]);
bot.api.users.info({
user: info.channel.members[i]
}, function(err, userInfo) {
if(userInfo.user.is_bot == false) {
console.log("user name:" + userInfo.user.name + " user id:" + userInfo.user.id + " is_bot:" + userInfo.user.is_bot);
raffleList.push(userInfo.user.id)
}
});
}
setTimeout(function() {
//get status & upload a file
raffelResult(message, bot);
}, 60000);
}
}.bind(this));
}
else {
bot.reply(message, 'Sorry <@' + message.user + '>, you are not authorized to spam :wink:');
}
});
function raffelResult(message, bot) {
console.log("raffleList:",raffleList);
var thirdRandomNumber = Math.floor(Math.random() * raffleList.length);
console.log("thirdRandomNumber:",thirdRandomNumber);
var thirdPrize = raffleList[thirdRandomNumber];
console.log("thirdPrize:",thirdPrize);
raffleList.splice(thirdRandomNumber,1);
console.log("raffleList after third prize slice:",raffleList);
var secondRandomNumber = Math.floor(Math.random() * raffleList.length);
console.log("secondRandomNumber:",secondRandomNumber);
var secondPrize = raffleList[secondRandomNumber];
console.log("secondPrize:",secondPrize);
raffleList.splice(secondRandomNumber,1);
console.log("raffleList after second prize slice:",raffleList);
var firstRandomNumber = Math.floor(Math.random() * raffleList.length);
console.log("firstRandomNumber:",firstRandomNumber);
var firstPrize = raffleList[firstRandomNumber];
console.log("firstPrize:",firstPrize);
raffleList = [];
console.log("raffleList after first prize slice:",raffleList);
bot.reply(message, "Here are the winners of the Raffle :"+
"\nThird Prize: <@" + thirdPrize + ">" +
"\nSecond Prize: <@" + secondPrize + ">" +
"\nFirst Prize: <@" + firstPrize + ">");
bot.startPrivateConversation({user: thirdPrize}, function(response, convo) {
convo.say('yay, you got the third prize!');
});
bot.startPrivateConversation({user: secondPrize}, function(response, convo) {
convo.say('yay, you got the second prize!');
});
bot.startPrivateConversation({user: firstPrize}, function(response, convo) {
convo.say('yay, you got the first prize!');
});
}
function useSlackQuestion(reply, convo, bot) {
convo.ask({
text: "What do you think of Slack?",
attachments:[
{
title: 'choose your thought..',
fallback: 'You are unable to choose the actions!',
callback_id: 'rate_slack',
attachment_type: 'default',
actions: [
{
"name":"not my tool",
"text": "Not my tool :card_file_box: ",
"value": "not my tool",
"type": "button",
},
{
"name":"interested",
"text": "Interested :raised_hand::skin-tone-4: ",
"value": "interested",
"type": "button",
},
{
"name":"my favorite",
"text": "My Favorite :heart: ",
"value": "my favorite",
"type": "button",
}
]
}
]
},[
{
pattern: "not my tool",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("How do you think of Slack?", "Not my tool :neutral_face: ", "rate_slack_notMyTool"));
//convo.say('it was only okay?');
whichBotQuestion(reply, convo, bot);
convo.next();
// do something awesome here.
}
},
{
pattern: "interested",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("How do you think of Slack?", "Interested! :raised_hand::skin-tone-4: ", "rate_slack_interested"));
//convo.say('I am glad you liked it!');
whichBotQuestion(reply, convo, bot);
convo.next();
}
},
{
pattern: "my favorite",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("How do you think of Slack?", "My favorite :heart:", "rate_slack_fav"));
//convo.say('I am flying high :rocket:');
whichBotQuestion(reply, convo, bot);
convo.next();
}
},
{
default: true,
callback: function(reply, convo) {
convo.say('you chose not to click my buttons.. hmm i wonder');
convo.next();
console.log("reply:",reply);
console.log("convo:",convo);
console.log("response:",response);
}
}
],{'key': 'rate_slack'});
}
function whichBotQuestion(reply, convo, bot) {
convo.ask({
text: "Which bot would you want to use?",
attachments:[
{
title: 'choose your :robot_face:',
fallback: 'You are unable to choose the actions!',
callback_id: 'which_bot',
attachment_type: 'default',
actions: [
{
"name":"scrum bot",
"text": "@scrum_bot :scrum_bot:",
"value": "scrum bot",
"type": "button",
},
{
"name":"cats bot",
"text": "@cats_bot :cats_bot:",
"value": "cats bot",
"type": "button",
},
{
"name":"survey bot",
"text": "@survey_bot :survey_bot:",
"value": "survey bot",
"type": "button",
},
{
"name":"watson bot",
"text": "@watson_bot :watson_bot:",
"value": "watson bot",
"type": "button",
}
]
}
]
},[
{
pattern: "scrum bot",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("Which bot would you want to use?", "@scrum_bot :scrum_bot: - sounds like scrum masters take a lot of your time :wink: ", "bot_scrum"));
//convo.say('it was only okay?');
otherBotIdeas(reply, convo, bot);
convo.next();
// do something awesome here.
}
},
{
pattern: "cats bot",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("Which bot would you want to use?", "@cats_bot :cats_bot: - your PM will be very happy :smile: ", "bot_cats"));
//convo.say('I am glad you liked it!');
otherBotIdeas(reply, convo, bot);
convo.next();
}
},
{
pattern: "survey bot",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("Which bot would you want to use?", "@survey_bot :survey_bot: - sounds like you are a fan of quick results :racing_car: ", "bot_survey"));
//convo.say('I am flying high :rocket:');
otherBotIdeas(reply, convo, bot);
convo.next();
}
},
{
pattern: "watson bot",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("Which bot would you want to use?", "@watson_bot :watson_bot: - sounds like you are technology :ninja:", "bot_watson"));
//convo.say('I am flying high :rocket:');
otherBotIdeas(reply, convo, bot);
convo.next();
}
},
{
default: true,
callback: function(reply, convo) {
convo.say('you chose not to click my buttons.. hmm i wonder');
convo.next();
console.log("reply:",reply);
console.log("convo:",convo);
console.log("response:",response);
}
}
],{'key': 'fav_bot'});
}
function otherBotIdeas(reply, convo, bot) {
convo.ask({
text: "Which of the following ideas would you like to see as bots?",
attachments:[
{
title: 'choose your idea',
fallback: 'You are unable to choose the actions!',
callback_id: 'other_bot',
attachment_type: 'default',
actions: [
{
"name":"vacation tracker bot",
"text": "Vacation tracker :robot_face:",
"value": "vacation tracker bot",
"type": "button",
},
{
"name":"360 feedback bot",
"text": "360 Feedback :robot_face:",
"value": "360 feedback bot",
"type": "button",
},
{
"name":"complaint bot",
"text": "Complaint :robot_face:",
"value": "complaint bot",
"type": "button",
},
{
"name":"purchase order bot",
"text": "Purchase order :robot_face:",
"value": "purchase order bot",
"type": "button",
}
]
}
]
},[
{
pattern: "vacation tracker bot",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("Which of the following ideas would you like to see as bots?", "Vacation tracker bot - Sounds like you travel the world :world_map: ", "bot_vacationTracker"));
//convo.say('it was only okay?');
extraComments(reply, convo, bot);
convo.next();
// do something awesome here.
}
},
{
pattern: "360 feedback bot",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("Which of the following ideas would you like to see as bots?", "360 Feedback bot - sounds like you interested in growth :ok_hand::skin-tone-4: ", "bot_feedback"));
//convo.say('I am glad you liked it!');
extraComments(reply, convo, bot);
convo.next();
}
},
{
pattern: "complaint bot",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("Which of the following ideas would you like to see as bots?", "Complaint bot - sounds like you need some peace of mind :peace_symbol: ", "bot_complaint"));
//convo.say('I am flying high :rocket:');
extraComments(reply, convo, bot);
convo.next();
}
},
{
pattern: "purchase order bot",
callback: function(reply, convo) {
bot.replyInteractive(reply, fetchInteractiveReply("Which of the following ideas would you like to see as bots?", "Purchase order :robot_face: - sounds like you are the office admin :smile:", "bot_po"));
//convo.say('I am flying high :rocket:');
extraComments(reply, convo, bot);
convo.next();
}
},
{
default: true,
callback: function(reply, convo) {
convo.say('you chose not to click my buttons.. hmm i wonder');
convo.next();
console.log("reply:",reply);
console.log("convo:",convo);
console.log("response:",response);
}
}
],{'key': 'otherIdeas'});
}
function extraComments(reply, convo, bot) {
convo.ask("Any other ideas/pain points/comments/feedback..?", function(response, convo) {
//convo.say("Awesome.");
//askTodayStatus(response, convo);
convo.say("Awesome! Thank you for your valuable feedback :sunglasses: ");
convo.say("I'll make sure @ojas.gosar buys you a :beer: tonight.. ");
convo.next();
}, {'key': 'extraComments'});
}
function fetchInteractiveReply(interactiveText, interactiveTitle, interactiveCallbackId) {
var interactiveReply = {
text: interactiveText,
attachments:[
{
title: interactiveTitle,
fallback: 'nothing to fall back!',
callback_id: interactiveCallbackId,
attachment_type: 'default'
}
]
}
return interactiveReply;
}
beepboop.on('botkit.rtm.started', function (bot, resource, meta) {
var slackUserId = resource.SlackUserID
if (meta.isNew && slackUserId) {
bot.api.im.open({ user: slackUserId }, function (err, response) {
if (err) {
return console.log("im.open error:",err)
}
var dmChannel = response.channel.id
bot.say({channel: dmChannel, text: 'Thanks for adding me to your team!'})
bot.say({channel: dmChannel, text: 'Just /invite me to a channel!'})
})
}
});
controller.on('bot_channel_join', function (bot, message) {
console.log("bot_channel_join")
bot.reply(message, "I'm here!")
});
function getStatusAndUpload(message, key, bot){
controller.storage.users.all(function(err,userList) {
if (err) {
console.log("Error getting all users: ", err);
}
else {
console.log("Success all users: ", JSON.stringify(userList));
//var jsonUserList = JSON.stringify(userList);
var status = "";
for (user in userList) {
console.log("userList[user].channels", userList[user].channels);
for (userChannel in userList[user].channels) {
console.log("userList[user].channels[userChannel]:", userList[user].channels[userChannel]);
console.log("message.channel", message.channel);
if (userList[user].channels[userChannel].id == message.channel) {
for (surveys in userList[user].channels[userChannel].surveys) {
console.log("userList[user].channels[userChannel].surveys:", userList[user].channels[userChannel].surveys);
//console.log("key:", key);
if (userList[user].channels[userChannel].surveys[surveys].id == key) {
console.log("found Match:");
status += userList[user].channels[userChannel].surveys[surveys].text;
}
}
}
}
}
console.log("Final feedback: ", status);
bot.api.files.upload({
content:((!status)? "No feedback provided" : status),
filename: key+"Survey",
channels: message.channel
}, function(err,result) {
if (err) {
console.log("Error uploading file", err);
}
else {
console.log("Result:",result);
}
});
}
});
}
beepboop.on('add_resource', function (msg) {
console.log('received request to add bot to team')
});
// Send the user who added the bot to their team a welcome message the first time it's connected
beepboop.on('botkit.rtm.started', function (bot, resource, meta) {
var slackUserId = resource.SlackUserID
if (meta.isNew && slackUserId) {
bot.api.im.open({ user: slackUserId }, function (err, response) {
if (err) {
return console.log("im.open error:",err)
}
var dmChannel = response.channel.id
bot.say({channel: dmChannel, text: 'Thanks for adding me to your team!'})
bot.say({channel: dmChannel, text: 'Just /invite me to a channel!'})
})
}
});
controller.hears(['status', 'state'], 'direct_message,direct_mention,mention', function(bot, message) {
//var date = Moment().format("YYYYMMDD");
getStatusAndUpload(message, 'bbl', bot);
});
// areYouReadyForScrum = function(response, convo) {
// convo.ask('Its Scrum-time! Are you ready for standup?', [
// {
// pattern: bot.utterances.yes,
// callback: function(response, convo) {
// convo.say('Great, lets begin..');
// askYesterdayStatus(response, convo);
// convo.next();
// }
// },
// {
// pattern: bot.utterances.no,
// default: true,
// callback: function(response, convo) {
// convo.say('Alright, will ping you in sometime..');
// timeOutRepeat(response, convo);
// convo.next();
// }
// }
// ]);
// };
// askYesterdayStatus = function(response, convo) {
// console.log(convo);
// convo.ask("What did you do yesterday?", function(response, convo) {
// convo.say("Awesome.");
// askTodayStatus(response, convo);
// convo.next();
// }, {'key': 'yesterday'});
// };
// timeOutRepeat = function(response, convo) {
// setTimeout(function() {
// bot.startPrivateConversation(response, areYouReadyForScrum);
// convo.next();
// }, process.env.INDIVIDUAL_SCRUM_TIMEOUT);
// };
// askTodayStatus = function(response, convo) {
// convo.ask("What do you plan to do today?", function(response, convo) {
// convo.say("Ok. Sounds Great!")
// askIssues(response, convo)
// convo.next();
// }, {'key': 'today'});
// };
// askIssues = function(response, convo) {
// convo.ask("Any impediments or blocking issues??", function(response, convo) {
// convo.say("Ok! Thank you :simple_smile: ");
// convo.next();
// }, {'key': 'issues'});
// };
// beepboop.on('add_resource', function (msg) {
// console.log('received request to add bot to team')
// });
// // Send the user who added the bot to their team a welcome message the first time it's connected
// beepboop.on('botkit.rtm.started', function (bot, resource, meta) {
// var slackUserId = resource.SlackUserID
// if (meta.isNew && slackUserId) {
// bot.api.im.open({ user: slackUserId }, function (err, response) {
// if (err) {
// return console.log("im.open error:",err)
// }
// var dmChannel = response.channel.id
// bot.say({channel: dmChannel, text: 'Thanks for adding me to your team!'})
// bot.say({channel: dmChannel, text: 'Just /invite me to a channel!'})
// })
// }
// });
controller.on('bot_channel_join', function (bot, message) {
console.log("bot_channel_join")
bot.reply(message, "I'm here!")
});
// controller.hears(['hello', 'hi'], 'direct_message,direct_mention,mention', function(bot, message) {
// bot.api.reactions.add({
// timestamp: message.ts,
// channel: message.channel,
// name: 'robot_face',
// }, function(err, res) {
// if (err) {
// bot.botkit.log('Failed to add emoji reaction :(', err);
// }
// });
// controller.storage.users.get(message.user, function(err, user) {
// if (user && user.name) {
// bot.reply(message, 'Hello ' + user.name + '!!');
// } else {
// bot.reply(message, 'Hello.');
// }
// });
// });
// controller.hears(['call me (.*)', 'my name is (.*)'], 'direct_message,direct_mention,mention', function(bot, message) {
// var name = message.match[1];
// controller.storage.users.get(message.user, function(err, user) {
// if (!user) {
// user = {
// id: message.user,
// };
// }
// user.name = name;
// controller.storage.users.save(user, function(err, id) {
// bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.');
// });
// });
// });
// controller.hears(['what is my name', 'who am i'], 'direct_message,direct_mention,mention', function(bot, message) {
// controller.storage.users.get(message.user, function(err, user) {
// if (user && user.name) {
// bot.reply(message, 'Your name is ' + user.name);
// } else {
// bot.startConversation(message, function(err, convo) {
// if (!err) {
// convo.say('I do not know your name yet!');
// convo.ask('What should I call you?', function(response, convo) {
// convo.ask('You want me to call you `' + response.text + '`?', [
// {
// pattern: 'yes',
// callback: function(response, convo) {
// // since no further messages are queued after this,
// // the conversation will end naturally with status == 'completed'
// convo.next();
// }
// },
// {
// pattern: 'no',
// callback: function(response, convo) {
// // stop the conversation. this will cause it to end with status == 'stopped'
// convo.stop();
// }
// },
// {
// default: true,
// callback: function(response, convo) {
// convo.repeat();
// convo.next();
// }
// }
// ]);
// convo.next();
// }, {'key': 'nickname'}); // store the results in a field called nickname
// convo.on('end', function(convo) {
// if (convo.status == 'completed') {
// bot.reply(message, 'OK! I will update my dossier...');
// controller.storage.users.get(message.user, function(err, user) {
// if (!user) {
// user = {
// id: message.user,
// };
// }
// user.name = convo.extractResponse('nickname');
// controller.storage.users.save(user, function(err, id) {
// bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.');
// });
// });
// } else {
// // this happens if the conversation ended prematurely for some reason
// bot.reply(message, 'OK, nevermind!');
// }
// });
// }
// });
// }
// });
// });
// controller.hears(['identify yourself', 'who are you', 'what is your name'],
// 'direct_message,direct_mention,mention', function(bot, message) {
// var hostname = os.hostname();
// var uptime = formatUptime(process.uptime());
// bot.reply(message,
// ':robot_face: I am a bot named <@' + bot.identity.name +
// '>. I have been running for ' + uptime + ' on ' + hostname + '.' +
// '\n I have been created by Mr. Ojas Gosar');
// });
// function formatUptime(uptime) {
// var unit = 'second';
// if (uptime > 60) {
// uptime = uptime / 60;
// unit = 'minute';
// }
// if (uptime > 60) {
// uptime = uptime / 60;
// unit = 'hour';
// }
// if (uptime != 1) {
// unit = unit + 's';
// }
// uptime = uptime + ' ' + unit;
// return uptime;
// }