-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
1534 lines (1400 loc) · 53.1 KB
/
bot.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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const snoozeAmountReminderTrigger = 5;
const database = require('./connectors/database');
const rewards = require('./generate-reward');
const Time = require('./time');
/**
* Creates a quick reply.
*
* @param message String to send.
* @param options ['reply1', 'reply2'] Array of buttons.
* @returns {{text: *, quick_replies: Array}}
*/
const createQuickReply = (message, options) => {
const replies = [];
options.forEach(el => {
replies.push(
createQRItem(
el,
'PICKED_' + el.toUpperCase().split(' ').join('_')
)
);
});
return {
text: message,
quick_replies: replies
};
};
/**
* Builds a quick reply item to work within a quick reply, usage:
{
text: 'Choose these options',
quick_replies: [
createQRItem('Hi', 'PICKED_GREETING_MESSAGE')
]
}
*/
const createQRItem = (text, payload) => {
return {
content_type: 'text',
title: text,
payload: payload
};
};
/**
* Convert to Pascal Case.
*
* @param str Unfriendly string.
* @returns {string}
*/
const convertToFriendlyName = str => {
if (str === undefined || str === null) {
return '';
}
return str.split('_').join(' ').toLowerCase();
};
/**
* Build the Settings message and send it to the user.
*/
function displaySettings(user, sender, reply, debug) {
const usr = user;
if (debug) {
delete usr.survey1a;
delete usr.survey1b;
delete usr.survey1c;
delete usr.survey1d;
delete usr.survey2a;
delete usr.survey2b;
delete usr.survey2c;
delete usr.survey2d;
delete usr.survey2h;
delete usr.survey2i;
delete usr.survey2j;
delete usr.survey2k;
delete usr.survey2l;
delete usr.surveyModality1a;
delete usr.surveyModality1b;
delete usr.surveyModality1c;
delete usr.surveyModality1d;
reply(sender,
{
text: (JSON.stringify(usr)).substring(0, 640) // Trim output to max message
},
{
text: (JSON.stringify(Time.reminderTimes)).substring(0, 640)
}
);
} else {
reply(sender,
{
text: 'Your reminder time is set to ' + convertToFriendlyName(user.reminderTime) + '.'
}
);
}
}
/**
* Build the Help message and send it to the user.
*/
function displayHelp(showDontKnow, sender, reply) {
database.getGlobals(globals => {
let firstMsg = 'There are ' + globals.remainingDays + ' days remaining in the study.';
const secondMsg = 'If you have any questions email [email protected]';
if (globals.remainingDays === 0) {
firstMsg = 'This is the last day of the study.';
}
if (globals.remainingDays < 0) {
firstMsg = 'The study has ended.';
}
const sorryMsg = 'Sorry, I don\'t know how to respond to that. This is everything I have...';
const qr = createQuickReply(
'Here is the list of commands you can message me.',
[
'About',
'Help'
]
);
if (showDontKnow) {
reply(sender,{text: sorryMsg},{text: firstMsg},{text: secondMsg},qr);
} else {
reply(sender,{text: firstMsg},{text:secondMsg}, qr);
}
});
}
/**
* Build the About message and send it to the user.
*/
function displayAbout(sender, reply) {
database.getGlobals(globals => {
reply(sender,
{
text: 'Harry\'s Habits is a chatbot to help you form new healthy habits. It is part of a study that looks at habit formation.'
},
{
text: 'The study lasts for 1 month, with 3 weeks of chatbot interaction followed by 1-week without, finishing with a survey.'
},
{
text: 'It\'s conducted by the University of Bristol and approved by their ethics committee (reference id of 54701).'
},
{
text: 'If you would like to quit at any time, press the Manage button at the top of the screen, then Manage Messages to block all communication.'
},
{
text: 'For more information visit www.harrymt.com/harryshabits'
}
);
});
}
/**
* Build the GetStarted message and send it to the user.
*/
function displayGetStarted(sender, reply) {
reply(sender,
{
text: 'Welcome to Harry\'s Habits! Thank you for taking part in the study!'
},
{
text: 'Before we start, can you tell me a bit more about yourself? (This data cannot be used to identify you and will not be shared). What is your gender?',
quick_replies: [
createQRItem('Male', 'PICKED_GENDER_MALE'),
createQRItem('Female', 'PICKED_GENDER_FEMALE'),
createQRItem('Non-Binary', 'PICKED_GENDER_NON_BINARY'),
createQRItem('Don\'t Say', 'PICKED_GENDER_DONT_SAY')
]
}
);
}
/**
* Build the HowOld message and send it to the user.
*/
function displayHowOld(user, sender, reply) {
user.expectingAge = true;
database.updateUser(user, () => {
reply(sender,
{
text: 'Thank you. How old are you?'
}
);
});
}
/**
* Build the UsedHabitAppsBefore message and send it to the user.
*/
function displayUsedHabitAppsBefore(sender, reply) {
reply(sender,
{
text: 'Thanks, have you used any habit tracking apps or systems before?',
quick_replies: [
createQRItem('Yes', 'PICKED_HABIT_APPS_YES'),
createQRItem('No', 'PICKED_HABIT_APPS_NO')
]
}
);
}
/**
* Build the WhatHabitsToDevelop message and send it to the user.
*/
function displayWhatHabitsToDevelop(user, sender, reply) {
user.expectingPreviousHabits = true;
database.updateUser(user, () => {
reply(sender,
{
text: 'Cool, what did you track before?'
}
);
});
}
/**
* Build the DidTheyWork message and send it to the user.
*/
function displayDidTheyWork(sender, reply) {
reply(sender,
{
text: 'Okay, did they work?',
quick_replies: [
createQRItem('Yes', 'PICKED_PREVIOUS_HABIT_APPS_DID_WORK_YES'),
createQRItem('No', 'PICKED_PREVIOUS_HABIT_APPS_DID_WORK_NO')
]
}
);
}
/**
* Build the PickHabit message and send it to the user.
*/
function displayPickHabit(sender, reply) {
reply(sender,
{
text: 'Brill! Now onto the fun part. What type of habit would you like to complete?',
quick_replies: [
createQRItem('Type: Exercise', 'PICKED_HABIT_CATEGORY_PHYSICAL'),
createQRItem('Type: Relaxation', 'PICKED_HABIT_CATEGORY_RELAXATION')
]
}
);
}
/**
* Build the PhysicalHabits message and send it to the user.
*/
function displayPhysicalHabits(sender, reply) {
reply(sender,
{
text: 'What specific habit would you like to pick?',
quick_replies: [
createQRItem('Stretching', 'PICKED_HABIT_STRETCH'),
createQRItem('Press Ups', 'PICKED_HABIT_PRESS_UPS'),
createQRItem('Plank', 'PICKED_HABIT_PLANK'),
createQRItem('Different Type', 'PICKED_SHOW_HABITS_LIST')
]
}
);
}
/**
* Build the RelaxationHabits message and send it to the user.
*/
function displayRelaxationHabits(sender, reply) {
reply(sender,
{
text: 'What specific habit would you like to pick?',
quick_replies: [
createQRItem('Reading', 'PICKED_HABIT_READING'),
createQRItem('Writing', 'PICKED_HABIT_WRITING'),
createQRItem('Meditation', 'PICKED_HABIT_MEDITATION'),
createQRItem('Different Type', 'PICKED_SHOW_HABITS_LIST')
]
}
);
}
/**
* Build the ReminderTime message and send it to the user.
*/
function displayReminderTime(habit, sender, reply) {
let habitInstructions = 'Okay, try and';
if (habit === 'PLANK') {
habitInstructions += ' hold your plank for at least 30 seconds.';
} else if (habit === 'PRESS_UPS') {
habitInstructions += ' complete at least 10 press-ups.';
} else if (habit === 'STRETCH') {
habitInstructions += ' stretch for at least 1 minute.';
} else if (habit === 'READING') {
habitInstructions += ' read for at least 5 minutes.';
} else if (habit === 'WRITING') {
habitInstructions += ' write for at least 5 minutes';
} else if (habit === 'MEDITATION') {
habitInstructions += ' close your eyes and take deep breaths for at least 5 minutes.';
} else {
habitInstructions = 'Great!';
}
reply(sender,
{
text: habitInstructions
},
{
text: 'I am going to check every day to see how you\'re getting on with your habit.'
},
createQuickReply(
'What time would you like me to message you?',
[
'Morning',
'Afternoon',
'Evening'
]
)
);
}
/**
* Build the NestedTime message and send it to the user.
*/
function displayNestedTime(timePeriod, sender, reply) {
reply(sender,
createQuickReply(
'What time in the ' + timePeriod + '?',
[
'Early ' + timePeriod,
'Mid ' + timePeriod,
'Late ' + timePeriod
]
)
);
}
/**
* Build the ExistingRoutine message and send it to the user.
*/
function displayExistingRoutine(habit, time, user, sender, reply) {
let existingRoutines = [];
if (time === 'MORNING') {
existingRoutines = [
'waking up',
'eating breakfast',
'arriving at work'
];
} else if (time === 'AFTERNOON') {
existingRoutines = [
'eating lunch',
'leaving work'
];
} else {
existingRoutines = [
'leaving work',
'eating dinner',
'getting ready for bed'
];
}
let message = 'Habits are better formed when part of your existing daily routine. For example ' + convertToFriendlyName(habit) + ' after ';
for (let i = 0; i < existingRoutines.length; i++) {
if ((i + 1) === existingRoutines.length) {
message += 'or ' + existingRoutines[i] + '. ';
} else {
message += existingRoutines[i] + ', ';
}
}
message += 'Please enter one of these contexts (e.g. \'' + existingRoutines[0] + '\') or enter your own below:';
user.expectingHabitContext = true;
database.updateUser(user, () => {
reply(sender,
{
text: message
}
);
});
}
/**
* Build the Interview message and send it to the user.
*/
function displayInterview(sender, reply) {
reply(sender,
{
text: 'At the end of the study, I\'d like to interview you to see how you got on. Would you be interested in this?',
quick_replies: [
createQRItem('Yes', 'PICKED_INTERVIEW_YES'),
createQRItem('No', 'PICKED_INTERVIEW_NO')
]
}
);
}
/**
* Build the ContactDetails message and send it to the user.
*/
function displayContactDetails(user, sender, reply) {
user.expectingContactDetails = true;
database.updateUser(user, () => {
reply(sender,
{
text: 'Thanks! What\'s your email? I\'ll use it to contact you to arrange the interview [nothing else]'
}
);
});
}
/**
* Build the FinalStage message and send it to the user.
*/
function displayFinalStage(habit, time, sender, reply) {
reply(sender,
{
text: 'All set up! I will check if you have completed your ' + convertToFriendlyName(habit) + ' tomorrow around ' + convertToFriendlyName(time) + '!'
},
{
text: 'If you would like to quit at any time, press the Manage button at the top of the screen, then Manage Messages to block all communication.'
},
{
text: 'Catch you tomorrow!'
},
{
text: '(you can close messenger now)'
}
);
}
/**
* Build the Survey1b message and send it to the user.
*/
function displaySurvey1b(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something I do without having to consciously remember',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY1_B_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY1_B_AGREE'),
createQRItem('Neither', 'SURVEY1_B_NEITHER'),
createQRItem('Disagree', 'SURVEY1_B_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY1_B_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey1c message and send it to the user.
*/
function displaySurvey1c(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something I do without thinking',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY1_C_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY1_C_AGREE'),
createQRItem('Neither', 'SURVEY1_C_NEITHER'),
createQRItem('Disagree', 'SURVEY1_C_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY1_C_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey1d message and send it to the user.
*/
function displaySurvey1d(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something I start doing before I realise I\'m doing it',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY1_D_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY1_D_AGREE'),
createQRItem('Neither', 'SURVEY1_D_NEITHER'),
createQRItem('Disagree', 'SURVEY1_D_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY1_D_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the ModalityQuestion1a message and send it to the user.
*/
function displayModalityQuestion1a(sender, reply) {
reply(sender,
{
text: 'The rewards helped me form my new habit',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY1_MODALITY_A_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY1_MODALITY_A_AGREE'),
createQRItem('Neither', 'SURVEY1_MODALITY_A_NEITHER'),
createQRItem('Disagree', 'SURVEY1_MODALITY_A_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY1_MODALITY_A_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the ModalityQuestion1b message and send it to the user.
*/
function displayModalityQuestion1b(sender, reply) {
reply(sender,
{
text: 'I enjoyed my rewards',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY1_MODALITY_B_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY1_MODALITY_B_AGREE'),
createQRItem('Neither', 'SURVEY1_MODALITY_B_NEITHER'),
createQRItem('Disagree', 'SURVEY1_MODALITY_B_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY1_MODALITY_B_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the ModalityQuestion1c message and send it to the user.
*/
function displayModalityQuestion1c(sender, reply) {
reply(sender,
{
text: 'I found my rewards annoying',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY1_MODALITY_C_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY1_MODALITY_C_AGREE'),
createQRItem('Neither', 'SURVEY1_MODALITY_C_NEITHER'),
createQRItem('Disagree', 'SURVEY1_MODALITY_C_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY1_MODALITY_C_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the ModalityQuestion1d message and send it to the user.
*/
function displayModalityQuestion1d(sender, reply) {
reply(sender,
{
text: 'I looked forward to my rewards',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY1_MODALITY_D_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY1_MODALITY_D_AGREE'),
createQRItem('Neither', 'SURVEY1_MODALITY_D_NEITHER'),
createQRItem('Disagree', 'SURVEY1_MODALITY_D_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY1_MODALITY_D_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the AnyMoreFeedback message and send it to the user.
*/
function displayAnyMoreFeedback(user, sender, reply) {
reply(sender,
{
text: 'Thank you! Do you have any other comments or any other feedback you would like to share?',
quick_replies: [
createQRItem('Yes', 'PICKED_YES_MORE_FEEDBACK'),
createQRItem('No', 'PICKED_NO_MORE_FEEDBACK')
]
}
);
}
/**
* Build the TakeFeedback message and send it to the user.
*/
function displayTakeFeedback(user, sender, reply) {
user.expectingMoreFeedback = true;
database.updateUser(user, () => {
reply(sender,
{
text: 'Type your comments below'
}
);
});
}
/**
* Build the Survey2b message and send it to the user.
*/
function displaySurvey2b(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something I do automatically',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY2_B_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY2_B_AGREE'),
createQRItem('Neither', 'SURVEY2_B_NEITHER'),
createQRItem('Disagree', 'SURVEY2_B_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY2_B_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey2c message and send it to the user.
*/
function displaySurvey2c(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something I do without having to consciously remember',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY2_C_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY2_C_AGREE'),
createQRItem('Neither', 'SURVEY2_C_NEITHER'),
createQRItem('Disagree', 'SURVEY2_C_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY2_C_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey2d message and send it to the user.
*/
function displaySurvey2d(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something that makes me feel weird if I do not do it',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY2_D_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY2_D_AGREE'),
createQRItem('Neither', 'SURVEY2_D_NEITHER'),
createQRItem('Disagree', 'SURVEY2_D_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY2_D_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey2e message and send it to the user.
*/
function displaySurvey2e(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something I do without thinking',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY2_E_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY2_E_AGREE'),
createQRItem('Neither', 'SURVEY2_E_NEITHER'),
createQRItem('Disagree', 'SURVEY2_E_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY2_E_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey2f message and send it to the user.
*/
function displaySurvey2f(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something that would require effort not to do it',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY2_F_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY2_F_AGREE'),
createQRItem('Neither', 'SURVEY2_F_NEITHER'),
createQRItem('Disagree', 'SURVEY2_F_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY2_F_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey2g message and send it to the user.
*/
function displaySurvey2g(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something that belongs to my daily routine',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY2_G_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY2_G_AGREE'),
createQRItem('Neither', 'SURVEY2_G_NEITHER'),
createQRItem('Disagree', 'SURVEY2_G_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY2_G_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey2h message and send it to the user.
*/
function displaySurvey2h(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something I start doing before I realize I\'m doing it',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY2_H_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY2_H_AGREE'),
createQRItem('Neither', 'SURVEY2_H_NEITHER'),
createQRItem('Disagree', 'SURVEY2_H_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY2_H_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey2i message and send it to the user.
*/
function displaySurvey2i(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something I would find hard not to do',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY2_I_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY2_I_AGREE'),
createQRItem('Neither', 'SURVEY2_I_NEITHER'),
createQRItem('Disagree', 'SURVEY2_I_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY2_I_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey2j message and send it to the user.
*/
function displaySurvey2j(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something I have no need to think about doing',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY2_J_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY2_J_AGREE'),
createQRItem('Neither', 'SURVEY2_J_NEITHER'),
createQRItem('Disagree', 'SURVEY2_J_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY2_J_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey2k message and send it to the user.
*/
function displaySurvey2k(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something that is typically \'me\'',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY2_K_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY2_K_AGREE'),
createQRItem('Neither', 'SURVEY2_K_NEITHER'),
createQRItem('Disagree', 'SURVEY2_K_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY2_K_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the Survey2l message and send it to the user.
*/
function displaySurvey2l(habit, context, sender, reply) {
reply(sender,
{
text: convertToFriendlyName(habit) + ' after ' + convertToFriendlyName(context) + ' is something I have been doing for a long time',
quick_replies: [
createQRItem('Strongly agree', 'SURVEY2_L_STRONGLY_AGREE'),
createQRItem('Agree', 'SURVEY2_L_AGREE'),
createQRItem('Neither', 'SURVEY2_L_NEITHER'),
createQRItem('Disagree', 'SURVEY2_L_DISAGREE'),
createQRItem('Strongly disagree', 'SURVEY2_L_STRONGLY_DISAGREE')
]
}
);
}
/**
* Build the FinalFinalMessage message and send it to the user.
*/
function displayFinalFinalMessage(user, sender, reply) {
reply(sender,
{
text: 'Thank you for participating in the study!'
},
{
text: 'If you have any more comments or questions email [email protected]'
},
{
text: 'Goodbye! 👋'
}
);
}
/**
* Build the EndOfBotPeriod message and send it to the user.
*/
function displayEndOfBotPeriod(user, sender, reply) {
const msgA = 'Thank you for your time! You will be unable to use me to track habits anymore.';
const msgB = 'If you have any further questions about the study, please contact [email protected].';
const msgGoodbye = 'Goodbye! 👋';
user.finished = true;
database.updateUser(user, () => {
if (user.interview) {
reply(sender,{text: msgA},{text: msgB},{text: 'I will contact you in about a week to see how you\'re getting on with your habit.'},{text: msgGoodbye});
} else {
reply(sender,{text: msgA},{text: msgB},{text: msgGoodbye});
}
});
}
/**
* General function to read an incoming message and decide how to respond.
* The read function will check if the message is just a string or a quick reply object.
* It will then perform some database changes and then return a message back
* to the user.
*/
const read = function (sender, message, reply) {
// Let's find the user object
database.find(sender, user => {
if (user.finished) {
reply(sender,
{
text: 'The study is now over, please contact [email protected] if you have any further questions.'
}
);
return;
}
let firstTime = false;
// If we have seen this user before, marked them as seen
if (!user.seenBefore) {
user.seenBefore = true;
}
// If user hasnt finish setting up, don't let them mark as completed.
if (!user.habit || !user.modality || !user.reminderTime) {
firstTime = true;
}
console.log(message);
if (message.quick_reply === undefined) {
if (message.text && user.expectingAge) {
user.expectingAge = false;
user.age = message.text.split("'").join("").split("\"").join("");
// Save user information to datastore
database.updateUser(user, () => {
displayUsedHabitAppsBefore(sender, reply);
});
} else if (message.text && user.expectingPreviousHabits) {
user.expectingPreviousHabits = false;
user.previousHabits = message.text.split("'").join("").split("\"").join("");
// Save user information to datastore
database.updateUser(user, () => {
displayDidTheyWork(sender, reply);
});
} else if (message.text && user.expectingHabitContext) {
user.expectingHabitContext = false;
user.habitContext = message.text.split("'").join("").split("\"").join("");
// Auto assign users a modality.
autoAssignModality(mode => {
user.modality = mode;
// Save user information to datastore
database.updateUser(user, () => {
displayInterview(sender, reply);
});
});
} else if (message.text && user.expectingContactDetails) {
user.expectingContactDetails = false;
user.email = message.text.split("'").join("").split("\"").join("");
// Save user information to datastore
database.updateUser(user, () => {
displayFinalStage(user.habit, user.reminderTime, sender, reply);
});
} else if (message.text && user.expectingMoreFeedback) {
user.expectingMoreFeedback = false;
user.moreFeedback = message.text.split("'").join("").split("\"").join("");
// Save user information to datastore
database.updateUser(user, () => {
displayEndOfBotPeriod(user, sender, reply);
});
// } else if (message.text && message.text.toLowerCase() === 'settings') {
// displaySettings(user, sender, reply);
} else if (message.text && (message.text.toLowerCase() === 'help')) {
displayHelp(false, sender, reply);
} else if (message.text && (message.text.toLowerCase() === 'about')) {
displayAbout(sender, reply);
} else if (message.text && (message.text.toLowerCase() === 'harrymt')) {
displaySettings(user, sender, reply, true);
} else {
if (firstTime) {
displayGetStarted(sender, reply);
} else if (message.sticker_id ||
message.text.toLowerCase() === 'bye' ||
message.text.toLowerCase() === 'thanks' ||
message.text.toLowerCase() === 'thank you' ||
message.text.toLowerCase() === 'sweet' ||
message.text.toLowerCase() === 'great' ||
message.text.toLowerCase() === 'k' ||
message.text.toLowerCase() === 'ok' ||
message.text.toLowerCase() === 'okay' ||
message.text.toLowerCase() === 'ty' ||
message.text.toLowerCase() === 'good' ||
message.text.toLowerCase() === 'cool' ||
message.text.toLowerCase() === 'awesome') {
// If user sends a sticker to no response, reply with a thumb
reply(sender, { text: '👍' });
} else if (message.text.toLowerCase() === 'sex' || message.text.toLowerCase() === 'dick') {
// If user sends a sticker to no response, reply with a thumb
reply(sender, { text: 'I\'m not that kind of bot...' });
} else {7
// If users are trying to tell us to mark thier habit as completed, then issue the completed dialog
// if (message.text && (String(message.text.toLowerCase()).includes('track') ||
// String(message.text.toLowerCase()).includes('mark') ||
// String(message.text.toLowerCase()).includes('habit') ||
// String(message.text.toLowerCase()).includes('complete') ||
// String(message.text.toLowerCase()).includes('did it'))) {
// // Check if users have already completed their habit today
// database.hasUserCompletedHabit(user, hasCompleted => {
// if (!hasCompleted) {
// reply(sender,
// createQuickReply(
// 'Did you want to mark your daily habit ' + convertToFriendlyName(user.habit) + ' as completed?',
// [
// 'Completed Habit'
// ]
// )
// );
// } else {
// reply(sender, { text: 'Well done on completing your habit today. I\'ll see you tomorrow!' });
// }
// });
// } else {
displayHelp(true, sender, reply);
// }
}
}
} else {
if (message.quick_reply.payload === 'GET_STARTED_PAYLOAD') {
displayGetStarted(sender, reply);
} else if (message.quick_reply.payload === 'PICKED_ABOUT') {
displayAbout(sender, reply);
// } else if (message.quick_reply.payload === 'PICKED_SETTINGS') {
// displaySettings(user, sender, reply);
} else if (message.quick_reply.payload === 'PICKED_HELP') {
displayHelp(false, sender, reply);
} else if (message.quick_reply.payload === 'PICKED_GENDER_MALE' ||
message.quick_reply.payload === 'PICKED_GENDER_FEMALE' ||
message.quick_reply.payload === 'PICKED_GENDER_NON_BINARY' ||
message.quick_reply.payload === 'PICKED_GENDER_DONT_SAY') {
user.gender = message.quick_reply.payload.substring(14);
database.updateUser(user, () => {
displayHowOld(user, sender, reply);
});
} else if (message.quick_reply.payload === 'PICKED_HABIT_APPS_YES') {
user.hasUsedHabitAppsBefore = true;
database.updateUser(user, () => {
displayWhatHabitsToDevelop(user, sender, reply);
});
} else if (message.quick_reply.payload === 'PICKED_HABIT_APPS_NO') {
user.hasUsedHabitAppsBefore = false;
database.updateUser(user, () => {
displayPickHabit(sender, reply);
});
} else if (message.quick_reply.payload === 'PICKED_PREVIOUS_HABIT_APPS_DID_WORK_YES') {
user.hasUsedHabitAppsBeforeWorked = true;
database.updateUser(user, () => {
displayPickHabit(sender, reply);
});
} else if (message.quick_reply.payload === 'PICKED_PREVIOUS_HABIT_APPS_DID_WORK_NO') {
user.hasUsedHabitAppsBeforeWorked = false;
database.updateUser(user, () => {