UNPKG

12.2 kBTypeScriptView Raw
1/**
2 * Copyright 2018 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import * as Api from '../api/v2';
17import { Headers } from '../../../framework';
18import { Surface, Available } from './surface';
19import { User } from './user';
20import { Image, RichResponse, Suggestions, RichResponseItem, MediaObject, SimpleResponse } from './response';
21import { Helper } from './helper';
22import { Arguments } from './argument';
23import { Device } from './device';
24import { Input } from './input';
25import { JsonObject } from '../../../common';
26import { ServiceBaseApp, AppOptions } from '../../../assistant';
27import { OAuth2Client } from 'google-auth-library';
28import { Canvas } from './canvas';
29/** @public */
30export declare type Intent = 'actions.intent.MAIN' | 'actions.intent.TEXT' | 'actions.intent.PERMISSION' | 'actions.intent.OPTION' | 'actions.intent.TRANSACTION_REQUIREMENTS_CHECK' | 'actions.intent.DELIVERY_ADDRESS' | 'actions.intent.TRANSACTION_DECISION' | 'actions.intent.CONFIRMATION' | 'actions.intent.DATETIME' | 'actions.intent.SIGN_IN' | 'actions.intent.NO_INPUT' | 'actions.intent.CANCEL' | 'actions.intent.NEW_SURFACE' | 'actions.intent.REGISTER_UPDATE' | 'actions.intent.CONFIGURE_UPDATES' | 'actions.intent.PLACE' | 'actions.intent.LINK' | 'actions.intent.MEDIA_STATUS' | 'actions.intent.COMPLETE_PURCHASE' | 'actions.intent.DIGITAL_PURCHASE_CHECK';
31/** @hidden */
32export declare type InputValueSpec = 'type.googleapis.com/google.actions.v2.PermissionValueSpec' | 'type.googleapis.com/google.actions.v2.OptionValueSpec' | 'type.googleapis.com/google.actions.v2.TransactionRequirementsCheckSpec' | 'type.googleapis.com/google.actions.v2.DeliveryAddressValueSpec' | 'type.googleapis.com/google.actions.v2.TransactionDecisionValueSpec' | 'type.googleapis.com/google.actions.v2.ConfirmationValueSpec' | 'type.googleapis.com/google.actions.v2.DateTimeValueSpec' | 'type.googleapis.com/google.actions.v2.NewSurfaceValueSpec' | 'type.googleapis.com/google.actions.v2.RegisterUpdateValueSpec' | 'type.googleapis.com/google.actions.v2.SignInValueSpec' | 'type.googleapis.com/google.actions.v2.PlaceValueSpec' | 'type.googleapis.com/google.actions.v2.LinkValueSpec' | 'type.googleapis.com/google.actions.transactions.v3.CompletePurchaseValueSpec' | 'type.googleapis.com/google.actions.transactions.v3.TransactionDecisionValueSpec' | 'type.googleapis.com/google.actions.transactions.v3.TransactionRequirementsCheckSpec' | 'type.googleapis.com/google.actions.transactions.v3.DigitalPurchaseCheckSpec';
33/** @hidden */
34export declare type DialogSpec = 'type.googleapis.com/google.actions.v2.PlaceValueSpec.PlaceDialogSpec' | 'type.googleapis.com/google.actions.v2.LinkValueSpec.LinkDialogSpec';
35/** @public */
36export declare type Response = RichResponse | RichResponseItem | Image | Suggestions | MediaObject | Helper<Intent, JsonObject>;
37/** @hidden */
38export interface ConversationResponse {
39 richResponse: Api.GoogleActionsV2RichResponse;
40 expectUserResponse: boolean;
41 userStorage: string;
42 expectedIntent?: Api.GoogleActionsV2ExpectedIntent;
43 noInputPrompts?: Api.GoogleActionsV2SimpleResponse[];
44 speechBiasingHints?: string[];
45}
46export interface ConversationOptionsInit<TConvData, TUserStorage> {
47 /** @public */
48 data?: TConvData;
49 /** @public */
50 storage?: TUserStorage;
51}
52/** @hidden */
53export interface ConversationBaseOptions<TConvData, TUserStorage> {
54 /** @public */
55 headers?: Headers;
56 /** @public */
57 init?: ConversationOptionsInit<TConvData, TUserStorage>;
58 /** @public */
59 debug?: boolean;
60 /** @public */
61 ordersv3?: boolean;
62}
63/** @hidden */
64export interface ConversationOptions<TUserStorage> {
65 /** @public */
66 request?: Api.GoogleActionsV2AppRequest;
67 /** @public */
68 headers?: Headers;
69 /** @public */
70 init?: ConversationOptionsInit<{}, TUserStorage>;
71 /** @public */
72 ordersv3?: boolean;
73}
74/**
75 * Throw an UnauthorizedError in an intent handler to make the library
76 * respond with a HTTP 401 Status Code.
77 *
78 * @example
79 * ```javascript
80 * const app = dialogflow()
81 *
82 * // If using Actions SDK:
83 * // const app = actionssdk()
84 *
85 * app.intent('intent', conv => {
86 * // ...
87 *
88 * // given a function to check if a user auth is still valid
89 * const valid = checkUserAuthValid(conv)
90 * if (!valid) {
91 * throw new UnauthorizedError()
92 * }
93 *
94 * // ...
95 * })
96 *
97 * ```
98 *
99 * @public
100 */
101export declare class UnauthorizedError extends Error {
102}
103/** @public */
104export declare class Conversation<TUserStorage> {
105 /** @public */
106 request: Api.GoogleActionsV2AppRequest;
107 /** @public */
108 headers: Headers;
109 /** @public */
110 responses: Response[];
111 /** @public */
112 expectUserResponse: boolean;
113 /** @public */
114 surface: Surface;
115 /** @public */
116 available: Available;
117 /** @public */
118 digested: boolean;
119 /**
120 * True if the app is being tested in sandbox mode. Enable sandbox
121 * mode in the [Actions console](console.actions.google.com) to test
122 * transactions.
123 * @public
124 */
125 sandbox: boolean;
126 /** @public */
127 input: Input;
128 /**
129 * Gets the {@link User} object.
130 * The user object contains information about the user, including
131 * a string identifier and personal information (requires requesting permissions,
132 * see {@link Permission|conv.ask(new Permission)}).
133 * @public
134 */
135 user: User<TUserStorage>;
136 /** @public */
137 arguments: Arguments;
138 /** @public */
139 device: Device;
140 /** @public */
141 canvas: Canvas;
142 /**
143 * Gets the unique conversation ID. It's a new ID for the initial query,
144 * and stays the same until the end of the conversation.
145 *
146 * @example
147 * ```javascript
148 *
149 * app.intent('actions.intent.MAIN', conv => {
150 * const conversationId = conv.id
151 * })
152 * ```
153 *
154 * @public
155 */
156 id: string;
157 /** @public */
158 type: Api.GoogleActionsV2ConversationType;
159 /**
160 * Shortcut for
161 * {@link Capabilities|conv.surface.capabilities.has('actions.capability.SCREEN_OUTPUT')}
162 * @public
163 */
164 screen: boolean;
165 /**
166 * Set reprompts when users don't provide input to this action (no-input errors).
167 * Each reprompt represents as the {@link SimpleResponse}, but raw strings also can be specified
168 * for convenience (they're passed to the constructor of {@link SimpleResponse}).
169 * Notice that this value is not kept over conversations. Thus, it is necessary to set
170 * the reprompts per each conversation response.
171 *
172 * @example
173 * ```javascript
174 *
175 * app.intent('actions.intent.MAIN', conv => {
176 * conv.noInputs = [
177 * 'Are you still there?',
178 * 'Hello?',
179 * new SimpleResponse({
180 * text: 'Talk to you later. Bye!',
181 * speech: '<speak>Talk to you later. Bye!</speak>'
182 * })
183 * ]
184 * conv.ask('What's your favorite color?')
185 * })
186 * ```
187 *
188 * @public
189 */
190 noInputs: (string | SimpleResponse)[];
191 /**
192 * Sets speech biasing options.
193 *
194 * @example
195 * ``` javascript
196 *
197 * app.intent('actions.intent.MAIN', conv => {
198 * conv.speechBiasing = ['red', 'blue', 'green']
199 * conv.ask('What is your favorite color out of red, blue, and green?')
200 * })
201 * ```
202 *
203 * @public
204 */
205 speechBiasing: string[];
206 /** @hidden */
207 _raw?: JsonObject;
208 /** @hidden */
209 _responded: boolean;
210 /** @hidden */
211 _init: ConversationOptionsInit<{}, TUserStorage>;
212 /** @hidden */
213 _ordersv3: boolean;
214 /** @hidden */
215 constructor(options?: ConversationOptions<TUserStorage>);
216 /** @public */
217 json<T = JsonObject>(json: T): this;
218 /** @public */
219 add(...responses: Response[]): this;
220 /**
221 * Asks to collect user's input. All user's queries need to be sent to the app.
222 * {@link https://developers.google.com/actions/policies/general-policies#user_experience|
223 * The guidelines when prompting the user for a response must be followed at all times}.
224 *
225 * @example
226 * ```javascript
227 *
228 * // Actions SDK
229 * const app = actionssdk()
230 *
231 * app.intent('actions.intent.MAIN', conv => {
232 * const ssml = '<speak>Hi! <break time="1"/> ' +
233 * 'I can read out an ordinal like <say-as interpret-as="ordinal">123</say-as>. ' +
234 * 'Say a number.</speak>'
235 * conv.ask(ssml)
236 * })
237 *
238 * app.intent('actions.intent.TEXT', (conv, input) => {
239 * if (input === 'bye') {
240 * return conv.close('Goodbye!')
241 * }
242 * const ssml = `<speak>You said, <say-as interpret-as="ordinal">${input}</say-as></speak>`
243 * conv.ask(ssml)
244 * })
245 *
246 * // Dialogflow
247 * const app = dialogflow()
248 *
249 * app.intent('Default Welcome Intent', conv => {
250 * conv.ask('Welcome to action snippets! Say a number.')
251 * })
252 *
253 * app.intent('Number Input', (conv, {num}) => {
254 * conv.close(`You said ${num}`)
255 * })
256 * ```
257 *
258 * @param responses A response fragment for the library to construct a single complete response
259 * @public
260 */
261 ask(...responses: Response[]): this;
262 /**
263 * Have Assistant render the speech response and close the mic.
264 *
265 * @example
266 * ```javascript
267 *
268 * // Actions SDK
269 * const app = actionssdk()
270 *
271 * app.intent('actions.intent.MAIN', conv => {
272 * const ssml = '<speak>Hi! <break time="1"/> ' +
273 * 'I can read out an ordinal like <say-as interpret-as="ordinal">123</say-as>. ' +
274 * 'Say a number.</speak>'
275 * conv.ask(ssml)
276 * })
277 *
278 * app.intent('actions.intent.TEXT', (conv, input) => {
279 * if (input === 'bye') {
280 * return conv.close('Goodbye!')
281 * }
282 * const ssml = `<speak>You said, <say-as interpret-as="ordinal">${input}</say-as></speak>`
283 * conv.ask(ssml)
284 * })
285 *
286 * // Dialogflow
287 * const app = dialogflow()
288 *
289 * app.intent('Default Welcome Intent', conv => {
290 * conv.ask('Welcome to action snippets! Say a number.')
291 * })
292 *
293 * app.intent('Number Input', (conv, {num}) => {
294 * conv.close(`You said ${num}`)
295 * })
296 * ```
297 *
298 * @param responses A response fragment for the library to construct a single complete response
299 * @public
300 */
301 close(...responses: Response[]): this;
302 /** @public */
303 response(): ConversationResponse;
304}
305export interface ExceptionHandler<TUserStorage, TConversation extends Conversation<TUserStorage>> {
306 /** @public */
307 (conv: TConversation, error: Error): Promise<any> | any;
308}
309/** @hidden */
310export interface Traversed {
311 [key: string]: boolean;
312}
313/** @hidden */
314export interface ConversationAppOptions<TConvData, TUserStorage> extends AppOptions {
315 /** @public */
316 init?: () => ConversationOptionsInit<TConvData, TUserStorage>;
317 /**
318 * Client ID for User Profile Payload Verification
319 * See {@link Profile#payload|conv.user.profile.payload}
320 * @public
321 */
322 clientId?: string;
323 /** @public */
324 ordersv3?: boolean;
325}
326export interface OAuth2ConfigClient {
327 /** @public */
328 id: string;
329}
330export interface OAuth2Config {
331 /** @public */
332 client: OAuth2ConfigClient;
333}
334export interface ConversationApp<TConvData, TUserStorage> extends ServiceBaseApp {
335 /** @public */
336 init?: () => ConversationOptionsInit<TConvData, TUserStorage>;
337 /** @public */
338 auth?: OAuth2Config;
339 /** @public */
340 ordersv3: boolean;
341 /** @hidden */
342 _client?: OAuth2Client;
343}