forked from actions-on-google/actions-on-google-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest-extractor.js
408 lines (382 loc) · 12.4 KB
/
request-extractor.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* Copyright 2017 Google Inc. 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.
*/
'use strict';
const Debug = require('debug');
const debug = Debug('actions-on-google:debug');
const error = Debug('actions-on-google:error');
const { transformToSnakeCase } = require('./utils/transform');
/**
* The Actions on Google client library RequestExtractor.
*
* This class contains the methods to extract data from the request object.
*/
class RequestExtractor {
/**
* @param {function(): *} requestData Function that returns the
* request data object to be processed.
*/
constructor (requestData) {
this.requestData = requestData;
}
/**
* Get every method in RequestExtractor that doesn't exist in this class
* and put it in the app class.
* @param {AssistantApp} app AssistantApp instance to inject methods into.
*/
inject (app) {
this.app = app;
const target = Object.getPrototypeOf(this.app);
for (const method of Object.getOwnPropertyNames(Object.getPrototypeOf(this))) {
if (target.hasOwnProperty(method)) {
continue;
}
this.app[method] = this[method].bind(this);
}
}
/**
* Gets the {@link User} object.
* The user object contains information about the user, including
* a string identifier and personal information (requires requesting permissions,
* see {@link AssistantApp#askForPermissions|askForPermissions}).
*
* @example
* const app = new ApiAiApp({request: request, response: response});
* or
* const app = new ActionsSdkApp({request: request, response: response});
* const userId = app.getUser().userId;
*
* @return {User} Null if no value.
* @requestextractor
*/
getUser () {
debug('getUser');
const data = this.requestData();
if (!data || !data.user) {
this.app.handleError_('No user object');
return null;
}
const requestUser = data.user;
// User object includes original API properties
const user = Object.assign({}, requestUser);
// Backwards compatibility
user.user_id = user.userId;
user.access_token = user.accessToken;
const profile = user.profile;
user.userName = profile ? Object.assign({}, profile) : null;
return user;
}
/**
* If granted permission to device's location in previous intent, returns device's
* location (see {@link AssistantApp#askForPermissions|askForPermissions}).
* If device info is unavailable, returns null.
*
* @example
* const app = new ApiAiApp({request: req, response: res});
* or
* const app = new ActionsSdkApp({request: req, response: res});
* app.askForPermission("To get you a ride",
* app.SupportedPermissions.DEVICE_PRECISE_LOCATION);
* // ...
* // In response handler for permissions fallback intent:
* if (app.isPermissionGranted()) {
* sendCarTo(app.getDeviceLocation().coordinates);
* }
*
* @return {DeviceLocation} Null if location permission is not granted.
* @requestextractor
*/
getDeviceLocation () {
debug('getDeviceLocation');
const data = this.requestData();
if (!data || !data.device || !data.device.location) {
return null;
}
const deviceLocation = Object.assign({}, data.device.location);
deviceLocation.address = deviceLocation.formattedAddress;
return deviceLocation;
}
/**
* Find argument with requirements
* @param {Array<string>} targets Argument to find
* @return {*} The argument
*/
findArgument_ (...targets) {
const data = this.requestData();
if (data && data.inputs) {
for (const input of data.inputs) {
if (input.arguments) {
for (const argument of input.arguments) {
for (const target of targets) {
if (argument.name === target) {
return argument;
}
}
}
}
}
}
return null;
}
/**
* Get the argument value by name from the current intent.
* If the argument is included in originalRequest, and is not a text argument,
* the entire argument object is returned.
*
* Note: If incoming request is using an API version under 2 (e.g. 'v1'),
* the argument object will be in Proto2 format (snake_case, etc).
*
* @example
* const app = new ApiAiApp({request: request, response: response});
* const WELCOME_INTENT = 'input.welcome';
* const NUMBER_INTENT = 'input.number';
*
* function welcomeIntent (app) {
* app.ask('Welcome to action snippets! Say a number.');
* }
*
* function numberIntent (app) {
* const number = app.getArgument(NUMBER_ARGUMENT);
* app.tell('You said ' + number);
* }
*
* const actionMap = new Map();
* actionMap.set(WELCOME_INTENT, welcomeIntent);
* actionMap.set(NUMBER_INTENT, numberIntent);
* app.handleRequest(actionMap);
*
* @param {string} argName Name of the argument.
* @return {Object} Argument value matching argName
* or null if no matching argument.
* @requestextractor
*/
getArgumentCommon (argName) {
debug('getArgument: argName=%s', argName);
if (!argName) {
this.app.handleError_('Invalid argument name');
return null;
}
const argument = this.findArgument_(argName);
if (!argument) {
debug('Failed to get argument value: %s', argName);
return null;
} else if (argument.textValue) {
return argument.textValue;
} else {
if (!this.app.isNotApiVersionOne_()) {
return transformToSnakeCase(argument);
} else {
return argument;
}
}
}
/**
* Gets transactability of user. Only use after calling
* askForTransactionRequirements. Null if no result given.
*
* @return {string} One of Transactions.ResultType.
* @requestextractor
*/
getTransactionRequirementsResult () {
debug('getTransactionRequirementsResult');
const argument = this.findArgument_(this.app.BuiltInArgNames.TRANSACTION_REQ_CHECK_RESULT);
if (argument && argument.extension && argument.extension.resultType) {
return argument.extension.resultType;
}
debug('Failed to get transaction requirements result');
return null;
}
/**
* Gets order delivery address. Only use after calling askForDeliveryAddress.
*
* @return {DeliveryAddress} Delivery address information. Null if user
* denies permission, or no address given.
* @requestextractor
*/
getDeliveryAddress () {
debug('getDeliveryAddress');
const {
DELIVERY_ADDRESS_VALUE,
TRANSACTION_DECISION_VALUE
} = this.app.BuiltInArgNames;
const argument = this.findArgument_(DELIVERY_ADDRESS_VALUE, TRANSACTION_DECISION_VALUE);
if (argument && argument.extension) {
if (argument.extension.userDecision === this.app.Transactions.DeliveryAddressDecision.ACCEPTED) {
const { location } = argument.extension;
if (!location.postalAddress) {
debug('User accepted, but may not have configured address in app');
return null;
}
return location;
} else {
debug('User rejected giving delivery address');
return null;
}
}
debug('Failed to get order delivery address');
return null;
}
/**
* Gets transaction decision information. Only use after calling
* askForTransactionDecision.
*
* @return {TransactionDecision} Transaction decision data. Returns object with
* userDecision only if user declines. userDecision will be one of
* Transactions.ConfirmationDecision. Null if no decision given.
* @requestextractor
*/
getTransactionDecision () {
debug('getTransactionDecision');
const argument = this.findArgument_(this.app.BuiltInArgNames.TRANSACTION_DECISION_VALUE);
if (argument && argument.extension) {
return argument.extension;
}
debug('Failed to get order decision information');
return null;
}
/**
* Gets confirmation decision. Use after askForConfirmation.
*
* False if user replied with negative response. Null if no user
* confirmation decision given.
* @requestextractor
*/
getUserConfirmation () {
debug('getUserConfirmation');
const argument = this.findArgument_(this.app.BuiltInArgNames.CONFIRMATION);
if (argument) {
return argument.boolValue;
}
debug('Failed to get confirmation decision information');
return null;
}
/**
* Gets user provided date and time. Use after askForDateTime.
*
* @return {DateTime} Date and time given by the user. Null if no user
* date and time given.
* @requestextractors
*/
getDateTime () {
debug('getDateTime');
const argument = this.findArgument_(this.app.BuiltInArgNames.DATETIME);
if (argument) {
return argument.datetimeValue;
}
debug('Failed to get date/time information');
return null;
}
/**
* Gets status of user sign in request.
*
* @return {string} Result of user sign in request. One of
* ApiAiApp.SignInStatus or ActionsSdkApp.SignInStatus
* Null if no sign in status.
* @requestextractor
*/
getSignInStatus () {
debug('getSignInStatus');
const argument = this.findArgument_(this.app.BuiltInArgNames.SIGN_IN);
if (argument && argument.extension && argument.extension.status) {
return argument.extension.status;
}
debug('Failed to get sign in status');
return null;
}
/**
* Returns true if the app is being tested in sandbox mode. Enable sandbox
* mode in the (Actions console)[console.actions.google.com] to test
* transactions.
*
* @return {boolean} True if app is being used in Sandbox mode.
* @requestextractor
*/
isInSandbox () {
const data = this.requestData();
return data && data.isInSandbox;
}
/**
* Gets surface capabilities of user device.
*
* @return {Array<string>} Supported surface capabilities, as defined in
* AssistantApp.SurfaceCapabilities.
* @apiai
*/
getSurfaceCapabilities () {
debug('getSurfaceCapabilities');
const data = this.requestData();
if (!data || !data.surface || !data.surface.capabilities) {
error('No surface capabilities in incoming request');
return null;
}
if (data && data.surface && data.surface.capabilities) {
return data.surface.capabilities.map(capability => capability.name);
} else {
error('No surface capabilities in incoming request');
return null;
}
}
/**
* Gets type of input used for this request.
*
* @return {number} One of ApiAiApp.InputTypes.
* Null if no input type given.
* @requestextractor
*/
getInputType () {
debug('getInputType');
const data = this.requestData();
if (data && data.inputs) {
for (const input of data.inputs) {
if (input.rawInputs) {
for (const rawInput of input.rawInputs) {
if (rawInput.inputType) {
return rawInput.inputType;
}
}
}
}
}
error('No input type in incoming request');
return null;
}
/**
* Returns true if the request follows a previous request asking for
* permission from the user and the user granted the permission(s). Otherwise,
* false. Use with {@link AssistantApp#askForPermissions|askForPermissions}.
*
* @example
* const app = new ActionsSdkApp({request: request, response: response});
* or
* const app = new ApiAiApp({request: request, response: response});
* app.askForPermissions("To get you a ride", [
* app.SupportedPermissions.NAME,
* app.SupportedPermissions.DEVICE_PRECISE_LOCATION
* ]);
* // ...
* // In response handler for subsequent intent:
* if (app.isPermissionGranted()) {
* // Use the requested permission(s) to get the user a ride
* }
*
* @return {boolean} true if permissions granted.
* @requestextractor
*/
isPermissionGranted () {
debug('isPermissionGranted');
return this.getArgumentCommon(this.app.BuiltInArgNames.PERMISSION_GRANTED) === 'true';
}
}
module.exports = RequestExtractor;