-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
238 lines (207 loc) · 7.32 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
// dependencies
const Alexa = require('ask-sdk-core');
var people = require('./people.json');
var fortunes = require('./fortunes.json');
const fs = require('fs')
const peopleFile = './people.json';
const fortunesFile = './fortunes.json';
try {
if (fs.existsSync(peopleFile) && fs.existsSync(fortunesFile)) {
people = require('./people.json');
fortunes = require('./fortunes.json');
}
} catch(err) {
console.error(err)
}
//This function starts the conversation by asking who you want a fortune for
const GetFortuneHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest'
|| (request.type === 'IntentRequest'
&& request.intent.name === 'GetNewFortuneIntent');
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak('Who do you want a fortune for?')
.reprompt()
.getResponse();
},
};
//This function will get the name for who the fortune is for, and create a fortune!
const GetFortuneForIntent = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return(request.type === 'IntentRequest'
&& request.intent.name === 'GetFortuneForIntent');
},
handle(handlerInput) {
console.log("handlerInput for GetFortuneForIntent.");
const { attributesManager } = handlerInput;
const name = Alexa.getSlotValue(handlerInput.requestEnvelope, 'name');
const person = findPerson(name);
const fortune = getFortuneFor(name, person);
if (person !== null && fortune !== null) {
return handlerInput.responseBuilder
.speak('Getting a fortune for ' + name + '...<break time="500ms"/> '
+ fortune + ' <break time="1000ms"/>' + 'Do you want another fortune?')
.reprompt()
.getResponse();
} else {
return handlerInput.responseBuilder
.speak('Missing people and fortunes.')
.reprompt()
.getResponse();
}
},
};
const YesHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.YesIntent';
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak('Who do you want a fortune for?')
.reprompt()
.getResponse();
},
};
const NoHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.NoIntent';
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak('Ok goodbye!')
.reprompt()
.getResponse();
},
};
const HelpHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak("meow")
.reprompt("meow")
.getResponse();
},
};
const FallbackHandler = {
// The FallbackIntent can only be sent in those locales which support it,
// so this handler will always be skipped in locales where it is not supported.
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('fallback')
.getResponse();
},
};
const ExitHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& (request.intent.name === 'AMAZON.CancelIntent'
|| request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak("stopping")
.getResponse();
},
};
const SessionEndedRequestHandler = {
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();
},
};
// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`~~~~ Error handled: ${error.stack}`);
const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
GetFortuneHandler,
HelpHandler,
YesHandler,
NoHandler,
ExitHandler,
FallbackHandler,
SessionEndedRequestHandler,
GetFortuneForIntent,
)
.addErrorHandlers(ErrorHandler)
.lambda();
function findPerson(name) {
if (people.length > 0) {
for(const index in people) {
const person = people[index]
for (const nameIndex in person["name_variants"]) {
const localName = person["name_variants"][nameIndex]
if (localName.toLowerCase() === name.toLowerCase()) {
return person
}
}
}
//Return the default person if we fail to match a user
return people[0];
} else {
return null;
}
}
function getFortuneFor(name, person) {
if (fortunes.length > 0) {
//let fortune = getFromArray(new Array(fortunes[44]));
let fortune = getFromArray(fortunes);
//console.log("Fortune: " + fortune);
const nameToUse = person['display_name'] !== null ? person['display_name'] : name;
fortune = fortune.replace(/__name__/g, nameToUse);
fortune = fortune.replace(/__activity__/g, getFromArray(person['activity']));
fortune = fortune.replace(/__food__/g, getFromArray(person['food']));
fortune = fortune.replace(/__restaurant__/g, getFromArray(person['restaurant']));
fortune = fortune.replace(/__place__/g, getFromArray(person['place']));
fortune = fortune.replace(/__friend__/g, getFromArray(person['friend']));
fortune = fortune.replace(/__family__/g, getFromArray(person['family']));
return fortune
} else {
return null;
}
}
function getFromArray(array) {
return array[Math.floor(Math.random() * array.length)];
}