UNPKG

4.22 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2018 Google Inc. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.Permission = void 0;
19const helper_1 = require("../helper");
20const common_1 = require("../../../../../common");
21/**
22 * Asks the Assistant to guide the user to grant a permission. For example,
23 * if you want your app to get access to the user's name, you would invoke
24 * `conv.ask(new Permission)` with the context containing the reason for the request,
25 * and the {@link GoogleActionsV2PermissionValueSpecPermissions} permission.
26 * With this, the Assistant will ask the user, in your agent's voice,
27 * the following: '[Context with reason for the request],
28 * I'll just need to get your name from Google, is that OK?'.
29 *
30 * Once the user accepts or denies the request, the Assistant will fire another intent:
31 * `actions.intent.PERMISSION` with a boolean argument: `PERMISSION`
32 * and, if granted, the information that you requested.
33 *
34 * Notes for multiple permissions:
35 * * The order in which you specify the permission prompts does not matter -
36 * it is controlled by the Assistant to provide a consistent user experience.
37 * * The user will be able to either accept all permissions at once, or none.
38 * If you wish to allow them to selectively accept one or other, make several
39 * dialog turns asking for each permission independently with `conv.ask(new Permission)`.
40 * * Asking for `DEVICE_COARSE_LOCATION` and `DEVICE_PRECISE_LOCATION` at once is
41 * equivalent to just asking for `DEVICE_PRECISE_LOCATION`
42 *
43 * @example
44 * ```javascript
45 *
46 * // Actions SDK
47 * const app = actionssdk()
48 *
49 * app.intent('actions.intent.MAIN', conv => {
50 * conv.ask(new Permission({
51 * context: 'To read your mind',
52 * permissions: 'NAME',
53 * }))
54 * })
55 *
56 * app.intent('actions.intent.PERMISSION', (conv, input, granted) => {
57 * // granted: inferred first (and only) argument value, boolean true if granted, false if not
58 * const explicit = conv.arguments.get('PERMISSION') // also retrievable w/ explicit arguments.get
59 * const name = conv.user.name
60 * })
61 *
62 * // Dialogflow
63 * const app = dialogflow()
64 *
65 * app.intent('Default Welcome Intent', conv => {
66 * conv.ask(new Permission({
67 * context: 'To read your mind',
68 * permissions: 'NAME',
69 * }))
70 * })
71 *
72 * // Create a Dialogflow intent with the `actions_intent_PERMISSION` event
73 * app.intent('Get Permission', (conv, params, granted) => {
74 * // granted: inferred first (and only) argument value, boolean true if granted, false if not
75 * const explicit = conv.arguments.get('PERMISSION') // also retrievable w/ explicit arguments.get
76 * const name = conv.user.name
77 * })
78 * ```
79 *
80 * Read more:
81 * * {@link GoogleActionsV2PermissionValueSpecPermissions|Supported Permissions}
82 * * Check if the permission has been granted with `conv.arguments.get('PERMISSION')`
83 * * {@link Device#location|conv.device.location}
84 * * {@link User#name|conv.user.name}
85 * * {@link Place|conv.ask(new Place)} which also can ask for Location permission to get a place
86 * @public
87 */
88class Permission extends helper_1.SoloHelper {
89 /**
90 * @param options Permission options
91 * @public
92 */
93 constructor(options) {
94 super({
95 intent: 'actions.intent.PERMISSION',
96 type: 'type.googleapis.com/google.actions.v2.PermissionValueSpec',
97 data: {
98 optContext: options.context,
99 permissions: common_1.toArray(options.permissions),
100 ...options.extra,
101 },
102 });
103 }
104}
105exports.Permission = Permission;
106//# sourceMappingURL=permission.js.map
\No newline at end of file