1

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:

Would appreciate any help from the community.

sumandas
  • 555
  • 7
  • 20
  • I think you should put your question in the body of your text too. Otherwise as you are using firebase you have access to the logs with 'firebase functions:log', can you tell us what does this give to you? – Rémi C. May 16 '18 at 14:49
  • And do you have a conv.ask somewhere in your code or do you only use console logs? It could be due to that, otherwise can you show us when you are calling conv.Ask ? – Rémi C. May 16 '18 at 14:50

1 Answers1

2

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?` );
  });
Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • Made the suggested changes, with one extra line let conv = agent.conv(); still complaining: MalformedResponse 'final_response' must be set. Please suggest. – sumandas May 18 '18 at 00:38
  • https://stackoverflow.com/q/50422410/6114925 will be help as my issue is same as this user. – sumandas May 23 '18 at 05:10
  • I've changed the answer so it uses request-promise-native. Did that fix your problem? – Prisoner May 23 '18 at 10:20
  • 1
    Yes it actually fixed the issue, I realized you made the comment about the node version being 6 is causing this and you replicated it. So I just took that answer to solve my issue. – sumandas May 23 '18 at 16:20
  • 1
    Great! Glad we were able to get you working. If an answer has helped, upvoting and/or accepting the answer is always appreciated. – Prisoner May 23 '18 at 17:18