UNPKG

7.18 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 { AppHandler } from '../../assistant';
18import { ExceptionHandler, Argument, Intent, ConversationAppOptions, ConversationApp } from './conversation';
19import { ActionsSdkConversation } from './conv';
20import { BuiltinFrameworkMetadata } from '../../framework';
21/** @public */
22export interface ActionsSdkIntentHandler<TConvData, TUserStorage, TConversation extends ActionsSdkConversation<TConvData, TUserStorage>, TArgument extends Argument> {
23 /** @public */
24 (conv: TConversation,
25 /**
26 * The user's raw input query.
27 * See {@link Input#raw|Input.raw}
28 * Same as `conv.input.raw`
29 */
30 input: string,
31 /**
32 * The first argument value from the current intent.
33 * See {@link Arguments#get|Arguments.get}
34 * Same as `conv.arguments.parsed.list[0]`
35 */
36 argument: TArgument,
37 /**
38 * The first argument status from the current intent.
39 * See {@link Arguments#status|Arguments.status}
40 * Same as `conv.arguments.status.list[0]`
41 */
42 status: Api.GoogleRpcStatus | undefined): Promise<any> | any;
43}
44/** @hidden */
45export interface ActionSdkIntentHandlers {
46 [intent: string]: ActionsSdkIntentHandler<{}, {}, ActionsSdkConversation<{}, {}>, Argument> | string | undefined;
47}
48/** @hidden */
49export interface ActionsSdkHandlers<TConvData, TUserStorage, TConversation extends ActionsSdkConversation<TConvData, TUserStorage>> {
50 intents: ActionSdkIntentHandlers;
51 catcher: ExceptionHandler<TUserStorage, TConversation>;
52 fallback?: ActionsSdkIntentHandler<{}, {}, ActionsSdkConversation<{}, {}>, Argument> | string;
53}
54/** @public */
55export interface ActionsSdkMiddleware<TConversationPlugin extends ActionsSdkConversation<{}, {}>> {
56 /** @public */
57 (
58 /** @public */
59 conv: ActionsSdkConversation<{}, {}>,
60 /** @public */
61 framework: BuiltinFrameworkMetadata): (ActionsSdkConversation<{}, {}> & TConversationPlugin) | void | Promise<ActionsSdkConversation<{}, {}> & TConversationPlugin> | Promise<void>;
62}
63/** @public */
64export interface ActionsSdkApp<TConvData, TUserStorage, TConversation extends ActionsSdkConversation<TConvData, TUserStorage>> extends ConversationApp<TConvData, TUserStorage> {
65 /** @hidden */
66 _handlers: ActionsSdkHandlers<TConvData, TUserStorage, TConversation>;
67 /**
68 * Sets the IntentHandler to be executed when the fulfillment is called
69 * with a given Actions SDK intent.
70 *
71 * @param intent The Actions SDK intent to match.
72 * When given an array, sets the IntentHandler for any intent in the array.
73 * @param handler The IntentHandler to be executed when the intent is matched.
74 * When given a string instead of a function, the intent fulfillment will be redirected
75 * to the IntentHandler of the redirected intent.
76 * @public
77 */
78 intent<TArgument extends Argument>(intent: Intent | Intent[], handler: ActionsSdkIntentHandler<TConvData, TUserStorage, TConversation, TArgument> | Intent): this;
79 /**
80 * Sets the IntentHandler to be executed when the fulfillment is called
81 * with a given Actions SDK intent.
82 *
83 * @param intent The Actions SDK intent to match.
84 * When given an array, sets the IntentHandler for any intent in the array.
85 * @param handler The IntentHandler to be executed when the intent is matched.
86 * When given a string instead of a function, the intent fulfillment will be redirected
87 * to the IntentHandler of the redirected intent.
88 * @public
89 */
90 intent<TArgument extends Argument>(intent: string | string[], handler: ActionsSdkIntentHandler<TConvData, TUserStorage, TConversation, TArgument> | string): this;
91 /** @public */
92 catch(catcher: ExceptionHandler<TUserStorage, TConversation>): this;
93 /** @public */
94 fallback(handler: ActionsSdkIntentHandler<TConvData, TUserStorage, TConversation, Argument> | string): this;
95 /** @hidden */
96 _middlewares: ActionsSdkMiddleware<ActionsSdkConversation<{}, {}>>[];
97 /** @public */
98 middleware<TConversationPlugin extends ActionsSdkConversation<{}, {}>>(middleware: ActionsSdkMiddleware<TConversationPlugin>): this;
99 /** @public */
100 verification?: ActionsSdkVerification | string;
101}
102/** @public */
103export interface ActionsSdk {
104 /** @public */
105 <TConvData, TUserStorage, Conversation extends ActionsSdkConversation<TConvData, TUserStorage> = ActionsSdkConversation<TConvData, TUserStorage>>(options?: ActionsSdkOptions<TConvData, TUserStorage>): AppHandler & ActionsSdkApp<TConvData, TUserStorage, Conversation>;
106 /** @public */
107 <Conversation extends ActionsSdkConversation<{}, {}> = ActionsSdkConversation<{}, {}>>(options?: ActionsSdkOptions<{}, {}>): AppHandler & ActionsSdkApp<{}, {}, Conversation>;
108}
109/** @public */
110export interface ActionsSdkVerification {
111 /**
112 * Google Cloud Project ID for the Assistant app.
113 * @public
114 */
115 project: string;
116 /**
117 * Custom status code to return on verification error.
118 * @public
119 */
120 status?: number;
121 /**
122 * Custom error message as a string or a function that returns a string
123 * given the original error message set by the library.
124 *
125 * The message will get sent back in the JSON top level `error` property.
126 * @public
127 */
128 error?: string | ((error: string) => string);
129}
130/** @public */
131export interface ActionsSdkOptions<TConvData, TUserStorage> extends ConversationAppOptions<TConvData, TUserStorage> {
132 /**
133 * Validates whether request is from Google through signature verification.
134 * Uses Google-Auth-Library to verify authorization token against given Google Cloud Project ID.
135 * Auth token is given in request header with key, "authorization".
136 *
137 * HTTP Code 403 will be thrown by default on verification error.
138 *
139 * @example
140 * ```javascript
141 *
142 * const app = actionssdk({ verification: 'nodejs-cloud-test-project-1234' })
143 * ```
144 *
145 * @public
146 */
147 verification?: ActionsSdkVerification | string;
148}
149/**
150 * This is the function that creates the app instance which on new requests,
151 * creates a way to interact with the conversation API directly from Assistant,
152 * providing implementation for all the methods available in the API.
153 *
154 * Only supports Actions SDK v2.
155 *
156 * @example
157 * ```javascript
158 *
159 * const app = actionssdk()
160 *
161 * app.intent('actions.intent.MAIN', conv => {
162 * conv.ask('How are you?')
163 * })
164 * ```
165 *
166 * @public
167 */
168export declare const actionssdk: ActionsSdk;
169
\No newline at end of file