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