forked from jaywalker76/alexa-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
240 lines (207 loc) · 8.28 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
const Alexa = require('ask-sdk-core');
const helpOutput = 'You can demonstrate entity resolution by providing varied answers to the questions asked. Try saying lets play a game.';
const helpReprompt = 'Try saying "start over".';
const questions = [
{
answer: 'Thomas Edison',
question: 'Who is credited with suggesting the word "hello" be used when answering the telephone?',
synonyms: ['Edison', 'Menlo Park'],
},
{
answer: 'North America',
question: 'The Passenger Pigeon, now extinct, was endemic to which continent?',
synonyms: ['Northern America', 'Canada', 'Mexico', 'America'],
},
{
answer: 'Auguste Rodin',
question: 'Which artist created the sculpture "The Thinker"?',
synonyms: ['Rodin', 'Auguste', 'august', 'august rodin'],
},
{
answer: 'The Canary Islands',
question: 'What is the name of the Spanish islands that lie off the Northwest coast of Africa?',
synonyms: ['Canary Islands', 'Canary Island', 'the Canaries'],
},
];
// This is a list of positive speechcons that this skill will use when a user gets
// a correct answer. For a full list of supported speechcons, go here:
// https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/speechcon-reference
const speechConsCorrect = ['Booya', 'All righty', 'Bam', 'Bazinga', 'Bingo', 'Boom', 'Bravo', 'Cha Ching', 'Cheers', 'Dynomite',
'Hip hip hooray', 'Hurrah', 'Hurray', 'Huzzah', 'Oh dear. Just kidding. Hurray', 'Kaboom', 'Kaching', 'Oh snap', 'Phew',
'Righto', 'Way to go', 'Well done', 'Whee', 'Woo hoo', 'Yay', 'Wowza', 'Yowsa'];
// This is a list of negative speechcons that this skill will use when a user gets
// an incorrect answer. For a full list of supported speechcons, go here:
// https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/speechcon-reference
const speechConsWrong = ['Argh', 'Aw man', 'Blarg', 'Blast', 'Boo', 'Bummer', 'Darn', 'D\'oh', 'Dun dun dun', 'Eek', 'Honk', 'Le sigh',
'Mamma mia', 'Oh boy', 'Oh dear', 'Oof', 'Ouch', 'Ruh roh', 'Shucks', 'Uh oh', 'Wah wah', 'Whoops a daisy', 'Yikes'];
// handlers
const LaunchRequestHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest' ||
(request.type === 'IntentRequest' && request.intent.name === 'NewGameIntent');
},
handle(handlerInput) {
return nextQuestion(handlerInput, 'Welcome, ');
},
};
const AnswerHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' && request.intent.name === 'AnswerIntent';
},
handle(handlerInput) {
// get the Answer
const slotValues = getSlotValues(handlerInput.requestEnvelope.request.intent.slots);
// check the Answer
// where answer is my slot name
// what synonym the person said - slotValues.answer.synonym
// what that resolved to - slotValues.answer.resolved
return checkAnswer(handlerInput, slotValues.answer.resolved);
// report the results and ask a new quesiton
},
};
const AmazonHelpHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' && request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const responseBuilder = handlerInput.responseBuilder;
return responseBuilder
.speak(helpOutput)
.reprompt(helpReprompt)
.getResponse();
},
};
const AmazonCancelStopHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' &&
(request.intent.name === 'AMAZON.CancelIntent' || request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const responseBuilder = handlerInput.responseBuilder;
const speechOutput = 'Okay, talk to you later! ';
return responseBuilder
.speak(speechOutput)
.withShouldEndSession(true)
.getResponse();
},
};
const SessionEndedHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return handlerInput.responseBuilder.getResponse();
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
const request = handlerInput.requestEnvelope.request;
console.log(`Original Request was: ${JSON.stringify(request, null, 2)}`);
console.log(`Error handled: ${error}`);
return handlerInput.responseBuilder
.speak('Sorry, I can not understand the command. Please say again.')
.reprompt('Sorry, I can not understand the command. Please say again.')
.getResponse();
},
};
// helpers
function nextQuestion(handlerInput, preface) {
const responseBuilder = handlerInput.responseBuilder;
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
// get a question
const data = getRandomQuestion(questions);
sessionAttributes.currentQuestionIndex = data.index;
// ask the question
return responseBuilder
.speak(preface + data.text.question)
.reprompt(`Here's your question again ${data.text.question}`)
.getResponse();
}
function checkAnswer(handlerInput, givenAnswer) {
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
if (sessionAttributes.currentQuestionIndex) {
const correctAnswer = questions[sessionAttributes.currentQuestionIndex].answer;
if (correctAnswer.toUpperCase() === givenAnswer.toUpperCase()) {
// correct
return nextQuestion(handlerInput, `${getSpeechCon(true)} you got it, ${givenAnswer} was right. here's another, `);
}
// incorrect
return nextQuestion(handlerInput, `${getSpeechCon(false)} the answer was ${correctAnswer}. let's try again, `);
}
// no current question
return nextQuestion(handlerInput, 'Here\'s a new question ');
}
function getSpeechCon(type) {
if (type) {
return `<say-as interpret-as='interjection'>${getRandomPhrase(speechConsCorrect)}! </say-as><break strength='strong'/>`;
}
return `<say-as interpret-as='interjection'>${getRandomPhrase(speechConsWrong)} </say-as><break strength='strong'/>`;
}
function getRandomPhrase(array) {
// the argument is an array [] of words or phrases
const i = Math.floor(Math.random() * array.length);
return (array[i]);
}
function getSlotValues(filledSlots) {
const slotValues = {};
Object.keys(filledSlots).forEach((item) => {
const name = filledSlots[item].name;
slotValues[name] = {};
// Extract the nested key 'code' from the ER resolutions in the request
let erStatusCode;
try {
erStatusCode = ((((filledSlots[item] || {}).resolutions ||
{}).resolutionsPerAuthority[0] || {}).status || {}).code;
} catch (e) {
// console.log('erStatusCode e:' + e)
}
switch (erStatusCode) {
case 'ER_SUCCESS_MATCH':
slotValues[name].synonym = filledSlots[item].value;
slotValues[name].resolved = filledSlots[item].resolutions
.resolutionsPerAuthority[0].values[0].value.name;
slotValues[name].isValidated = filledSlots[item].value ===
filledSlots[item].resolutions.resolutionsPerAuthority[0].values[0].value.name;
slotValues[name].statusCode = erStatusCode;
break;
default: // ER_SUCCESS_NO_MATCH, undefined
slotValues[name].synonym = filledSlots[item].value;
slotValues[name].resolved = filledSlots[item].value;
slotValues[name].isValidated = false;
slotValues[name].statusCode = erStatusCode === undefined ? 'undefined' : erStatusCode;
break;
}
}, this);
return slotValues;
}
function getRandomQuestion(array) {
// the argument is an array [] of words or phrases
const i = Math.floor(Math.random() * array.length);
return ({
text: array[i],
index: i,
});
}
// exports
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
AmazonCancelStopHandler,
AmazonHelpHandler,
AnswerHandler,
LaunchRequestHandler,
SessionEndedHandler,
)
.addErrorHandlers(ErrorHandler)
.lambda();