-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
321 lines (287 loc) · 13.7 KB
/
index.js
File metadata and controls
321 lines (287 loc) · 13.7 KB
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
const alexa = require('ask-sdk-core');
const aws = require('aws-sdk');
const axios = require("axios");
const dom = require('xmldom').DOMParser;
const fs = require('fs');
const xpath = require('xpath');
const yaml = require('js-yaml');
global.config = "";
const s3 = new aws.S3();
const kms = new aws.KMS({'region': 'us-east-1'});
let environment = 'prod';
let base_url_search = "http://www.worldcat.org/webservices/catalog/search/worldcat/opensearch?q="
let base_url_location = "http://www.worldcat.org/webservices/catalog/content/libraries/"
let base_url_find_libraries = "http://www.worldcat.org/webservices/catalog/content/libraries?"
async function getLocation(requestEnvelope, serviceClientFactory, isGeoSupported) {
let location = "";
if (isGeoSupported && requestEnvelope.context.Geolocation.coordinate){
let geocoordinates = requestEnvelope.context.Geolocation.coordinate
location = {"lat": geocoordinates.latitudeInDegrees, "lon": geocoordinates.longitudeInDegrees};
} else {
const { deviceId } = requestEnvelope.context.System.device;
const deviceAddressServiceClient = serviceClientFactory.getDeviceAddressServiceClient();
const address = await deviceAddressServiceClient.getCountryAndPostalCode(deviceId);
location = {"postalCode": address.postalCode};
}
return location;
}
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
async handle(handlerInput) {
const { requestEnvelope, serviceClientFactory, responseBuilder } = handlerInput;
const isGeoSupported = requestEnvelope.context.System.device.supportedInterfaces.Geolocation;
if (isGeoSupported) {
const geolocationConsent = requestEnvelope.context.System.user.permissions.scopes['alexa::devices:all:geolocation:read.status']
if (isGeoSupported & geolocationConsent == 'DENIED') {
return responseBuilder
.speak('Please enable Geolocation permissions in the Amazon Alexa app.')
.withAskForPermissionsConsentCard(['alexa::devices:all:geolocation:read'])
.getResponse();
}
} else {
const addressConsent = requestEnvelope.context.System.user.permissions.scopes['read::alexa:device:all:address:country_and_postal_code'];
if (!addressConsent || addressConsent == 'DENIED') {
return responseBuilder
.speak('Please enable Location permissions in the Amazon Alexa app.')
.withAskForPermissionsConsentCard(['read::alexa:device:all:address:country_and_postal_code'])
.getResponse();
}
}
let location = await getLocation(requestEnvelope, serviceClientFactory, isGeoSupported);
let session_attributes = {
"location" : location
}
await handlerInput.attributesManager.setSessionAttributes(session_attributes);
const speechText = "Welcome to the Alexa Ask WorldCat service. Ask me to find a book for you. For example, you can say, \"Where can I find 'On the Road.'\"";
const reprompt = "Ask me to find a book for you. For example, you can say, \"Where can I find 'On the Road.'\"";
return responseBuilder
.speak(speechText)
.reprompt(reprompt)
.withSimpleCard('Ask WorldCat', speechText)
.getResponse();
}
};
const SearchIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'SearchIntent';
},
async handle(handlerInput) {
let attributes = await handlerInput.attributesManager.getSessionAttributes();
let search = handlerInput.requestEnvelope.request.intent.slots.Search.value;
let search_url = base_url_search + "%22" + encodeURIComponent(search) + "%22&wskey=" + config['wskey'];
var request_config = {
headers: {
'User-Agent': 'node.js KAC Alexa demo app'
}
};
// call the Search API
let request_response = await axios.get(search_url, request_config);
let doc = new dom().parseFromString(request_response.data);
let entries = doc.getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'entry');
// check for empty result set
if (entries.length == 0){
let speechText = "I'm sorry, I couldn't find anything matching your search."
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Ask WorldCat', speechText)
.getResponse();
}
// store pertinent metadata
let entry = entries[0]
let title = entry.getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'title')[0].firstChild.data
let authorName = entry.getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'author')[0]
let author = authorName.getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'name')[0].firstChild.data
let oclcNum = entry.getElementsByTagNameNS('http://purl.org/oclc/terms/', 'recordIdentifier')[0].firstChild.data
// get library location info from Search API
let location_url = base_url_location + oclcNum + "?wskey=" + config['wskey'] + "&format=json"
if (attributes.location.postalCode) {
location_url += "&location=" + attributes.location.postalCode
} else {
location_url += "&lat=" + attributes.location.lat + "lon=" + attributes.location.lon;
}
let location_response = await axios.get(location_url, request_config);
let location_data = location_response.data;
let closest_library = location_data['library'][0]
let closest_library_name = closest_library['institutionName']
let closest_library_address = closest_library['streetAddress1'] + " " + closest_library['streetAddress2'] + ", " + closest_library['city'] + ", " + closest_library['state'] + ", " + closest_library['postalCode'] + ", " + closest_library['country']
// build speech output and store session attributes
let speechText = "The closest library where you can find " + title + " by " + author + " is " + closest_library_name + '.';
let reprompt = "Do you need the library's address?";
let session_attributes = {
"title" : title,
"author" : author,
"closest_library_name" : closest_library_name,
"closest_library_address" : closest_library_address,
}
await handlerInput.attributesManager.setSessionAttributes(session_attributes);
// return response
return handlerInput.responseBuilder
.speak(speechText + ' ' + reprompt)
.reprompt(reprompt)
.withSimpleCard('Ask WorldCat', speechText + reprompt)
.getResponse();
}
};
const YesIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.YesIntent';
},
async handle(handlerInput) {
let attributes = await handlerInput.attributesManager.getSessionAttributes();
// pull needed metadata from session attributes
let title = attributes.title;
let author = attributes.author;
let closest_library_name = attributes.closest_library_name;
let closest_library_address = attributes.closest_library_address;
let speechText = "I've sent the address to your device."
// build card text
let card_text = closest_library_name + "\n\n" + closest_library_address;
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Ask WorldCat', card_text)
.getResponse();
}
};
const LibrarySearchIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'LibrarySearchIntent';
},
async handle(handlerInput) {
let attributes = await handlerInput.attributesManager.getSessionAttributes();
let library_search_url = base_url_find_libraries + "libtype=2&wskey=" + config['wskey'] + "&format=json";
if (attributes.location.postalCode) {
library_search_url += "&location=" + attributes.location.postalCode
} else {
library_search_url += "&lat=" + attributes.location.lat + "lon=" + attributes.location.lon;
}
var request_config = {
headers: {
'User-Agent': 'node.js KAC Alexa demo app'
}
};
// call the WorldCat Search API libraries endpoint
let library_response = await axios.get(library_search_url, request_config);
let location_data = library_response.data;
let closest_library = location_data['library'][0]
let closest_library_name = closest_library['institutionName']
let closest_library_address = closest_library['streetAddress1'] + closest_library['streetAddress2'] + ", " + closest_library['city'] + ", " + closest_library['state'] + ", " + closest_library['postalCode'] + ", " + closest_library['country']
let speech_output = "The closest public library is " + closest_library_name + ".\n\nIt is located at " + closest_library_address;
return handlerInput.responseBuilder
.speak(speech_output)
.withSimpleCard('Ask WorldCat', speech_output)
.getResponse();
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speechText = "Welcome to the Alexa Ask WorldCat service. Ask me to find a book for you. For example, you can say, \"Where can I find 'On the Road.'\"";
const reprompt = "Ask me to find a book for you. For example, you can say, \"Where can I find 'On the Road.'\"";
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(reprompt)
.withSimpleCard('Ask WorldCat', speechText)
.getResponse();
}
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const speechText = 'Goodbye!';
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Ask WorldCat', speechText)
.getResponse();
}
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
//any cleanup logic goes here
return handlerInput.responseBuilder.getResponse();
}
};
const addressErrorHandler = {
canHandle(handlerInput, error) {
return error.name.startsWith('ServiceError');
},
handle(handlerInput, error) {
console.log(error, error.stack);
let speechText = "I'm sorry, error retrieving your address."
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Ask WorldCat', speechText)
.getResponse();
}
};
const apiErrorHandler = {
canHandle(handlerInput, error) {
return error.response;
},
handle(handlerInput, error) {
console.log(error.message, error.config.url);
let speechText = "I'm sorry, application search error."
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Ask WorldCat', speechText)
.getResponse();
}
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
return handlerInput.responseBuilder
.speak('Sorry, I can\'t understand the command. Please say again.')
.reprompt('Sorry, I can\'t understand the command. Please say again.')
.getResponse();
},
};
let skill;
exports.handler = async function (event, context) {
try {
let data = await kms.decrypt({CiphertextBlob: fs.readFileSync(environment + "_config_encrypted.txt")}).promise();
global.config = yaml.load(data['Plaintext'].toString());
console.log(`REQUEST++++${JSON.stringify(event)}`);
if (!skill) {
skill = alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
SearchIntentHandler,
LibrarySearchIntentHandler,
YesIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
)
.addErrorHandlers(
addressErrorHandler,
apiErrorHandler,
ErrorHandler
)
.withApiClient(new alexa.DefaultApiClient())
.create();
}
const response = await skill.invoke(event, context);
console.log(`RESPONSE++++${JSON.stringify(response)}`);
return response;
} catch (Error){
console.log(Error, Error.stack);
}
};