UNPKG

10.8 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 ActionsApi from '../actionssdk/api/v2';
17import { AppHandler } from '../../assistant';
18import { ExceptionHandler, Argument, ConversationApp, ConversationAppOptions } from '../actionssdk';
19import { Contexts, Parameters } from './context';
20import { DialogflowConversation } from './conv';
21import { BuiltinFrameworkMetadata } from '../../framework';
22/** @public */
23export interface DialogflowIntentHandler<TConvData, TUserStorage, TContexts extends Contexts, TConversation extends DialogflowConversation<TConvData, TUserStorage, TContexts>, TParameters extends Parameters, TArgument extends Argument> {
24 /** @public */
25 (conv: TConversation, params: TParameters,
26 /**
27 * The first argument value from the current intent.
28 * See {@link Arguments#get|Arguments.get}
29 * Same as `conv.arguments.parsed.list[0]`
30 */
31 argument: TArgument,
32 /**
33 * The first argument status from the current intent.
34 * See {@link Arguments#status|Arguments.status}
35 * Same as `conv.arguments.status.list[0]`
36 */
37 status: ActionsApi.GoogleRpcStatus | undefined): Promise<any> | any;
38}
39/** @hidden */
40export interface DialogflowIntentHandlers {
41 [event: string]: Function | string | undefined;
42}
43/** @hidden */
44export interface DialogflowHandlers<TConvData, TUserStorage, TContexts extends Contexts, TConversation extends DialogflowConversation<TConvData, TUserStorage, TContexts>> {
45 intents: DialogflowIntentHandlers;
46 catcher: ExceptionHandler<TUserStorage, TConversation>;
47 fallback?: Function | string;
48}
49/** @public */
50export interface DialogflowMiddleware<TConversationPlugin extends DialogflowConversation> {
51 (
52 /** @public */
53 conv: DialogflowConversation,
54 /** @public */
55 framework: BuiltinFrameworkMetadata): (DialogflowConversation & TConversationPlugin) | void | Promise<DialogflowConversation & TConversationPlugin> | Promise<void>;
56}
57/** @public */
58export declare type DefaultDialogflowIntent = 'Default Welcome Intent' | 'Default Fallback Intent';
59/** @public */
60export interface DialogflowApp<TConvData, TUserStorage, TContexts extends Contexts, TConversation extends DialogflowConversation<TConvData, TUserStorage, TContexts>> extends ConversationApp<TConvData, TUserStorage> {
61 /** @hidden */
62 _handlers: DialogflowHandlers<TConvData, TUserStorage, TContexts, TConversation>;
63 /**
64 * Sets the IntentHandler to be execute when the fulfillment is called
65 * with a given Dialogflow intent name.
66 *
67 * @param intent The Dialogflow intent name to match.
68 * When given an array, sets the IntentHandler for any intent name in the array.
69 * @param handler The IntentHandler to be executed when the intent name is matched.
70 * When given a string instead of a function, the intent fulfillment will be redirected
71 * to the IntentHandler of the redirected intent name.
72 * @public
73 */
74 intent<TParameters extends Parameters>(intent: DefaultDialogflowIntent | DefaultDialogflowIntent[], handler: DialogflowIntentHandler<TConvData, TUserStorage, TContexts, TConversation, TParameters, Argument> | string): this;
75 /**
76 * Sets the IntentHandler to be execute when the fulfillment is called
77 * with a given Dialogflow intent name.
78 *
79 * @param intent The Dialogflow intent name to match.
80 * When given an array, sets the IntentHandler for any intent name in the array.
81 * @param handler The IntentHandler to be executed when the intent name is matched.
82 * When given a string instead of a function, the intent fulfillment will be redirected
83 * to the IntentHandler of the redirected intent name.
84 * @public
85 */
86 intent<TArgument extends Argument>(intent: DefaultDialogflowIntent | DefaultDialogflowIntent[], handler: DialogflowIntentHandler<TConvData, TUserStorage, TContexts, TConversation, Parameters, TArgument> | string): this;
87 /**
88 * Sets the IntentHandler to be execute when the fulfillment is called
89 * with a given Dialogflow intent name.
90 *
91 * @param intent The Dialogflow intent name to match.
92 * When given an array, sets the IntentHandler for any intent name in the array.
93 * @param handler The IntentHandler to be executed when the intent name is matched.
94 * When given a string instead of a function, the intent fulfillment will be redirected
95 * to the IntentHandler of the redirected intent name.
96 * @public
97 */
98 intent<TParameters extends Parameters, TArgument extends Argument>(intent: DefaultDialogflowIntent | DefaultDialogflowIntent[], handler: DialogflowIntentHandler<TConvData, TUserStorage, TContexts, TConversation, TParameters, TArgument> | string): this;
99 /**
100 * Sets the IntentHandler to be execute when the fulfillment is called
101 * with a given Dialogflow intent name.
102 *
103 * @param intent The Dialogflow intent name to match.
104 * When given an array, sets the IntentHandler for any intent name in the array.
105 * @param handler The IntentHandler to be executed when the intent name is matched.
106 * When given a string instead of a function, the intent fulfillment will be redirected
107 * to the IntentHandler of the redirected intent name.
108 * @public
109 */
110 intent<TParameters extends Parameters>(intent: string | string[], handler: DialogflowIntentHandler<TConvData, TUserStorage, TContexts, TConversation, TParameters, Argument> | string): this;
111 /**
112 * Sets the IntentHandler to be execute when the fulfillment is called
113 * with a given Dialogflow intent name.
114 *
115 * @param intent The Dialogflow intent name to match.
116 * When given an array, sets the IntentHandler for any intent name in the array.
117 * @param handler The IntentHandler to be executed when the intent name is matched.
118 * When given a string instead of a function, the intent fulfillment will be redirected
119 * to the IntentHandler of the redirected intent name.
120 * @public
121 */
122 intent<TArgument extends Argument>(intent: string | string[], handler: DialogflowIntentHandler<TConvData, TUserStorage, TContexts, TConversation, Parameters, TArgument> | string): this;
123 /**
124 * Sets the IntentHandler to be execute when the fulfillment is called
125 * with a given Dialogflow intent name.
126 *
127 * @param intent The Dialogflow intent name to match.
128 * When given an array, sets the IntentHandler for any intent name in the array.
129 * @param handler The IntentHandler to be executed when the intent name is matched.
130 * When given a string instead of a function, the intent fulfillment will be redirected
131 * to the IntentHandler of the redirected intent name.
132 * @public
133 */
134 intent<TParameters extends Parameters, TArgument extends Argument>(intent: string | string[], handler: DialogflowIntentHandler<TConvData, TUserStorage, TContexts, TConversation, TParameters, TArgument> | string): this;
135 /** @public */
136 catch(catcher: ExceptionHandler<TUserStorage, TConversation>): this;
137 /** @public */
138 fallback(handler: DialogflowIntentHandler<TConvData, TUserStorage, TContexts, TConversation, Parameters, Argument> | string): this;
139 /** @hidden */
140 _middlewares: DialogflowMiddleware<DialogflowConversation<{}, {}, Contexts>>[];
141 /** @public */
142 middleware<TConversationPlugin extends DialogflowConversation<{}, {}, Contexts>>(middleware: DialogflowMiddleware<TConversationPlugin>): this;
143 /** @public */
144 verification?: DialogflowVerification | DialogflowVerificationHeaders;
145}
146/** @public */
147export interface DialogflowVerificationHeaders {
148 /**
149 * A header key value pair to check against.
150 * @public
151 */
152 [key: string]: string;
153}
154/** @public */
155export interface DialogflowVerification {
156 /**
157 * An object representing the header key to value map to check against,
158 * @public
159 */
160 headers: DialogflowVerificationHeaders;
161 /**
162 * Custom status code to return on verification error.
163 * @public
164 */
165 status?: number;
166 /**
167 * Custom error message as a string or a function that returns a string
168 * given the original error message set by the library.
169 *
170 * The message will get sent back in the JSON top level `error` property.
171 * @public
172 */
173 error?: string | ((error: string) => string);
174}
175/** @public */
176export interface DialogflowOptions<TConvData, TUserStorage> extends ConversationAppOptions<TConvData, TUserStorage> {
177 /**
178 * Verifies whether the request comes from Dialogflow.
179 * Uses header keys and values to check against ones specified by the developer
180 * in the Dialogflow Fulfillment settings of the app.
181 *
182 * HTTP Code 403 will be thrown by default on verification error.
183 *
184 * @public
185 */
186 verification?: DialogflowVerification | DialogflowVerificationHeaders;
187}
188/** @public */
189export interface Dialogflow {
190 /** @public */
191 <TConvData, TUserStorage, TContexts extends Contexts = Contexts, Conversation extends DialogflowConversation<TConvData, TUserStorage, TContexts> = DialogflowConversation<TConvData, TUserStorage, TContexts>>(options?: DialogflowOptions<TConvData, TUserStorage>): AppHandler & DialogflowApp<TConvData, TUserStorage, TContexts, Conversation>;
192 /** @public */
193 <TContexts extends Contexts, Conversation extends DialogflowConversation<{}, {}, TContexts> = DialogflowConversation<{}, {}, TContexts>>(options?: DialogflowOptions<{}, {}>): AppHandler & DialogflowApp<{}, {}, TContexts, Conversation>;
194 /** @public */
195 <TConversation extends DialogflowConversation<{}, {}> = DialogflowConversation<{}, {}>>(options?: DialogflowOptions<{}, {}>): AppHandler & DialogflowApp<{}, {}, Contexts, TConversation>;
196}
197/**
198 * This is the function that creates the app instance which on new requests,
199 * creates a way to handle the communication with Dialogflow's fulfillment API.
200 *
201 * Supports Dialogflow v1 and v2.
202 *
203 * @example
204 * ```javascript
205 *
206 * const app = dialogflow()
207 *
208 * app.intent('Default Welcome Intent', conv => {
209 * conv.ask('How are you?')
210 * })
211 * ```
212 *
213 * @public
214 */
215export declare const dialogflow: Dialogflow;
216
\No newline at end of file