-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
300 lines (268 loc) · 12 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* *
* This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK (v2).
* Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,
* session persistence, api calls, and more.
* */
const Alexa = require('ask-sdk-core');
const messages = {
WELCOME: 'Welcome to the Sample Alexa Person Profile API Skill! You can ask for your name, given name or your phone number. What do you want to ask?',
WHAT_DO_YOU_WANT: 'What do you want to ask?',
PROFILE_NOT_RECOGNIZED: 'Your profile is not recognized. If you haven\'t yet, please create and train a voice profile through the Alexa app and try again.',
NOTIFY_MISSING_PERMISSIONS: 'Please enable Person Profile permissions in the Amazon Alexa app.',
NAME_MISSING: 'It looks like we don\'t have your name. You can set your name through the Alexa App.',
GIVEN_NAME_MISSING: 'It looks like we don\'t have your given name. You can set your given name through the Alexa App.',
NUMBER_MISSING: 'It looks like we don\'t have your phone number. You can set your phone number through the Alexa App.',
NAME_AVAILABLE: 'Your full name is: ',
GIVEN_NAME_AVAILABLE: 'Your given name is: ',
ABOUT_ACCOLITE: 'Accolite Digital is an innovative, best-in-class digital transformation services provider, successfully delivering design driven complex digital transformation initiatives to Fortune 500 clients. Accolite Digital is hiring! Apply to our roles by saying send my profile to accolite',
WELCOME_ACCOLITE: 'Welcome to Accolite, what would you like to do today?',
ERROR: 'Uh Oh. Looks like something went wrong.',
API_FAILURE: 'There was an error with the Alexa Person Profile API. Please try again.',
GOODBYE: 'Bye! Thanks for using the Sample Alexa Person Profile API Skill!',
UNHANDLED: 'This skill doesn\'t support that. Please ask something else.',
};
const PERMISSIONS = ['alexa::profile:mobile_number:read', 'alexa::profile:name:read', 'alexa::profile:given_name:read'];
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(messages.WELCOME_ACCOLITE)
.reprompt(messages.WELCOME_ACCOLITE)
.getResponse();
}
/* async handle(handlerInput) {
const person = handlerInput.requestEnvelope.context.System.person;
const consentToken = handlerInput.requestEnvelope.context.System.apiAccessToken;
if (person) {
const personId = person.personId;
console.log("Received personId: ", personId);
} else {
return handlerInput.responseBuilder
.speak(messages.PROFILE_NOT_RECOGNIZED)
.reprompt(messages.PROFILE_NOT_RECOGNIZED)
.getResponse();
}
try {
const client = handlerInput.serviceClientFactory.getUpsServiceClient();
const name = await client.getPersonsProfileName();
console.log('Name successfully retrieved, now responding to user.' + name);
let response;
if (name == null) {
response = handlerInput.responseBuilder.speak(messages.NAME_MISSING)
.getResponse();
} else {
const speechText = messages.WELCOME_ACCOLITE.format(name);
response = handlerInput.responseBuilder.speak(speechText)
.getResponse();
}
return response;
} catch (error) {
if (error.name !== 'ServiceError') {
const response = handlerInput.responseBuilder.speak(messages.ERROR).getResponse();
return response;
}
throw error;
}
}*/
};
const AboutAccoliteIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AboutAccoliteIntent';
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(messages.ABOUT_ACCOLITE)
.reprompt(messages.ABOUT_ACCOLITE)
.getResponse();
}
};
const SendProfileIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'SendProfileIntent';
},
handle(handlerInput) {
const speakOutput = 'You can say send profile or know more about accolite or even listen to our CEO! How can I help?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
}
const HelloWorldIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
},
async handle(handlerInput) {
const person = handlerInput.requestEnvelope.context.System.person;
const consentToken = handlerInput.requestEnvelope.context.System.apiAccessToken;
if (person) {
const personId = person.personId;
console.log("Received personId: ", personId);
} else {
return handlerInput.responseBuilder
.speak(messages.PROFILE_NOT_RECOGNIZED)
.reprompt(messages.PROFILE_NOT_RECOGNIZED)
.getResponse();
}
try {
const client = handlerInput.serviceClientFactory.getUpsServiceClient();
const name = await client.getPersonsProfileName();
console.log('Name successfully retrieved, now responding to user.' + name);
let response;
if (name == null) {
response = handlerInput.responseBuilder.speak(messages.NAME_MISSING)
.getResponse();
} else {
const speechText = messages.WELCOME_ACCOLITE.format(name);
response = handlerInput.responseBuilder.speak(speechText)
.getResponse();
}
return response;
} catch (error) {
if (error.name !== 'ServiceError') {
const response = handlerInput.responseBuilder.speak(messages.ERROR).getResponse();
return response;
}
throw error;
}
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speakOutput = 'You can say send profile or know more about accolite or even listen to our CEO! How can I help?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const speakOutput = 'Goodbye!';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
/* *
* FallbackIntent triggers when a customer says something that doesn’t map to any intents in your skill
* It must also be defined in the language model (if the locale supports it)
* This handler can be safely added but will be ingnored in locales that do not support it yet
* */
const FallbackIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent';
},
handle(handlerInput) {
const speakOutput = 'Sorry, I don\'t know about that. Please try again.';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
/* *
* SessionEndedRequest notifies that a session was ended. This handler will be triggered when a currently open
* session is closed for one of the following reasons: 1) The user says "exit" or "quit". 2) The user does not
* respond or says something that does not match an intent defined in your voice model. 3) An error occurs
* */
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`~~~~ Session ended: ${JSON.stringify(handlerInput.requestEnvelope)}`);
// Any cleanup logic goes here.
return handlerInput.responseBuilder.getResponse(); // notice we send an empty response
}
};
/* *
* The intent reflector is used for interaction model testing and debugging.
* It will simply repeat the intent the user said. You can create custom handlers for your intents
* by defining them above, then also adding them to the request handler chain below
* */
const IntentReflectorHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
},
handle(handlerInput) {
const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
const speakOutput = `You just triggered ${intentName}`;
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.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) {
const speakOutput = 'Sorry, I had trouble doing what you asked. Please try again.';
console.log(`~~~~ Error handled: ${JSON.stringify(error)}`);
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const ProfileError = {
canHandle(handlerInput, error) {
return error.name === 'ServiceError';
},
handle(handlerInput, error) {
if (error.statusCode === 403) {
return handlerInput.responseBuilder
.speak(messages.NOTIFY_MISSING_PERMISSIONS)
.withAskForPermissionsConsentCard(PERMISSIONS)
.getResponse();
}
return handlerInput.responseBuilder
.speak(messages.API_FAILURE)
.reprompt(messages.API_FAILURE)
.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,
HelloWorldIntentHandler,
AboutAccoliteIntentHandler,
SendProfileIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
FallbackIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler)
.addErrorHandlers(
ErrorHandler,
ProfileError)
.withCustomUserAgent('sample/accolite/v1.2')
.withApiClient(new Alexa.DefaultApiClient())
.lambda();