Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

node.js - Use Async Functions in DialogFlow WebHook

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...