0

I am having trouble getting the value returned by my https.get function inside the my Dialogflow intent to close the conversation. Regardless whether I execute this call in the app.intent or pass it one to an external function, it fails. I new to node.js but have used angular.js and javascript before but not having success in being able to close the conversation with a response. Google Actions emulator gives me the error

MalformedResponse expected_inputs[0].input_prompt.rich_initial_prompt: 'rich_response' must contain at least one item.

Below is my code:

app.intent('mywebhook', (conv, params) => {
    const stateName = params['geo-state-us'];
    console.log("My State is " + stateName);
    var pathString = 'api path' + encodeURIComponent(stateName);

    var request = https.get({
        host: 'www.mydomainame.com',
        path: pathString
    }, function (response){
        var json = "";

        response.on('data', function(chunk) {
            json += chunk;

        });

        response.on('end', function(){
            var jsonData = JSON.parse(json);
            var myfirstvar = jsonData[0].firstvar;
            var chat = "the value of first var is " + chat;
            console.log(chat); // this works fine
            conv.close(chat);
        });

    }).on('error', (e) => {
        console.error(e);
    });


}

I even tried doing conv.close(chat) outside and JSON.stringify(request) to get the value of myfirstvar but nothing worked. Spent a whole day trying different things but no avail.

daedsidog
  • 1,732
  • 2
  • 17
  • 36
user9465677
  • 437
  • 4
  • 21
  • Have you configured [Firebase Billing](https://stackoverflow.com/a/42787576/6763544) ( example a Blaze Plan ) to access an external network ie to get response from your external api? – Sairaj Sawant Dec 16 '18 at 04:15
  • @sai.raj - yes. and my webhook works fine for all other intents. Just not the one that I am calling the webhook for. I am sure the issue is something very trivial. – user9465677 Dec 16 '18 at 04:23
  • I guess your code isn't returning Promise – Sairaj Sawant Dec 16 '18 at 04:35
  • may be...and that is what I want to know..how to return the value i want to return. – user9465677 Dec 16 '18 at 04:47

1 Answers1

1

Try the refactored code below ( and comment if that works or not ):

app.intent('mywebhook', myWebHookFunction);

function myWebHookFunction(conv, params) {

  return new Promise(function (resolve, reject) {

    const stateName = params['geo-state-us'];
    console.log("My State is " + stateName);
    var pathString = 'api path' + encodeURIComponent(stateName);

    var request = https.get({
      host: 'www.mydomainame.com',
      path: pathString
    }, function (response) {
      var json = "";

      response.on('data', function (chunk) {
        json += chunk;

      });

      response.on('end', function () {
        var jsonData = JSON.parse(json);
        var myfirstvar = jsonData[0].firstvar;
        var chat = "the value of first var is " + chat; // chat or myfirstvar?
        console.log(chat); // this works fine 
        conv.close(chat);
        resolve();
      });

    }).on('error', (e) => {
      console.error(e);
      reject();
    });
  });
}
Sairaj Sawant
  • 1,842
  • 1
  • 12
  • 16
  • this works. I have one more question. Do you know how would I pass the value chat to another function. – user9465677 Dec 16 '18 at 05:43
  • If you are using Actions on Google, see [this](https://developers.google.com/actions/assistant/save-data) – Sairaj Sawant Dec 16 '18 at 05:51
  • 1
    The original code works fine. As far as using the storage is concerned, my main goal is to hand off the chat from Google Home to a mobile phone / screen device such Google Base. I know how to hand off the conversation using the surface capabilities, but I cannot hand off the value. Which I am not sure if I use storage would work. The chat variable somehow needs to become global. Basically when we call conv.ask(new NewSurface( the chat variable needs to become global or the json needs to become global. – user9465677 Dec 16 '18 at 10:17