-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (117 loc) · 4.87 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
/* eslint-disable func-names */
/* eslint-disable no-console */
const Alexa = require('ask-sdk');
const cheerio = require('cheerio');
const request = require('request');
function getAnalysis(name, gender) {
//Evaluate the source
var source = 'https://www.kabalarians.com/name-meanings/names/' + gender + '/' + name + '.htm';
//Use Promise to wait for data
return new Promise((resolve, reject) => {
request(source, (error, response, body) => {
//Print the error if one occurred, message may be passed to Alexa to inform user
console.log('error:', error);
//Get the analysis from the HTML response body
var analysis = cheerio.load(body)('#headerOL ul:first-of-type li:first-of-type').text();
if (analysis.length == 0) {
//Error message if analysis not found
analysis = 'Sorry, ' + name + ', I couldn\'t find an analysis for your name';
}
resolve(analysis);
});
});
}
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
const speechText = 'Welcome to Name Analysis. ' +
'I can help you understand whether your first name is helping or hurting you... ' +
'Please tell me your first name...';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt('Please tell me your first name...')
.getResponse();
}
};
const AnalysisIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AnalysisIntent';
},
async handle(handlerInput) {
var gender = handlerInput.requestEnvelope.request.intent.slots.Gender.value;
var name = handlerInput.requestEnvelope.request.intent.slots.Name.value;
var analysis = await getAnalysis(name, gender);
return handlerInput.responseBuilder
.speak(analysis + ' <break time="3s"/>Would you like to try another name?')
.reprompt('Would you like to try another name?')
.getResponse();
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speechText = 'I can help you understand whether your first name is helping or hurting you... ' +
'Please tell me your first name...';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(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 = 'Thank you for trying Name Analysis. Remember your name can both help you and hurt you!';
return handlerInput.responseBuilder
.speak(speechText)
.getResponse();
}
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
// Any cleanup logic goes here.
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.message}`);
const speechText = `Sorry, I couldn't understand what you said. Please try again.`;
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
};
// This handler acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
AnalysisIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler)
.addErrorHandlers(
ErrorHandler)
.lambda();