-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar_lambda_2.js
1341 lines (1180 loc) · 50.4 KB
/
grammar_lambda_2.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
const Alexa = require('ask-sdk-core');
const grammar_a1 = require('./content');
const quizes = require('./quiz');
const langs = require('./languages');
const i18n = require('i18next');
const sprintf = require('i18next-sprintf-postprocessor');
const languageString = {
en: {
translation: {
CHAPTERS_NAMES: grammar_a1.EN_US.CHAPTERS_NAMES,
CHAPTERS_NAMES_JSON: grammar_a1.EN_US.CHAPTERS_NAMES_JSON,
SUBJECT_PRONOUNS: grammar_a1.EN_US.SUBJECT_PRONOUNS,
SUBJECT_PRONOUNS_QUIZ: quizes.EN_US.SUBJECT_PRONOUNS,
TOBE_AFIRMATIVE: grammar_a1.EN_US.TOBE_AFIRMATIVE,
TOBE_AFIRMATIVE_QUIZ: quizes.EN_US.TOBE_AFIRMATIVE,
TOBE_NEGATIVE: grammar_a1.EN_US.TOBE_NEGATIVE,
TOBE_NEGATIVE_QUIZ: quizes.EN_US.TOBE_NEGATIVE,
TOBE_INTEROCATIVE: grammar_a1.EN_US.TOBE_INTEROCATIVE,
TOBE_INTEROCATIVE_QUIZ: quizes.EN_US.TOBE_INTEROCATIVE,
CARDINAL_NUMBERS: grammar_a1.EN_US.CARDINAL_NUMBERS,
CARDINAL_NUMBERS_QUIZ: quizes.EN_US.CARDINAL_NUMBERS,
IMAGE_URLS: grammar_a1.EN_US.URLS,
LANGUAGE: 'English',
PLEASE_STATE: 'Please state the correct number. ',
QUIZ_COMPLETED: 'Finished the quiz. Final score: %s',
QUIZ_CORRECT_ANSWER: 'Congrats. Correct-o. ',
QUIZ_WRONG_ANSWER: 'Boo hoo. That was wrong. Correct answer is: %s. ',
QUIZ_INVALID_ANSWER: 'Well you clearly didn\'t hear the instructions then. ',
QUIZ_DONTKNOW: 'Well you have to learn it. ',
QUIZ_QUIT: 'Give up like life has given up on you. Final score: %s',
WELCOME_MESSAGE: 'Welcome to %s. Let\'s learn some english gramatical rules. ',
SKILL_NAME: 'English Grammar ',
EXTRA_INFO_CHAPTER: 'You can say basic info to get the introductory info, more info to get more details or examples to listen to some examples. ',
HELP_START: 'Please state the grammatical rule you want to review',
CHAPTER_INFO: 'Please state which of the following sections of grammatical rule %s you want to access. ',
OPTION_START_HELP: 'You can say index to get the full list of grammatical rules you want to access. ',
OPTION_START_CHAPTERS: 'Simply say one of the following grammatical rules to access one. ',
OPTION_UNRECOGNISED: 'This command is unrecognised by the English Grammar skill. Please say something else ',
OPTION_CHAPTER_HELP: 'You can say basic info to get the introductory info, more info to get more details or examples to listen to some examples. ',
OPTION_START_QUIZ: 'Simply say the grammatical rule title and quiz. ',
OPTIONS_QUIZ: 'Say the number of your correct answer, skip to skip the question and end to quit the quiz. ',
NO_CHAPTER: 'No grammatical rule is selected. ',
GRAMATICAL_OPTIONS: '%s Say start quiz to do an exercise. Or say a different rule to study. ',
WRONG_INPUT: 'This command is not useful here. ',
START_INTENT_OPTIONS: 'Say the grammatical rule you want to access. Say show me grammatical rule to get the list of the grammatical rule ',
QUIT_MESSAGE: 'Thank you for using English Grammar. See you around ',
NO_ACCESS_TO_THIS: 'You don\'t have access to this information, please give more details ',
GENERAL_OPTIONS: 'You can say help to get help on the application, index to see the list of grammatical rules, the grammatical rule name or quiz to do a quiz exercise. ',
FALLBACK_MESSSAGE: 'The %s skill can\'t help you with that. What can I help you with? ',
FALLBACK_REPROMPT: 'What can I help you with? ',
BASIC_INFO: 'Basic info for: ',
MORE_INFO: 'More info for: ',
EXAMPLE_INFO: 'Example for: ',
QUIZ_QUESTION: 'Question',
TOTALLY_WRONG_INPUT: 'That\'s just jibber-jabber. It doesn\'t make any sense. ',
SORRY_SAY_AGAIN: 'Sorry, I can\'t understand the command. Please try saying again in %s....',
NO_PREVIOUS_INFO: 'How can i access the next if no previous exists. ',
HELP_NEEDED: 'Try saying help to see the next available commands. '
},
},
'es-ES': {
translation: {
CHAPTERS_NAMES: grammar_a1.ES_ES.CHAPTERS_NAMES,
CHAPTERS_NAMES_JSON: grammar_a1.ES_ES.CHAPTERS_NAMES_JSON,
SUBJECT_PRONOUNS: grammar_a1.ES_ES.SUBJECT_PRONOUNS,
SUBJECT_PRONOUNS_QUIZ: quizes.ES_ES.SUBJECT_PRONOUNS,
TOBE_AFIRMATIVE: grammar_a1.ES_ES.TOBE_AFIRMATIVE,
TOBE_AFIRMATIVE_QUIZ: quizes.ES_ES.TOBE_AFIRMATIVE,
TOBE_NEGATIVE: grammar_a1.ES_ES.TOBE_NEGATIVE,
TOBE_NEGATIVE_QUIZ: quizes.ES_ES.TOBE_NEGATIVE,
TOBE_INTEROCATIVE: grammar_a1.ES_ES.TOBE_INTEROCATIVE,
TOBE_INTEROCATIVE_QUIZ: quizes.ES_ES.TOBE_INTEROCATIVE,
CARDINAL_NUMBERS: grammar_a1.ES_ES.CARDINAL_NUMBERS,
CARDINAL_NUMBERS_QUIZ: quizes.ES_ES.CARDINAL_NUMBERS,
IMAGE_URLS: grammar_a1.ES_ES.URLS,
LANGUAGE_OPTIONS: langs.OPTIONS,
LANGUAGE_MORE_OPTIONS: langs.MORE_OPTIONS,
LANGUAGE_CHAPTERS: langs.CHAPTERS,
lANGUAGE_REPEAT: langs.REPEAT,
LANGUAGE: 'Castellano',
WELCOME_MESSAGE: 'Bienvenido a %s. Aprendamos algo. ',
GENERAL_OPTIONS: 'Puedes decir, ayuda, para obtener la ayuda de la aplicación. O, Índice, para escuchar la lista de normas gramaticales disponibles. También puedes decir el nombre de la norma gramatical o ejercicios para practicar lo aprendido. ', //this one
HELP_START: 'Por favor, indique la norma gramatical que desearía revisar ',
TOTALLY_WRONG_INPUT: 'Eso son simplemente balbuceos. No tiene ningún sentido para mí. ',
FALLBACK_MESSSAGE: 'La %s skill no puede ayudarte con eso. ¿Con qué podría yo ayudarte? ',
FALLBACK_REPROMPT: '¿Con qué podría yo ayudarte? ',
HELP_NEEDED: 'Intenta pidiendo ayuda para ver los comando disponibles. ',
OPTION_START_HELP: 'Puedes decir, índice, para obtener la lista de las normas gramaticales. También puedes decir el nombre de la norma gramatical o ejercicios para practicar lo aprendido. ',
OPTION_START_CHAPTERS: 'Simplemente dí una de las siguientes normas gramaticales para poder acceder: ',
OPTION_UNRECOGNISED: 'Este comando no es reconocible por esta aplicación. Por favor, pruebe con algo diferente.',
OPTION_CHAPTER_HELP: '¿Quíeres que continuemos? di, más información, o, ejemplos. ',
BASIC_INFO: 'Aquí viene: ',
EXTRA_INFO_CHAPTER: 'Puedes decir más informacíon para obtener más detalles, o ejemplos para obtener una lista de ejemplos de la norma gramatical. ', //this one
GRAMATICAL_OPTIONS: ' %s para hacer un ejercicio di, ejercicio. Para seguir estudiando, di el nombre de una norma gramatical.',
OPTION_START_QUIZ: 'Simplemente di el título de la norma gramatical, seguido de la palabra, ejercicios',
OPTIONS_QUIZ: 'Dí el número de la respuesta correcta, saltar para saltar el ejercicio y salir para terminar con los ejercicios. ',
PLEASE_STATE: 'Por favor, indique el número correcto. ',
QUIZ_COMPLETED: 'Termine el ejercicio. Su puntuación ha sido de: %s puntos ',
QUIZ_CORRECT_ANSWER: 'Felicitaciones. ',
QUIZ_WRONG_ANSWER: 'Espera un momento. Eso ha sido incorrecto. La respuesta correcta es: %s. ',
QUIZ_INVALID_ANSWER: 'Creo que no escuchaste correctamente las insgtrucciones. ',
QUIZ_DONTKNOW: 'Parece que tienes que aprenderlo. ',
QUIZ_QUIT: 'No creo que tengas otra opcion más que rendirte. Puntuación final: %s ',
SKILL_NAME: 'Gramática inglesa ',
CHAPTER_INFO: 'Por favor, indique a cual de las siguietnes secciones de la norma gramatical sobre %s quieres acceder. ',
NO_CHAPTER: 'No se ha seleccionado ninguna norma gramatical. ',
WRONG_INPUT: 'Esta instruccion no es útil aquí. ',
START_INTENT_OPTIONS: 'Dí el título de la norma gramatical a la que le gustaría acceder. Dí, mostrarme la norma gramatical para obtener la lista de las normas gramaticales. ',
QUIT_MESSAGE: 'Gracias por utilizar la gramatica inglesa de Books for Languages. Nos vemos pronto. ',
NO_ACCESS_TO_THIS: 'Parece que no tienes acceso a esta información, por favor, dame más detalles. ',
MORE_INFO: 'Más información para: ',
EXAMPLE_INFO: 'Ejemplos de: ',
QUIZ_QUESTION: 'Pregunta ',
SORRY_SAY_AGAIN: 'Lo siento, no puedo entender su instrucción. Podria volver a probar en %s.... ',
NO_PREVIOUS_INFO: 'Como puedo ahcceder al próximo si no existe el anterior?. '
},
},
};
/**
* INTENTIONS
*/
const EndQuizIntent = {
canHandle(handlerInput)
{
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'EndQuizIntent'
},
handle(handlerInput)
{
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
let valid_intent_msg = validAccessIntent(handlerInput,'EndQuizIntent');
if (valid_intent_msg == 'ok')
{
let output = requestAttributes.t('QUIZ_QUIT',sessionAttributes.quiz_score);
let next_intents = ['ShowOptionsIntent','StartQuizIntent','GrammaticalRuleIntent','MoreInfoExamplesIntent'];
Object.assign(sessionAttributes,{
quiz_score: 0,
question_ids: [],
nextIntents: next_intents,
speechOutput: output,
quiz: 'false',
answered: 'false',
finished: 'true',
question_index: 0,
next_cycle:'true',
answerOutput:'',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(output+'. '+requestAttributes.t('GRAMATICAL_OPTIONS',requestAttributes.t('OPTION_CHAPTER_HELP')))
.getResponse();
}
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(requestAttributes.t('WRONG_INPUT'))
.getResponse();
},
};
const DontKnowQuizIntent = {
canHandle(handlerInput)
{
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'DontKnowQuizIntent';
},
handle(handlerInput)
{
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
let valid_intent_msg = validAccessIntent(handlerInput,'DontKnowQuizIntent');
if (valid_intent_msg == 'ok')
{
let output=requestAttributes.t('QUIZ_DONTKNOW')
let next_intents = ['ShowOptionsIntent','AnswerQuizIntent','DontKnowQuizIntent','EndQuizIntent',,'StartQuizIntent',];
Object.assign(sessionAttributes,
{
nextIntents: next_intents,
//speechOutput: output,
quiz: 'true',
answered: 'false',
answerOutput:'',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
let translated_chapter = translate(handlerInput,sessionAttributes.chapter,requestAttributes.t('LANGUAGE_CHAPTERS'));
return startQuiz(handlerInput,translated_chapter,output);
}
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(requestAttributes.t('WRONG_INPUT'))
.getResponse();
},
};
const AnswerQuizIntent = {
canHandle(handlerInput)
{
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AnswerQuizIntent';
},
handle(handlerInput)
{
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
let valid_intent_msg = validAccessIntent(handlerInput,'AnswerQuizIntent');
if (valid_intent_msg == 'ok')
{
const slots = handlerInput.requestEnvelope.request.intent.slots;
const answer = slots['Answer'].value;
let output;
let session_output='';
if (answer>=1 && answer <= sessionAttributes.answer_index)
{
output = checkIfvalid(handlerInput,answer);
if (output == requestAttributes.t('QUIZ_CORRECT_ANSWER'))
session_output='';
else
session_output=output;
}
else
{
Object.assign(sessionAttributes,
{
answered:'false',
answerOutput: '',
});
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(requestAttributes.t('QUIZ_INVALID_ANSWER')+requestAttributes.t('OPTIONS_QUIZ')+sessionAttributes.speechOutput)
.getResponse();
}
let next_intents = ['ShowOptionsIntent','AnswerQuizIntent','DontKnowQuizIntent','EndQuizIntent',,'StartQuizIntent',];
Object.assign(sessionAttributes,
{
nextIntents: next_intents,
answerOutput: session_output,
quiz: 'true',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return startQuiz(handlerInput,translate(handlerInput,sessionAttributes.chapter,requestAttributes.t('LANGUAGE_CHAPTERS')),output);
}
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(requestAttributes.t('WRONG_INPUT'))
.getResponse();
},
};
const StartQuizIntent = {
canHandle(handlerInput)
{
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'StartQuizIntent';
},
handle(handlerInput)
{
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
let valid_intent_msg = validAccessIntent(handlerInput,'StartQuizIntent');
if (valid_intent_msg == 'ok')
{
//addImage(handlerInput,'true');
const slots = handlerInput.requestEnvelope.request.intent.slots;
let chapter = slots['Chapters'].value;
if (chapter == undefined)
chapter = sessionAttributes.chapter;
if (chapter == requestAttributes.t('NO_CHAPTER'))
{
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(chapter+requestAttributes.t('OPTION_START_QUIZ'))
.getResponse();
}
//create Questions and Answers here....
let translated_chapter = translate(handlerInput,chapter,requestAttributes.t('LANGUAGE_CHAPTERS'));
generateQuestions(handlerInput,sessionAttributes,translated_chapter);
let next_intents = ['ShowOptionsIntent','AnswerQuizIntent','DontKnowQuizIntent','EndQuizIntent','StartQuizIntent',];
Object.assign(sessionAttributes,
{
quiz_score: 0,
nextIntents: next_intents,
chapter: chapter,
quiz: 'true',
speechOutput: '',
question_index: 0,
finished: 'false',
answerOutput:'',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return startQuiz(handlerInput,translated_chapter,'');
}
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(valid_intent_msg)
.getResponse();
},
};
const MoreInfoExamplesIntent = {
canHandle(handlerInput)
{
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'MoreInfoExamplesIntent';
},
handle(handlerInput)
{
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
let speakOut='';
let translated_option;
let translated_chapter;
let valid_intent_msg = validAccessIntent(handlerInput,'MoreInfoExamplesIntent');
if (valid_intent_msg=='ok')
{
//the user gave a response to the welcome message and this gets its response
const slots = handlerInput.requestEnvelope.request.intent.slots;
const extra_choice = slots['MoreOptions'].value;
if (sessionAttributes.language != 'English')
{
translated_chapter = translate(handlerInput,sessionAttributes.chapter,requestAttributes.t('LANGUAGE_CHAPTERS'));
translated_option = translate(handlerInput,extra_choice,requestAttributes.t('LANGUAGE_MORE_OPTIONS'));
}
else
{
translated_option = extra_choice;
translated_chapter = sessionAttributes.chapter;
}
const chapterJsonName = returnJsonName(handlerInput,translated_chapter);
//function to decide the appropriate action for the users response. Returns appropriate message back to show.
speakOut = provideInfo(handlerInput,chapterJsonName,translated_option);
let next_intents = ['ShowOptionsIntent','StartQuizIntent','MoreInfoExamplesIntent','GrammaticalRuleIntent'];
Object.assign(sessionAttributes,
{
nextIntents: next_intents,
speechOutput: speakOut,
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
speakOut+=speakOut;
}
else
{
speakOut=valid_intent_msg;
}
handlerInput.responseBuilder.TemplateD
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(speakOut)
.getResponse();
},
};
const GrammaticalRuleIntent = {
canHandle(handlerInput)
{
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'GrammaticalRuleIntent';
},
handle(handlerInput)
{
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
let speakOut='';
let translated_option;
const slots = handlerInput.requestEnvelope.request.intent.slots;
let valid_intent_msg = validAccessIntent(handlerInput,'GrammaticalRuleIntent');
if (sessionAttributes.chapter != requestAttributes.t('NO_CHAPTER') || slots['GrammaticalRule'].value!=undefined )
{
if (valid_intent_msg == 'ok')
{
let options = slots['GrammaticalRule'].value;
const more_options = slots['MoreOptions'].value;
if (options == undefined)
options = sessionAttributes.chapter;
if (sessionAttributes.language != 'English')
translated_option = translate(handlerInput,options,requestAttributes.t('LANGUAGE_CHAPTERS'));
else
translated_option = options;
Object.assign(sessionAttributes,
{
chapter: options,
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
const chapterJsonName= returnJsonName(handlerInput,translated_option);
if (more_options!=undefined && more_options !='basic info')
speakOut = provideInfo(handlerInput,chapterJsonName,translate(handlerInput,more_options,requestAttributes.t('LANGUAGE_MORE_OPTIONS')));
else
speakOut = provideInfo(handlerInput,chapterJsonName,'basic info');
let next_intents = ['ShowOptionsIntent','StartQuizIntent','MoreInfoExamplesIntent','GrammaticalRuleIntent'];
Object.assign(sessionAttributes,
{
nextIntents: next_intents,
speechOutput: speakOut,
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
}
else
{
speakOut=valid_intent_msg;
}
}
else
{
speakOut = requestAttributes.t('NO_ACCESS_TO_THIS');
}
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(speakOut+' '+requestAttributes.t('GRAMATICAL_OPTIONS',requestAttributes.t('OPTION_CHAPTER_HELP')))
.getResponse();
},
};
const ShowOptionsIntent = {
canHandle(handlerInput)
{
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'ShowOptionsIntent';
},
handle(handlerInput)
{
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
//the user gave a response to the welcome message and this gets its response
const slots = handlerInput.requestEnvelope.request.intent.slots;
let options = slots['Options'].value;
let translated_option;
if (sessionAttributes.language != 'English')
translated_option = translate(handlerInput,options,requestAttributes.t('LANGUAGE_OPTIONS'));
//speakOUT will change accordingly to current_chapter...
//function to decide the appropriate action for the users response. Returns appropriate message back to show.
if (translated_option != undefined)
options = translated_option;
let speakOut=decideOption(handlerInput,options);
/* Object.assign(sessionAttributes,
{
speechOutput: speakOut,
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes); */
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(speakOut)
.getResponse();
},
};
const LaunchRequest = {
canHandle(handlerInput) {
const {request} = handlerInput.requestEnvelope;
return request.type === 'LaunchRequest'
|| (request.type === 'IntentRequest' && request.intent.name === 'AMAZON.StartOverIntent');
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
let speakOutput = requestAttributes.t('WELCOME_MESSAGE',requestAttributes.t('SKILL_NAME')) +requestAttributes.t('GENERAL_OPTIONS')+ requestAttributes.t('HELP_START');
defineNextIntents(handlerInput,requestAttributes.t('NO_CHAPTER'));
//addImage(handlerInput,'true');
Object.assign(sessionAttributes,
{
speechOutput: speakOutput,
language: requestAttributes.t('LANGUAGE'),
function:'not yet',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
},
};
const LocalizationInterceptor = {
process(handlerInput) {
const localizationClient = i18n.use(sprintf).init({
lng: handlerInput.requestEnvelope.request.locale,
overloadTranslationOptionHandler: sprintf.overloadTranslationOptionHandler,
resources: languageString,
returnObjects: true
});
const attributes = handlerInput.attributesManager.getRequestAttributes();
attributes.t = function (...args) {
return localizationClient.t(...args);
};
},
};
const HelpIntent = {
canHandle(handlerInput) {
const { request } = handlerInput.requestEnvelope;
return request.type === 'IntentRequest' && request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
let speakOutput = requestAttributes.t('HELP_START');
handlerInput.attributesManager.setSessionAttributes(speakOutput);
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
},
};
const FallbackHandler = {
// 2018-May-01: AMAZON.FallackIntent is only currently available in en-US locale.
// This handler will not be triggered except in that locale, so it can be
// safely deployed for any locale.
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.FallbackIntent';
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t('TOTALLY_WRONG_INPUT')+requestAttributes.t('FALLBACK_MESSSAGE',requestAttributes.t('SKILL_NAME'))+requestAttributes.t('HELP_NEEDED'))
.reprompt(requestAttributes.t('FALLBACK_REPROMPT'))
.getResponse();
},
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const speechOutput = requestAttributes.t('QUIT_MESSAGE');
return handlerInput.responseBuilder.speak(speechOutput)
.withShouldEndSession(true)
.getResponse();
},
};
const SessionEndedRequest = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return handlerInput.responseBuilder.getResponse();
},
};
const RepeatMessageIntent = {
canHandle(handlerInput)
{
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'RepeatMessageIntent';
},
handle(handlerInput) {
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
const output=sessionAttributes.speechOutput;
let answerOutput='';
if (sessionAttributes.answerOutput != undefined)
answerOutput=sessionAttributes.answerOutput;
return handlerInput.responseBuilder
.withShouldEndSession(false)
.speak(answerOutput+output)
.getResponse();
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t('SORRY_SAY_AGAIN',requestAttributes.t('LANGUAGE')))//+'error: '+error+'located: '+sessionAttributes.function+' chapter:'+sessionAttributes.chapter+' option:'+sessionAttributes.msg_debug)
.getResponse();
},
};
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
LaunchRequest,
HelpIntent,
ShowOptionsIntent,
GrammaticalRuleIntent,
CancelAndStopIntentHandler,
RepeatMessageIntent,
SessionEndedRequest,
FallbackHandler,
StartQuizIntent,
AnswerQuizIntent,
DontKnowQuizIntent,
EndQuizIntent,
MoreInfoExamplesIntent,
)
.addRequestInterceptors(LocalizationInterceptor)
.addErrorHandlers(ErrorHandler)
.lambda();
/**
* USER DEFINED FUNCTIONS..
*/
function next(handlerInput,next_value)
{
if (next_value==undefined)
return undefined;
else
return next_value;
}
/**
*
* @param {*} handlerInput Input from user
* @param {*} phrase The word inputed that needs to be translated into english.
* @param {*} langArray The section of the file we need to search for the translation.
*/
function translate(handlerInput,phrase,langArray)
{
const sessionAttributes =handlerInput.attributesManager.getSessionAttributes();
Object.assign(sessionAttributes,
{
function: 'start of translate',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const selected_lang = requestAttributes.t('LANGUAGE');
for (let i=0;i<langArray.length; i++)
if (selected_lang == 'Castellano')
if (langArray[i].Spanish == phrase)
return langArray[i].English;
Object.assign(sessionAttributes,
{
function: 'end of startQuiz',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return phrase;
}
function translateTest(handlerInput,phrase,target_lang,langArray)
{
const sessionAttributes =handlerInput.attributesManager.getSessionAttributes();
Object.assign(sessionAttributes,
{
function: 'start of translateTest',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const selected_lang = requestAttributes.t('LANGUAGE');
for (let i=0;i<langArray.length; i++)
if (target_lang == selected_lang && selected_lang!='English')
{
if (langArray[i].English == phrase)
{
if (target_lang == 'Castellano')
{return langArray[i].Spanish;}
}
}
else
{
if (selected_lang == 'Castellano')
if (langArray[i].Spanish == phrase)
return langArray[i].English;
}
Object.assign(sessionAttributes,
{
function: 'end of translateTest',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return phrase;
}
/**
*
* @param {*} handlerInput
* @param {*} option
*/
function returnJsonName(handlerInput,chapterName)
{
const sessionAttributes =handlerInput.attributesManager.getSessionAttributes();
Object.assign(sessionAttributes,
{
function: 'start of returnJsonName',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const chapterNames = requestAttributes.t('CHAPTERS_NAMES');
const chapterJsonNames = requestAttributes.t('CHAPTERS_NAMES_JSON');
let i=0;
while (chapterNames[i] != translate(handlerInput,chapterName,requestAttributes.t('LANGUAGE_CHAPTERS')) && i<chapterNames.length)
{
i++;
}
Object.assign(sessionAttributes,
{
function: 'end of returnJsonName',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
if (i<chapterNames.length)
return chapterJsonNames[i];
return 'WRONG CHAPTER';
}
/**
* Function that adds tokens for every picture used in the skill.
* Tokens are used to load the pictures in a later stage.
*/
/**
* FUNCTION decideStartOption
* TODO
* decides which actino to do next according to the inital response of the user
*/
function decideOption(handlerInput,option)
{
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
Object.assign(sessionAttributes,
{
function: 'start of decideOption',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
let return_msg;
let request_msg;
if (!(sessionAttributes.chapter == requestAttributes.t('NO_CHAPTER')))
request_msg = 'OPTION_CHAPTER_'//+returnJsonName(handlerInput,sessionAttributes.chapter)+'_';
else
request_msg = 'OPTION_START_';
if (option == 'help')
{
request_msg +='HELP';
if (sessionAttributes.quiz=='true')
return_msg = requestAttributes.t('OPTIONS_QUIZ')+sessionAttributes.speechOutput;
else
return_msg= requestAttributes.t(request_msg);
}
else if (option == 'grammatical rule' || option =='index' || option =='table of contents' )
{
return_msg = requestAttributes.t('OPTION_START_CHAPTERS')+all_Chapters(handlerInput);
}
else if (option == 'quiz')
{
return_msg = requestAttributes.t('OPTION_START_QUIZ');
}
else
{
return_msg = requestAttributes.t('OPTION_UNRECOGNISED');
}
Object.assign(sessionAttributes,
{
function: 'end of decideOption',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return return_msg;
}
function all_Chapters(handlerInput)
{
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
Object.assign(sessionAttributes,
{
function: 'start of all_chapters',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
let return_msg='';
let tempArray=requestAttributes.t('CHAPTERS_NAMES');
for (let i=0; i<tempArray.length;i++)
return_msg += translateTest(handlerInput,tempArray[i],'Castellano',requestAttributes.t('LANGUAGE_CHAPTERS'))+'. ';
Object.assign(sessionAttributes,
{
function: 'end of all_chapters',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
return return_msg;
}
/*
Function that returns the length of an array. To return length of sections of information in
chapter.
*/
function chapterLength(chapterArray)
{
let length=0;
while (typeof chapterArray[length] !== 'undefined')
{
length++;
}
return length;
}
/**
*
* To show the basic information of a chapter.
* MUST be common for every chapter
* Generalization function.
**/
function provideInfo(handlerInput,chapterNameJson,option)
{
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
let chapterArray = requestAttributes.t(chapterNameJson);
let length = chapterLength(chapterArray);
let curr_position=0;
Object.assign(sessionAttributes,
{
function: 'before while of provideInfo',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
if (option =='example')
option = 'examples'
while (chapterArray[curr_position].title != option && curr_position < length)
curr_position++;
if (option == 'basic info')
{
//addImage(handlerInput,'true');
return requestAttributes.t('BASIC_INFO')+sessionAttributes.chapter+'. '+chapterArray[curr_position].sub_text;
}
else if (option == 'more info')
{
Object.assign(sessionAttributes,
{
function: 'before while of provideInfo',
msg: 'before add Image in provide',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
//addImage(handlerInput,'false');
return requestAttributes.t('MORE_INFO')+sessionAttributes.chapter+'. '+chapterArray[curr_position].sub_text;
}
else if (option == 'examples')
{
//addImage(handlerInput,'true');
return requestAttributes.t('EXAMPLE_INFO')+sessionAttributes.chapter+'. '+chapterArray[curr_position].sub_text;
}
}
/**
* Generates the questions in a random order and populates them with the shuffeled answers.
* @param {*} handelrInput
* @param {*} sessionAttributes
* @param {*} translate
*/
function generateQuestions(handlerInput,sessionAttributes,chapterName)
{
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
Object.assign(sessionAttributes,
{
function: 'start of generateQuestions',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
const chapterJsonName = returnJsonName(handlerInput,chapterName);
let chapterArray = requestAttributes.t(chapterJsonName+'_QUIZ');
let currentIndex = chapterLength(chapterArray);
let chaptercopy = chapterArray;
//object variables
let questions_ids=[];
while (currentIndex !== 0)
{
let randomIndex = Math.floor(Math.random()*currentIndex);
currentIndex -= 1;
let tempVal = chaptercopy[currentIndex];
chaptercopy[currentIndex] = chaptercopy[randomIndex];
chaptercopy[randomIndex] = tempVal;
questions_ids.push(chaptercopy[currentIndex].id);
}
Object.assign(sessionAttributes,{
question_ids:questions_ids,
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
Object.assign(sessionAttributes,
{
function: 'end of generateQuestions',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
}
function generateAnswers(handlerInput,question_id,chapterName)
{
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
const chapterJsonName = returnJsonName(handlerInput,chapterName);
let chapterArray = requestAttributes.t(chapterJsonName+'_QUIZ');
Object.assign(sessionAttributes,
{
error: chapterArray,
function: 'start of generateAnswers',
});
handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
let answers_copy = chapterArray[question_id].answers;
let answerCurrentIndex = answers_copy.length;
let final_answers=[];
const correct=answers_copy[0];
while (answerCurrentIndex !== 0)
{
let randomAnswerIndex = Math.floor(Math.random()*answerCurrentIndex);
answerCurrentIndex -=1;
let tempAnsVal = answers_copy[answerCurrentIndex];
answers_copy[answerCurrentIndex] = answers_copy[randomAnswerIndex];
answers_copy[randomAnswerIndex] = tempAnsVal;
final_answers.push(answers_copy[answerCurrentIndex]);
}