必须设置使用web-fulfillment和firebase抛出MalformedResponse'final_response'的操作

时间:2018-05-16 02:30:38

标签: node.js google-cloud-functions actions-on-google dialogflow

环境:使用firebase云部署谷歌行动。 Action正在使用webhook从函数中获取结果。 我正在使用Blaze计划,因此调用外部URL应该是合法的。 我正在使用dialogflow V2。

我的部分功能是执行以下操作: 我使用以下(Masked code detail)创建外部API请求:

var requestObj = require('request');
var options = {
  url: 'my url',
  headers: {
    'User-Agent': 'request'
  }
};

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    var info = JSON.parse(body);
    result = JSON.parse(body).element;
    console.log('Title 0  ' + result);
  }
}

requestObj(options, callback);

获得结果后,我会解析并使用它。

以下是我从堆栈溢出解决方案尝试的参考点:

感谢社区的任何帮助。

1 个答案:

答案 0 :(得分:2)

在大多数情况下,涉及MalformedResponse和使用request之类的异步调用,问题是您在回调之外发送响应。这通常是因为图书馆期待一个Promise,你正在以非承诺的方式处理事情。

我通常的做法是:

所以(非常粗略地)

var request = require('request-promise-native');

var options = {
  uri: 'https://example.com/api',
  json: true // Automatically parses the JSON string in the response
};

return request(options)
  .then( response => {
    // The response will be a JSON object already. Do whatever with it.
    var value = response.whatever.you.want;
    return conv.ask( `The value is ${value}. What now?` );
  });