iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 14
1

昨天的index.ts負責

  • init firebase

  • http onRequest

  • 製作Message、action dispatcher
    model.ts 則是作為型別check的標準

      export interface MessageInterface {
         session: string,
         queryText: string,
         action?: string,
         event?: string, 
         contaxt? :string,
         parameters: any,
         idToken?: string,
         sentiment?: number,
         time: number,
         responseText?: string,
         res: functions.Response<any>
      }
    
      export class Message implements MessageInterface {
          session: string
          queryText: string
          action?: string
          event?: string
          contaxt?: string
          parameters: any
          idToken?: string
          sentiment?: number
          time: number
          responseText?: string
          res: functions.Response<any>
    
      constructor(message: MessageInterface) {
          this.session = message.session
          this.queryText = message.queryText
          this.action = message.action
          this.event = message.event
          this.contaxt = message.contaxt
          this.parameters = message.parameters
          this.idToken = message.idToken
          this.sentiment = message.sentiment
          this.time = message.time
          this.responseText = message.responseText
          this.res = message.res
      }
      }
    

responseFormat.ts 則是設定回覆text的格式,並利用model做型別確認

    import { Message } from "./model"
    export const toTextResponse = (message: Message): any => {
        const textResponse = {
            "fulfillmentMessages": [
                {
                    "text": {
                        "text": [
                            `${message.responseText}`
                        ]
                    }
                }
            ]
        }

        return textResponse
    }

    export const toGoogleAssistantResponse = (message: Message): any => {
        const googleAssistantResponse = {
            "payload": {
                "google": {
                    "expectUserResponse": true,
                    "richResponse": {
                        "items": [
                            {
                                "simpleResponse": {
                                    "textToSpeech": `${message.responseText}`
                                }
                            }
                        ]
                    }
                }
            }
        }

        return googleAssistantResponse
    }

    export const toEventResponse = (message: Message): any => {
        const eventResponse = {
            "followupEventInput": {
                "name": `${message.event}`,
                "languageCode": "zh-tw",
                "parameters": message.parameters
            }
        }

        return eventResponse
    }

workFlow.ts

import {  MessageInterface } from "./model"
import * as responseFormat from "./responseFormat"
// import * as languageService from "./languageService"



export const moodConversation = async(dialogflowMessage: MessageInterface): Promise<any> => {
    switch (dialogflowMessage.parameters.mood) {
        case "case1":
            dialogflowMessage.responseText = 'It is case 1 '
            break;

        case "case2":
            dialogflowMessage.responseText = 'It is case 2 '
            break;

        default:
            break;
    }

    console.log("responseText:", dialogflowMessage.responseText)

    const responseType =  responseFormat.toGoogleAssistantResponse(dialogflowMessage)
    dialogflowMessage.res.status(200).json(responseType)
}


export const ask4physiological = async(dialogflowMessage: MessageInterface): Promise<any> => {

   dialogflowMessage.event = "event1"
   dialogflowMessage.parameters = {
       name: dialogflowMessage.parameters.name,
       physiological: dialogflowMessage.parameters.physiological,
       value: 123
   }


    const eventMessage =      responseFormat.toEventResponse(dialogflowMessage)
    dialogflowMessage.res.status(200).json(eventMessage)
}

建立好後,我們便可deploy這隻fullfillmentWebhhok到firebase,並將網址丟入dialogflow以觸發。


上一篇
Day13 dialogflow(2)
下一篇
Day15 15天專案結合與pubsub介紹
系列文
手拉手教自己用樹梅派、GoogleHome、GCP做一個AI智慧提醒機器人!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言