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
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 );
});
});
}