/**
 * Copyright 2018 Google Inc. All Rights Reserved.d
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import * as Api from './api/v2' ; import * as ApiV1 from './api/v1' ; import { Image , Suggestions , BasicCard , SimpleResponse , LinkOutSuggestion , List , Carousel , Button , } from '../actionssdk' ; import { JsonObject } from '../../common' ; export type IncomingMessage = string | Image | Suggestions | BasicCard | SimpleResponse | LinkOutSuggestion | List | Carousel | JsonObject ; const toImage = ( imageUri ) => { if ( imageUri ) { return new Image ( { url : imageUri , alt : '' , } ) ; } return undefined ; } ; export class Incoming { public parsed : IncomingMessage [ ] = [ ] ; hidden constructor ( fulfillment ) { if ( typeof fulfillment === 'undefined' ) { return ; } if ( Array . isArray ( fulfillment ) ) { for ( const message of fulfillment ) { const { text , image , quickReplies , card , simpleResponses , basicCard , suggestions , linkOutSuggestion , listSelect , carouselSelect , platform , payload , } = message ; if ( platform && platform !== 'ACTIONS_ON_GOOGLE' && platform !== 'PLATFORM_UNSPECIFIED' ) { continue ; } if ( text ) { this . parsed . push ( ... text . text ! ) ; continue ; } if ( image ) { this . parsed . push ( new Image ( { url : image . imageUri ! , alt : image . accessibilityText ! , } ) ) ; continue ; } if ( quickReplies ) { this . parsed . push ( new Suggestions ( quickReplies . quickReplies ! ) ) ; continue ; } if ( card ) { const { buttons } = card ; this . parsed . push ( new BasicCard ( { title : card . title , subtitle : card . subtitle , image : toImage ( card . imageUri ) , buttons : buttons ? buttons . map ( b => new Button ( { title : b . text ! , url : b . postback , } ) ) : undefined , } ) ) ; continue ; } if ( simpleResponses ) { this . parsed . push ( ... simpleResponses . simpleResponses ! . map ( s => new SimpleResponse ( { speech : s . textToSpeech || s . ssml ! , text : s . displayText , } ) ) ) ; continue ; } if ( basicCard ) { const { image , buttons } = basicCard ; this . parsed . push ( new BasicCard ( { title : basicCard . title , subtitle : basicCard . subtitle , text : basicCard . formattedText , image : image ? new Image ( { url : image . imageUri ! , alt : image . accessibilityText ! , } ) : undefined , buttons : buttons ? buttons . map ( b => new Button ( { title : b . title ! , url : b . openUriAction ! . uri , } ) ) : undefined , } ) ) ; continue ; } if ( suggestions ) { this . parsed . push ( new Suggestions ( suggestions . suggestions ! . map ( s => s . title ! ) ) ) ; continue ; } if ( linkOutSuggestion ) { this . parsed . push ( new LinkOutSuggestion ( { name : linkOutSuggestion . destinationName ! , openUrlAction : { url : linkOutSuggestion . uri , } , } ) ) ; continue ; } if ( listSelect ) { this . parsed . push ( new List ( { title : listSelect . title , items : listSelect . items ! , } ) ) ; continue ; } if ( carouselSelect ) { this . parsed . push ( new Carousel ( { items : carouselSelect . items ! , } ) ) ; continue ; } if ( payload ) { this . parsed . push ( payload ) ; continue ; } } } else { const { speech , messages } = fulfillment ; if ( speech ) { this . parsed . push ( speech ) ; } if ( typeof messages !== 'undefined' ) { for ( const message of messages ) { const { platform , type } = message ; if ( platform && platform !== 'google' ) { continue ; } if ( type === 0 ) { const assumed = message as ApiV1 . DialogflowV1MessageText ; this . parsed . push ( assumed . speech ! ) ; continue ; } if ( type === 3 ) { const assumed = message as ApiV1 . DialogflowV1MessageImage ; this . parsed . push ( toImage ( assumed . imageUrl ) ! ) ; continue ; } if ( type === 1 ) { const assumed = message as ApiV1 . DialogflowV1MessageCard ; const { buttons } = assumed ; this . parsed . push ( new BasicCard ( { title : assumed . title , subtitle : assumed . subtitle , image : toImage ( assumed . imageUrl ) , buttons : buttons ? buttons . map ( b => new Button ( { title : b . text ! , url : b . postback , } ) ) : undefined , } ) ) ; continue ; } if ( type === 2 ) { const assumed = message as ApiV1 . DialogflowV1MessageQuickReplies ; this . parsed . push ( new Suggestions ( assumed . replies ! ) ) ; continue ; } if ( type === 4 ) { const assumed = message as ApiV1 . DialogflowV1MessageCustomPayload ; this . parsed . push ( assumed . payload ! ) ; continue ; } if ( type === 'simple_response' ) { const assumed = message as ApiV1 . DialogflowV1MessageSimpleResponse ; this . parsed . push ( new SimpleResponse ( { text : assumed . displayText , speech : assumed . textToSpeech ! , } ) ) ; continue ; } if ( type === 'basic_card' ) { const assumed = message as ApiV1 . DialogflowV1MessageBasicCard ; const { image = { } , buttons } = assumed ; this . parsed . push ( new BasicCard ( { title : assumed . title , subtitle : assumed . subtitle , text : assumed . formattedText , image : toImage ( image . url ) , buttons : buttons ? buttons . map ( b => new Button ( { title : b . title ! , url : b . openUrlAction ! . url , } ) ) : undefined , } ) ) ; continue ; } if ( type === 'list_card' ) { const assumed = message as ApiV1 . DialogflowV1MessageList ; this . parsed . push ( new List ( { title : assumed . title , items : assumed . items ! , } ) ) ; continue ; } if ( type === 'suggestion_chips' ) { const assumed = message as ApiV1 . DialogflowV1MessageSuggestions ; this . parsed . push ( new Suggestions ( assumed . suggestions ! . map ( s => s . title ! ) ) ) ; continue ; } if ( type === 'carousel_card' ) { const assumed = message as ApiV1 . DialogflowV1MessageCarousel ; this . parsed . push ( new Carousel ( { items : assumed . items ! , } ) ) ; continue ; } if ( type === 'link_out_chip' ) { const assumed = message as ApiV1 . DialogflowV1MessageLinkOut ; this . parsed . push ( new LinkOutSuggestion ( { name : assumed . destinationName ! , openUrlAction : { url : assumed . url ! , } , } ) ) ; continue ; } if ( type === 'custom_payload' ) { const assumed = message as ApiV1 . DialogflowV1MessageGooglePayload ; this . parsed . push ( assumed . payload ! ) ; continue ; } } } } } example param type public get < TMessage extends IncomingMessage > ( type ) ; public get ( type ) ; get < TMessage extends IncomingMessage > ( type ) { for ( const message of this ) { if ( typeof type === 'string' ) { if ( typeof message === type ) { return message ; } continue ; } if ( message instanceof type ) { return message ; } } return null ; } /**
     * Gets the Dialogflow incoming messages as an iterator.
     * Messages are converted into client library class instances or a string.
     * See  Incoming for details on how the conversion works.
     *
     *  example public [ Symbol . iterator ] ( ) { return this . parsed [ Symbol . iterator ] ( ) ; } }