【问题标题】:Dialogflow api call works, but chatbot shuts offDialogflow api 调用有效,但聊天机器人关闭
【发布时间】:2019-02-11 18:23:59
【问题描述】:

在 Dialogflow 中,我使用免费版 (V2) 和 Firebase 的 blaze 计划。 我有一个适用于“测试”一词的意图。当我在模拟器中输入“测试”时,聊天机器人没有响应并离开聊天。假设调用我的 API 并检索信息。

奇怪的是,有一个 console.log 打印出正文并从 API 返回 JSON。这意味着 API 调用工作正常,但机器人内部的某个地方仍然存在错误。

我发现了这个问题:Dialogflow v2 error “MalformedResponse 'final_response' must be set”

它看起来很像我的问题,但我似乎无法弄清楚我应该改变什么来让我的工作。

提前感谢您的宝贵时间。

实现:

function testcommand(agent) {
  callNPApi().then((output) => {
    agent.add(output);
  }).catch(() => {
    agent.add("That went wrong!");
  });
}

function callNPApi() {
  return new Promise((resolve, reject) => {
    request2(url, function (error, response2, body){
      //The substring is too ensure it doesnt crash for the character limit yet
      body = body.substring(1,10);
      console.log('Api errors: ' + JSON.stringify(error));
      console.log('Api body: ' + JSON.stringify(body));

      if (error) {
        reject();
      }
      resolve('api call returned: ');
    });
  });
}

控制台中的响应:

{
  "responseMetadata": {
    "status": {
      "code": 10,
      "message": "Failed to parse Dialogflow response into AppResponse because of empty speech response",
      "details": [
        {
          "@type": "type.googleapis.com/google.protobuf.Value",
          "value": "{\"id\":\"bca7bd81-58f1-40e7-a5d5-e36b60986b66\",\"timestamp\":\"2018-09-06T12:45:26.718Z\",\"lang\":\"nl\",\"result\":{},\"alternateResult\":{},\"status\":{\"code\":200,\"errorType\":\"success\"},\"sessionId\":\"ABwppHFav_2zx7FWHNQn7d0uw8B_I06cY91SKfn1eJnVNFa3q_Y6CrE_OAJPV-ajaZXl7o2ZHfdlVAZwXw\"}"
        }
      ]
    }
  }
}

控制台中的错误:

MalformedResponse
'final_response' must be set.

【问题讨论】:

  • 你的 api 客户端是异步的吗? SDK 认为一切都已完成,并且未设置响应。尝试返回承诺

标签: javascript firebase dialogflow-es actions-on-google


【解决方案1】:

是的,这是同样的问题。

问题是您正在从callNPApi() 返回一个 Promise,但您的事件处理程序(我假设是 testcommand()没有也返回一个 Promise。如果您在处理程序中的任何位置进行异步调用,则必须使用 Promise,如果您使用的是 Promise,则还必须从处理程序返回该 Promise。

在您的情况下,这应该是一个简单的更改。只需将“return”添加到您的处理程序。所以它可能看起来像这样

function testcommand(agent) {
  return callNPApi().then((output) => {
    agent.add(output);
  }).catch(() => {
    agent.add("That went wrong!");
  });
}

【讨论】:

  • 感谢您的宝贵时间。我真的很感激您花额外的时间来解释为什么它不能与我的代码示例一起使用!
猜你喜欢
  • 2019-10-28
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
  • 2011-04-23
相关资源
最近更新 更多