Action using web-fulfillment with firebase throwing MalformedResponse 'final_response' must be set

喜欢而已 提交于 2019-12-02 12:32:47

问题


Env : Using firebase cloud deployed google action. Action is using webhook to get results from functions. I am using Blaze plan so calling external URL should be legit. I am using dialogflow V2.

Part of my function's job is doing the following: I make an external API request using the following (Masked code detail):

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);

Once I have the result, I parse it and use it.

Following are my reference points that I tried from stack overflow solutions:

  • Dialogflow v2 API + Actions v2 API: MalformedResponse 'final_response' must be set

Would appreciate any help from the community.


回答1:


In most cases involving the MalformedResponse and an asynchronous call using something like request, the problem is that you're sending the response outside of the callback. Frequently this is because the library is expecting a Promise and you are handling things in a non-promise like way.

My usual practice is:

  • use the request-promise library (or the request-promise-native library)
  • in one of the then sections where you get the results, make sure you call conv.ask()
  • make sure you return the promise itself.

So (very roughly)

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?` );
  });


来源:https://stackoverflow.com/questions/50361643/action-using-web-fulfillment-with-firebase-throwing-malformedresponse-final-res

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!