Using 3rd party API within Dialogflow Fulfillment

后端 未结 1 961
孤街浪徒
孤街浪徒 2020-12-09 22:15

I\'ve got a Dialogflow agent for which I\'m using the Inline Editor (powered by Cloud Functions for Firebase). When I try to embed an HTTPS GET handler within the Intent han

相关标签:
1条回答
  • 2020-12-09 22:38

    It isn't just that there is a "better" way to do it with Promises - agent.handleRequest() requires you to use Promises if you have any code that is being called asynchronously. The issue is that your findWidget function is returning before the https.get finishes running, so the reply doesn't end up being sent.

    I tend to use the request-promise-native package to do HTTP calls, since it simplifies many things. (However, you're free to wrap the call in a Promise yourself.) So the Promise version might look something like this (untested):

      var findWidget = function(agent) {
    
        const request = require('request-promise-native');
        const url = "https://api.site.com/sub?query=";
    
        agent.add(`Found a widget for you:`);
    
        var widgetName = getArgument('Name');
    
        return request.get( url+widgetName )
          .then( jsonBody => {
            var body = JSON.parse(jsonBody);
            agent.add(new Card({
              title: `Widget ` + body.name,
              text: body.description,
              buttonText: 'Open link',
              buttonUrl: body.homepage
            }));
            return Promise.resolve( agent ); 
          });
        });  
      }
    
    0 讨论(0)
提交回复
热议问题