-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
276 lines (244 loc) · 10.4 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
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"]*/
/**
* This sample demonstrates a simple skill built with the Amazon Alexa Skills
* nodejs skill development kit.
* This sample supports en-US lauguage.
* The Intent Schema, Custom Slots and Sample Utterances for this skill, as well
* as testing instructions are located at https://github.com/alexa/skill-sample-nodejs-trivia
**/
'use strict';
const Alexa = require('alexa-sdk');
const questions = require('./question_bank_clean.json');
const EXAM_NAME = 'Ham Radio General Class'; // TODO Be sure to change this for your skill.
const ANSWER_COUNT = 4; // The number of possible answers per trivia question.
const EXAM_LENGTH = 5; // The number of questions per trivia game.
const EXAM_STATES = {
TRIVIA: '_ASKMODE', // Asking exam questions.
START: '_STARTMODE', // Entry point, start the game.
HELP: '_HELPMODE', // The user is asking for help.
};
const APP_ID = undefined; // TODO replace with your app ID (OPTIONAL)
function populateExamQuestions() {
const examQuestions = [];
const indexList = [];
let index = questions.length;
if (EXAM_LENGTH > index) {
throw new Error('Invalid Exam Length.');
}
// Build our test pool
for (let j = 0; j < EXAM_LENGTH; j += 1) {
const rand = Math.floor(Math.random() * index);
//index -= 1;
examQuestions.push(questions[rand]);
// @TODO: remove question from pool?
}
return examQuestions;
}
function handleUserResponse(userGaveUp) {
let speechOutput = '';
let speechOutputAnalysis = '';
const examQuestions = this.attributes.questions;
let currentScore = parseInt(this.attributes.score, 10);
let currentQuestionIndex = parseInt(this.attributes.currentQuestionIndex, 10);
var correctAnswerText = this.attributes.correctAnswerText;
console.log(this.event);
if (this.event.request.intent.slots.Response.value === this.attributes.correctAnswerValue) {
currentScore += 1;
speechOutputAnalysis = 'correct. ';
} else {
if (!userGaveUp) {
speechOutputAnalysis = 'wrong. ';
}
speechOutputAnalysis += `The correct answer is ${correctAnswerText}. `;
}
// Check if we can exit the game session after GAME_LENGTH questions (zero-indexed)
if (this.attributes.currentQuestionIndex === EXAM_LENGTH - 1) {
speechOutput = userGaveUp ? '' : 'That answer is ';
speechOutput += `${speechOutputAnalysis}You got ${currentScore.toString()} out of ${
EXAM_LENGTH.toString()} questions correct. Good luck with your exam!`;
this.emit(':tell', speechOutput);
} else {
currentQuestionIndex += 1;
let question = examQuestions[currentQuestionIndex];
const spokenQuestion = question.question_text;
const roundAnswers = question.responses;
const questionIndexForSpeech = currentQuestionIndex + 1;
let repromptText = `Question ${questionIndexForSpeech.toString()}. ${spokenQuestion} `;
var correctAnswerValue = '';
for (let i = 0; i < question.responses.length; i += 1) {
let thisResponse = question.responses[i];
repromptText += `${thisResponse.response_text}. `;
if (thisResponse.is_correct) {
correctAnswerText = thisResponse.response_text;
correctAnswerValue = thisResponse.selection;
}
}
speechOutput += userGaveUp ? '' : 'That answer is ';
speechOutput += `${speechOutputAnalysis}Your score is ${currentScore.toString()}. ${repromptText}`;
Object.assign(this.attributes, {
speechOutput: repromptText,
repromptText,
currentQuestionIndex,
questions: examQuestions,
score: currentScore,
correctAnswerValue: correctAnswerValue,
correctAnswerText: correctAnswerText,
});
this.emit(':askWithCard', speechOutput, repromptText, EXAM_NAME, repromptText);
}
}
const newSessionHandlers = {
// enter here, setup handlers & state
'NewSession': function () {
this.handler.state = EXAM_STATES.START;
if (this.event.request.type === 'LaunchRequest') {
this.emitWithState('StartExam', true);
} else if (this.event.request.type === 'IntentRequest') {
console.log(`current intent: ${this.event.request.intent.name
}, current state:${this.handler.state}`);
const intent = this.event.request.intent.name;
this.emitWithState(intent);
}
},
'SessionEndedRequest': function () {
const speechOutput = 'OK, Goodbye!';
this.emit(':tell', speechOutput);
},
};
const createStateHandler = Alexa.CreateStateHandler;
const startStateHandlers = createStateHandler(EXAM_STATES.START, {
'BeginExamIntent': function (newGame) {
console.log('starting!');
let speechOutput = newGame ? `Welcome to ${EXAM_NAME}. I will ask you ${EXAM_LENGTH.toString()
} questions, try to get as many right as you can. Just say the letter of the answer. Let's begin. ` : '';
const examQuestions = populateExamQuestions();
console.log(examQuestions);
const currentQuestionIndex = 0;
const currentQuestion = examQuestions[currentQuestionIndex];
let repromptText = `Question 1. ${currentQuestion.question_text} `;
var correctAnswerText = '';
var correctAnswerValue = '';
for (let i = 0; i < currentQuestion.responses.length; i += 1) {
let thisResponse = currentQuestion.responses[i];
repromptText += `${thisResponse.response_text}. `;
if (thisResponse.is_correct) {
correctAnswerText = thisResponse.response_text;
correctAnswerValue = thisResponse.selection;
}
}
speechOutput += repromptText;
Object.assign(this.attributes, {
speechOutput: repromptText,
repromptText,
currentQuestionIndex,
questions: examQuestions,
score: 0,
correctAnswerText: correctAnswerText,
correctAnswerValue: correctAnswerValue
});
// Set the current state to trivia mode. The skill will now use handlers defined in triviaStateHandlers
this.handler.state = EXAM_STATES.TRIVIA;
this.emit(':askWithCard', speechOutput, repromptText, EXAM_NAME, repromptText);
},
'AMAZON.HelpIntent': function () {
this.handler.state = EXAM_STATES.HELP;
this.emitWithState('helpTheUser', true);
},
'Unhandled': function () {
this.emit('StartExam', true);
},
'SessionEndedRequest': function () {
const speechOutput = 'OK, Goodbye!';
this.emit(':tell', speechOutput);
},
});
const examStateHandlers = createStateHandler(EXAM_STATES.TRIVIA, {
'ProvideResponseIntent': function () {
handleUserResponse.call(this, false);
},
'DontKnowIntent': function () {
handleUserResponse.call(this, true);
},
'AMAZON.StartOverIntent': function () {
this.handler.state = EXAM_STATES.START;
this.emitWithState('StartExam', false);
},
'AMAZON.RepeatIntent': function () {
this.emit(':ask', this.attributes.speechOutput, this.attributes.repromptText);
},
'AMAZON.HelpIntent': function () {
this.handler.state = EXAM_STATES.HELP;
this.emitWithState('helpTheUser', false);
},
'AMAZON.StopIntent': function () {
this.handler.state = EXAM_STATES.HELP;
const speechOutput = 'Would you like to keep going?';
this.emit(':ask', speechOutput, speechOutput);
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', 'Ok, let\'s try again soon.');
},
'Unhandled': function () {
const speechOutput = `Try saying the letter of your answer`;
this.emit(':ask', speechOutput, speechOutput);
},
'EndExamIntent': function () {
const speechOutput = 'OK, Goodbye!';
this.emit(':tell', speechOutput);
},
});
const helpStateHandlers = createStateHandler(EXAM_STATES.HELP, {
'helpTheUser': function (newExam) {
const askMessage = newExam ? 'Would you like to start a practice exam?' : 'To repeat the last question, say, repeat. Would you like to keep going?';
const speechOutput = `I will ask you ${EXAM_LENGTH} multiple choice questions. Respond with the letter of the answer. `
+ `For example, say A, B, C or D. To start a new exam at any time, say, start exam. ${askMessage}`;
const repromptText = `To give an answer to a question, respond with the letter of the answer . ${askMessage}`;
this.emit(':ask', speechOutput, repromptText);
},
'BeginExamIntent': function () {
this.handler.state = EXAM_STATES.START;
this.emitWithState('StartExam', false);
},
'AMAZON.RepeatIntent': function () {
this.emitWithState('helpTheUser');
},
'AMAZON.HelpIntent': function () {
this.emitWithState('helpTheUser', false);
},
'AMAZON.YesIntent': function () {
if (this.attributes.speechOutput && this.attributes.repromptText) {
this.handler.state = EXAM_STATES.TRIVIA;
this.emitWithState('AMAZON.RepeatIntent');
} else {
this.handler.state = EXAM_STATES.START;
this.emitWithState('StartExam', false);
}
},
'AMAZON.NoIntent': function () {
const speechOutput = 'Ok, we\'ll estother time. Goodbye!';
this.emit(':tell', speechOutput);
},
'AMAZON.StopIntent': function () {
const speechOutput = 'Would you like to keep going with your exam?';
this.emit(':ask', speechOutput, speechOutput);
},
'AMAZON.CancelIntent': function () {
this.handler.state = EXAM_STATES.TRIVIA;
this.emitWithState('AMAZON.RepeatIntent');
},
'Unhandled': function () {
const speechOutput = 'Say yes to continue, or no to end the exam.';
this.emit(':ask', speechOutput, speechOutput);
},
'EndExamIntent': function () {
const speechOutput = 'OK, Goodbye!';
this.emit(':tell', speechOutput);
},
});
exports.handler = (event, context) => {
const alexa = Alexa.handler(event, context);
alexa.registerHandlers(newSessionHandlers, startStateHandlers, examStateHandlers, helpStateHandlers);
alexa.APP_ID = APP_ID;
alexa.execute();
};