[英]Using 3rd party API within Dialogflow Fulfillment
I've got a Dialogflow agent for which I'm using the Inline Editor (powered by Cloud Functions for Firebase). 我有一个Dialogflow代理,我正在使用内联编辑器(由Firebase的Cloud Functions提供支持)。 When I try to embed an HTTPS GET handler within the Intent handler, it crashes with log entry "Ignoring exception from a finished function". 当我尝试在Intent处理程序中嵌入HTTPS GET处理程序时,它会与日志条目“忽略已完成的函数中的异常”崩溃。 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. 我可以看到它确实在升级到Blaze计划后执行外部查询,因此它不是计费帐户的限制。 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);
});
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. 使用Promises的方法不仅仅是“更好”的方法 - agent.handleRequest()
要求您使用Promises,如果您有任何异步调用的代码。 The issue is that your findWidget
function is returning before the https.get
finishes running, so the reply doesn't end up being sent. 问题是你的findWidget
函数在https.get
完成运行之前返回,因此回复不会最终被发送。
I tend to use the request-promise-native package to do HTTP calls, since it simplifies many things. 我倾向于使用request-promise-native包来进行HTTP调用,因为它简化了很多事情。 (However, you're free to wrap the call in a Promise yourself.) So the Promise version might look something like this (untested): (但是,您可以自己将调用包装在Promise中。)因此,Promise版本可能看起来像这样(未经测试):
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 );
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.