首页 > 解决方案 > 格式错误的响应:由于语音响应为空,无法将 Dialogflow 响应解析为 AppResponse

问题描述

我在 Dialogflow 中使用 firebase 函数来实现 webhook。我正在使 webhook 成功,fulfillment status但它不起作用。我使用的是版本 1。当我在 Google Assistant 模拟器上对其进行测试时,它显示“应用程序没有响应”。

火力基地功能

const functions = require('firebase-functions');

exports.webhook = functions.https.onRequest((request, response) => {
    response.send({
        "google":{
           "richResponse":{
              "items":[
                 {
                    "simpleResponse":{
                       "textToSpeech":"Hey! Good to see you."
                    }
                 },
                 {
                    "mediaResponse":{
                       "mediaType":"AUDIO",
                       "mediaObjects":[
                          {
                             "name":"Exercises",
                             "description":"ex",
                             "largeImage":{
                                "url":"http://res.freestockphotos.biz/pictures/17/17903-balloons-pv.jpg",
                                "accessibilityText":"..."
                             },
                             "contentUrl":"https://theislam360.me:8080/hbd.mp3"
                          }
                       ]
                    }
                 }
              ],
              "suggestions":[
                 {
                    "title":"chips"
                 }
              ]
           }
        }
      }
   )
});`

{google...当我通过 GUI 手动将响应从自定义有效负载中复制粘贴到末尾时,它可以工作。而对于 webhook,它不起作用。

原始 API 响应

 {
  "id": "eaf627ed-26b5-4965-b0b0-bc77144e144b",
  "timestamp": "2019-04-15T11:54:18.948Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "play hbd",
    "action": "",
    "actionIncomplete": false,
    "parameters": {
      "any": "hbd"
    },
    "contexts": [],
    "metadata": {
      "isFallbackIntent": "false",
      "webhookResponseTime": 34,
      "intentName": "play",
      "intentId": "e60071cd-ce31-4ef9-ae9b-cc370c3362b3",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "false"
    },
    "fulfillment": {
      "messages": []
    },
    "score": 1
  },
  "status": {
    "code": 200,
    "errorType": "success"
  },
  "sessionId": "e91bd62f-766b-b19d-d37b-2917ac20caa6"
}

履行请求

{
  "id": "eaf627ed-26b5-4965-b0b0-bc77144e144b",
  "timestamp": "2019-04-15T11:54:18.948Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "play hbd",
    "speech": "",
    "action": "",
    "actionIncomplete": false,
    "parameters": {
      "any": "hbd"
    },
    "contexts": [],
    "metadata": {
      "intentId": "e60071cd-ce31-4ef9-ae9b-cc370c3362b3",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "false",
      "isFallbackIntent": "false",
      "intentName": "play"
    },
    "fulfillment": {
      "speech": "",
      "messages": []
    },
    "score": 1
  },
  "status": {
    "code": 200,
    "errorType": "success"
  },
  "sessionId": "e91bd62f-766b-b19d-d37b-2917ac20caa6"
}

履行响应

{
  "google": {
    "richResponse": {
      "items": [
        {
          "simpleResponse": {
            "textToSpeech": "Hey! Good to see you."
          }
        },
        {
          "mediaResponse": {
            "mediaType": "AUDIO",
            "mediaObjects": [
              {
                "name": "Exercises",
                "description": "ex",
                "largeImage": {
                  "url": "http://res.freestockphotos.biz/pictures/17/17903-balloons-pv.jpg",
                  "accessibilityText": "..."
                },
                "contentUrl": "https://theislam360.me:8080/hbd.mp3"
              }
            ]
          }
        }
      ],
      "suggestions": [
        {
          "title": "chips"
        }
      ]
    }
  }
}

履行状态

Webhook execution successful

在此处输入图像描述

Firebase 日志 在此处输入图像描述

Google 助理模拟器日志 在此处输入图像描述

标签: dialogflow-esactions-on-google

解决方案


您没有在响应中使用正确的 JSON。通过将其放入 GUI 中的“自定义负载”部分,它会为您创建更大的 JSON 响应。该google对象需要位于dataDialogflow v1 或payloadDialogflow v2 的对象下。(如果你还没有切换到 v2 - 你应该立即这样做,因为 v1 将在大约一个月后关闭。)

所以你返回的应该看起来更像

{
  "payload": {
    "google": {
      ...
    }
  }
}

推荐阅读