/
RequestEnvelopeUtils.ts
248 lines (227 loc) · 9.17 KB
/
RequestEnvelopeUtils.ts
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
/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import {
IntentRequest,
Request,
RequestEnvelope,
Session,
Slot,
SupportedInterfaces,
} from 'ask-sdk-model';
import { createAskSdkError } from 'ask-sdk-runtime';
/**
* Retrieves the locale from the request.
*
* The method returns the locale value present in the request. More information about the locale can be found
* here: https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#request-locale
*
* @param {RequestEnvelope} requestEnvelope
* @return {string}
*/
export function getLocale(requestEnvelope : RequestEnvelope) : string {
return requestEnvelope.request.locale;
}
/**
* Retrieves the type of the request.
*
* The method retrieves the request type of the input request. More information about the different request
* types are mentioned here:
* https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#request-body-parameters
*
* @param {RequestEnvelope} requestEnvelope
* @return {string}
*/
export function getRequestType(requestEnvelope : RequestEnvelope) : string {
return requestEnvelope.request.type;
}
/**
* Retrieves the name of the intent from the request.
*
* The method retrieves the intent name from the input request, only if the input request is an {@link IntentRequest}.
*
* @param {RequestEnvelope} requestEnvelope
* @return {string}
*/
export function getIntentName(requestEnvelope : RequestEnvelope) : string {
if (getRequestType(requestEnvelope) === 'IntentRequest') {
return (requestEnvelope.request as IntentRequest).intent.name;
}
throw createAskSdkError(
'RequestEnvelopeUtils',
`Expecting request type of IntentRequest but got ${getRequestType(requestEnvelope)}.`);
}
/**
* Get request object.
*
* We can set a specific type to the response by using the generics
* @param {RequestEnvelope} requestEnvelope
* @return {Request}
* @example
* ```
* const intentRequest = getRequest<IntentRequest>(requestEnvelope)
* console.log(intentRequest.intent.name)
* ```
*/
export function getRequest <T extends Request> (requestEnvelope : RequestEnvelope) : T {
return requestEnvelope.request as T;
}
/**
* Retrieves the account linking access token from the request.
*
* The method retrieves the user's accessToken from the input request. Once a user successfully enables a skill
* and links their Alexa account to the skill, the input request will have the user's access token. A null value
* is returned if there is no access token in the input request. More information on this can be found here:
* https://developer.amazon.com/docs/account-linking/add-account-linking-logic-custom-skill.html
*
* @param {RequestEnvelope} requestEnvelope
* @return {string}
*/
export function getAccountLinkingAccessToken(requestEnvelope : RequestEnvelope) : string {
return requestEnvelope.context.System.user.accessToken;
}
/**
* Retrieves the API access token from the request.
*
* The method retrieves the apiAccessToken from the input request, which has the encapsulated information of
* permissions granted by the user. This token can be used to call Alexa-specific APIs. More information
* about this can be found here:
* https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#system-object
*
* The SDK automatically injects this value into service client instances retrieved from the {@link services.ServiceClientFactory}
*
* @param {RequestEnvelope} requestEnvelope
* @return {string}
*/
export function getApiAccessToken(requestEnvelope : RequestEnvelope) : string {
return requestEnvelope.context.System.apiAccessToken;
}
/**
* Retrieves the device ID from the request.
*
* The method retrieves the deviceId property from the input request. This value uniquely identifies the device
* and is generally used as input for some Alexa-specific API calls. More information about this can be found here:
* https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#system-object
*
* @param {RequestEnvelope} requestEnvelope
* @return {string}
*/
export function getDeviceId(requestEnvelope : RequestEnvelope) : string {
return requestEnvelope.context.System.device ? requestEnvelope.context.System.device.deviceId : null;
}
/**
* Retrieves the user ID from the request.
*
* The method retrieves the userId property from the input request. This value uniquely identifies the user
* and is generally used as input for some Alexa-specific API calls. More information about this can be found here:
* https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#system-object
*
* @param {RequestEnvelope} requestEnvelope
* @return {string}
*/
export function getUserId(requestEnvelope : RequestEnvelope) : string {
return requestEnvelope.context.System.user ? requestEnvelope.context.System.user.userId : null;
}
/**
* Retrieves the dialog state from the request.
*
* The method retrieves the `dialogState` from the intent request, if the skill's interaction model includes a
* dialog model. This can be used to determine the current status of user conversation and return the appropriate
* dialog directives if the conversation is not yet complete. More information on dialog management can be found here:
* https://developer.amazon.com/docs/custom-skills/define-the-dialog-to-collect-and-confirm-required-information.html
*
* @param {RequestEnvelope} requestEnvelope
* @return {string}
*/
export function getDialogState(requestEnvelope : RequestEnvelope) : string {
if (getRequestType(requestEnvelope) === 'IntentRequest') {
return (requestEnvelope.request as IntentRequest).dialogState;
}
throw createAskSdkError(
'RequestEnvelopeUtils',
`Expecting request type of IntentRequest but got ${getRequestType(requestEnvelope)}.`);
}
/**
* Returns the {@link Slot} for the given slot name from the request.
*
* This method attempts to retrieve the requested {@link Slot} from the incoming request. If the slot does not
* exist in the request, a null value will be returned.
*
* @param {RequestEnvelope} requestEnvelope
* @param {string} slotName
* @return {Slot}
*/
export function getSlot(requestEnvelope : RequestEnvelope, slotName : string) : Slot {
if (getRequestType(requestEnvelope) === 'IntentRequest') {
const slots : {[key : string] : Slot} = (requestEnvelope.request as IntentRequest).intent.slots;
if (slots != null) {
return slots[slotName];
}
return null;
}
throw createAskSdkError(
'RequestEnvelopeUtils',
`Expecting request type of IntentRequest but got ${getRequestType(requestEnvelope)}.`);
}
/**
* Returns the value from the given {@link Slot} in the request.
*
* This method attempts to retrieve the requested {@link Slot}'s value from the incoming request. If the slot does not
* exist in the request, a null will be returned.
*
* @param {RequestEnvelope} requestEnvelope
* @param {string} slotName
* @return {string}
*/
export function getSlotValue(requestEnvelope : RequestEnvelope, slotName : string) : string {
if (getRequestType(requestEnvelope) === 'IntentRequest') {
const slot = getSlot(requestEnvelope, slotName);
if (slot) {
return slot.value;
}
return null;
}
throw createAskSdkError(
'RequestEnvelopeUtils',
`Expecting request type of IntentRequest but got ${getRequestType(requestEnvelope)}.`);
}
/**
* Retrieves the {@link SupportedInterfaces} from the request.
*
* This method returns an object listing each interface that the device supports. For example, if
* supportedInterfaces includes AudioPlayer, then you know that the device supports streaming audio using the
* AudioPlayer interface.
*
* @param {RequestEnvelope} requestEnvelope
* @return {SupportedInterfaces}
*/
export function getSupportedInterfaces(requestEnvelope : RequestEnvelope) : SupportedInterfaces {
return requestEnvelope.context.System.device.supportedInterfaces;
}
/**
* Returns whether the request is a new session.
*
* The method retrieves the new value from the input request's session, which indicates if it's a new session or
* not. More information can be found here :
* https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#session-object
*
* @param requestEnvelope
*/
export function isNewSession(requestEnvelope : RequestEnvelope) : boolean {
const session : Session = requestEnvelope.session;
if (session) {
return session.new;
}
throw createAskSdkError(
'RequestEnvelopeUtils',
`The provided request doesn't contain a session.`);
}