5

Referring to the solution posted here (DialogFlow V2 Webhook - Expects Speech responses Immediately and not after async requests)
What I want to achieve is that the web hook should wait until I get a response from my api call. P.S: The API is working, its just that the bot doesn't wait for the response to come. Any help would be greatly appreciated. Thanks

const rp = require('request-promise');

    function convert(params){
    return rp('https://data.fixer.io/api/convert?access_key=[my key]&from='+
            params['currency-from']+'&to='+params['currency-to']+'&amount='+params.amount) 
    .then((data) => {
       let responseData = JSON.parse(data);
       let message = responseData.result;
       console.log('Success');
       return Promise.resolve(message);
    }).catch((err)=> {
        return Promise.reject(err);
    });
}

  function currencyConversion(agent) {
        let params = request.body.result.parameters;
        return convert(params)
        .then((message)=> {
             agent.add(`${params.amount} ${params['currency-from']} is ${message} ${params['currency-to']}`);
                return Promise.resolve()
        })
       .catch((err) => {
                console.log(err);
                agent.add("Uh oh, something happened.");
                return Promise.resolve();
            })
  }

let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('currency.convert', currencyConversion);
Prisoner
  • 49,922
  • 7
  • 53
  • 105
Utsav Preet
  • 248
  • 3
  • 9

2 Answers2

4

You didn't state what environment you were running in, but given your code, and the agent.parameters change I outlined above, I was able to duplicate your problem using Firebase Cloud Functions with node.js 6.14.

I was able to get it to work by using the request-promise-native package instead of request-promise. As the name suggests, this uses native Promises instead of the Bluebird Promise package, but otherwise the calls you're making are identical.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
1

The Promise logic looks sound, but this line doesn't make sense:

let params = request.body.result.parameters;

There is no request object that is in scope.

You can get the parameters for the request from agent.parameters.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • the logic is working, I'm getting the output. The only thing is that my webhook isn't waiting for the output is come, it just exits. Can you please help me with that. Thanks – Utsav Preet May 20 '18 at 14:35