UNPKG

11.8 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2018 Google Inc. All Rights Reserved.d
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.Incoming = void 0;
19const actionssdk_1 = require("../actionssdk");
20const toImage = (imageUri) => {
21 if (imageUri) {
22 return new actionssdk_1.Image({
23 url: imageUri,
24 alt: '',
25 });
26 }
27 return undefined;
28};
29class Incoming {
30 /** @hidden */
31 constructor(fulfillment) {
32 /** @public */
33 this.parsed = [];
34 if (typeof fulfillment === 'undefined') {
35 return;
36 }
37 if (Array.isArray(fulfillment)) {
38 // Dialogflow v2
39 for (const message of fulfillment) {
40 const { text, image, quickReplies, card, simpleResponses, basicCard, suggestions, linkOutSuggestion, listSelect, carouselSelect, platform, payload, } = message;
41 if (platform &&
42 platform !== 'ACTIONS_ON_GOOGLE' &&
43 platform !== 'PLATFORM_UNSPECIFIED') {
44 continue;
45 }
46 if (text) {
47 this.parsed.push(...text.text);
48 continue;
49 }
50 if (image) {
51 this.parsed.push(new actionssdk_1.Image({
52 url: image.imageUri,
53 alt: image.accessibilityText,
54 }));
55 continue;
56 }
57 if (quickReplies) {
58 this.parsed.push(new actionssdk_1.Suggestions(quickReplies.quickReplies));
59 continue;
60 }
61 if (card) {
62 const { buttons } = card;
63 this.parsed.push(new actionssdk_1.BasicCard({
64 title: card.title,
65 subtitle: card.subtitle,
66 image: toImage(card.imageUri),
67 buttons: buttons
68 ? buttons.map(b => new actionssdk_1.Button({
69 title: b.text,
70 url: b.postback,
71 }))
72 : undefined,
73 }));
74 continue;
75 }
76 if (simpleResponses) {
77 this.parsed.push(...simpleResponses.simpleResponses.map(s => new actionssdk_1.SimpleResponse({
78 speech: s.textToSpeech || s.ssml,
79 text: s.displayText,
80 })));
81 continue;
82 }
83 if (basicCard) {
84 const { image, buttons } = basicCard;
85 this.parsed.push(new actionssdk_1.BasicCard({
86 title: basicCard.title,
87 subtitle: basicCard.subtitle,
88 text: basicCard.formattedText,
89 image: image
90 ? new actionssdk_1.Image({
91 url: image.imageUri,
92 alt: image.accessibilityText,
93 })
94 : undefined,
95 buttons: buttons
96 ? buttons.map(b => new actionssdk_1.Button({
97 title: b.title,
98 url: b.openUriAction.uri,
99 }))
100 : undefined,
101 }));
102 continue;
103 }
104 if (suggestions) {
105 this.parsed.push(new actionssdk_1.Suggestions(suggestions.suggestions.map(s => s.title)));
106 continue;
107 }
108 if (linkOutSuggestion) {
109 this.parsed.push(new actionssdk_1.LinkOutSuggestion({
110 name: linkOutSuggestion.destinationName,
111 openUrlAction: {
112 url: linkOutSuggestion.uri,
113 },
114 }));
115 continue;
116 }
117 if (listSelect) {
118 this.parsed.push(new actionssdk_1.List({
119 title: listSelect.title,
120 items: listSelect.items,
121 }));
122 continue;
123 }
124 if (carouselSelect) {
125 this.parsed.push(new actionssdk_1.Carousel({
126 items: carouselSelect.items,
127 }));
128 continue;
129 }
130 if (payload) {
131 this.parsed.push(payload);
132 continue;
133 }
134 }
135 }
136 else {
137 // Dialogflow v1
138 const { speech, messages } = fulfillment;
139 if (speech) {
140 this.parsed.push(speech);
141 }
142 if (typeof messages !== 'undefined') {
143 for (const message of messages) {
144 const { platform, type } = message;
145 if (platform && platform !== 'google') {
146 continue;
147 }
148 if (type === 0) {
149 const assumed = message;
150 this.parsed.push(assumed.speech);
151 continue;
152 }
153 if (type === 3) {
154 const assumed = message;
155 this.parsed.push(toImage(assumed.imageUrl));
156 continue;
157 }
158 if (type === 1) {
159 const assumed = message;
160 const { buttons } = assumed;
161 this.parsed.push(new actionssdk_1.BasicCard({
162 title: assumed.title,
163 subtitle: assumed.subtitle,
164 image: toImage(assumed.imageUrl),
165 buttons: buttons
166 ? buttons.map(b => new actionssdk_1.Button({
167 title: b.text,
168 url: b.postback,
169 }))
170 : undefined,
171 }));
172 continue;
173 }
174 if (type === 2) {
175 const assumed = message;
176 this.parsed.push(new actionssdk_1.Suggestions(assumed.replies));
177 continue;
178 }
179 if (type === 4) {
180 const assumed = message;
181 this.parsed.push(assumed.payload);
182 continue;
183 }
184 if (type === 'simple_response') {
185 const assumed = message;
186 this.parsed.push(new actionssdk_1.SimpleResponse({
187 text: assumed.displayText,
188 speech: assumed.textToSpeech,
189 }));
190 continue;
191 }
192 if (type === 'basic_card') {
193 const assumed = message;
194 const { image = {}, buttons } = assumed;
195 this.parsed.push(new actionssdk_1.BasicCard({
196 title: assumed.title,
197 subtitle: assumed.subtitle,
198 text: assumed.formattedText,
199 image: toImage(image.url),
200 buttons: buttons
201 ? buttons.map(b => new actionssdk_1.Button({
202 title: b.title,
203 url: b.openUrlAction.url,
204 }))
205 : undefined,
206 }));
207 continue;
208 }
209 if (type === 'list_card') {
210 const assumed = message;
211 this.parsed.push(new actionssdk_1.List({
212 title: assumed.title,
213 items: assumed.items,
214 }));
215 continue;
216 }
217 if (type === 'suggestion_chips') {
218 const assumed = message;
219 this.parsed.push(new actionssdk_1.Suggestions(assumed.suggestions.map(s => s.title)));
220 continue;
221 }
222 if (type === 'carousel_card') {
223 const assumed = message;
224 this.parsed.push(new actionssdk_1.Carousel({
225 items: assumed.items,
226 }));
227 continue;
228 }
229 if (type === 'link_out_chip') {
230 const assumed = message;
231 this.parsed.push(new actionssdk_1.LinkOutSuggestion({
232 name: assumed.destinationName,
233 openUrlAction: {
234 url: assumed.url,
235 },
236 }));
237 continue;
238 }
239 if (type === 'custom_payload') {
240 const assumed = message;
241 this.parsed.push(assumed.payload);
242 continue;
243 }
244 }
245 }
246 }
247 }
248 // tslint:disable-next-line:no-any allow constructors with any type of arguments
249 get(type) {
250 for (const message of this) {
251 if (typeof type === 'string') {
252 if (typeof message === type) {
253 return message;
254 }
255 continue;
256 }
257 if (message instanceof type) {
258 return message;
259 }
260 }
261 return null;
262 }
263 /**
264 * Gets the Dialogflow incoming messages as an iterator.
265 * Messages are converted into client library class instances or a string.
266 * See {@link Incoming#get|conv.incoming.get} for details on how the conversion works.
267 *
268 * @example
269 * ```javascript
270 *
271 * // Dialogflow
272 * const app = dialogflow()
273 *
274 * // Create messages in the Dialogflow Console Intent Responses section
275 * app.intent('Default Welcome Intent', conv => {
276 * const messages = [...conv.incoming]
277 * // do something with the messages
278 * // or just spread them out back to the user
279 * conv.ask(`Here's what was set in the Dialogflow console`)
280 * conv.ask(...conv.incoming)
281 * }
282 * ```
283 *
284 * @public
285 */
286 [Symbol.iterator]() {
287 return this.parsed[Symbol.iterator]();
288 // suppose to be Array.prototype.values(), but can't use because of bug:
289 // https://bugs.chromium.org/p/chromium/issues/detail?id=615873
290 }
291}
292exports.Incoming = Incoming;
293//# sourceMappingURL=incoming.js.map
\No newline at end of file