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 handler, it crashes with log entry "Ignoring exception from a finished function". Maybe there's a better way to do this with promises but I'm new to that. I can see that it does in fact execute the external query after upgrading to the Blaze plan so it's not a limitation of the billing account. Anyhow, here's the code:
'use strict';
const functions = require('firebase-functions');
//const rp = require('request-promise');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function findWidget(agent) {
const https = require("https");
const url = "https://api.site.com/sub?query=";
agent.add(`Found a widget for you:`);
var widgetName = getArgument('Name');
https.get(url + widgetName, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
body = JSON.parse(body);
agent.add(new Card({
title: `Widget ` + body.name,
text: body.description,
buttonText: 'Open link',
buttonUrl: body.homepage
})
);
});
});
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('Find Old Movie Intent', findOldMovie);
intentMap.set('Find Movie Intent', findMovie);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});
Enable fulfillment responseNavigate to the Dialogflow console and click Intents. Click Schedule Appointment Intent. Scroll down to Fulfillment and turn on Enable webhook call for the intent.
When you enable fulfillment for an intent, Dialogflow responds to that intent by calling a service that you define. For example, if an end-user wants to schedule a haircut on Friday, your service can check your database and respond to the end-user with availability information for Friday.
Enter here service account name and service description and click on Create button. On the next page, you'll see Service account permissions settings. Select Dialogflow API Client role and click on the Continue button. The next page is to grant users access to the service account you just created.
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 );
});
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With