node.js - 如何在 Dialogflow 内联中读取 API CALL 的 URL?
问题描述
我以 node.js 的形式进行了一些 API 调用,并检查了它在我的本地运行良好。
所以我尝试放入Dialogflow。
但它没有给出任何变量。
这是错误。
错误:无法处理请求
我在下面解释我的程序。
[1]
这在我的本地使用 node.js 效果很好。
它给出的值为 ['aa','bb','cc',...,'dd']
const request = require('request');
const url = "https://datos.madrid.es/portal/site/egob/menuitem.ac61933d6ee3c31cae77ae7784f1a5a0/?vgnextoid=00149033f2201410VgnVCM100000171f5a0aRCRD&format=json&file=0&filename=201132-0-museos&mgmtid=118f2fdbecc63410VgnVCM1000000b205a0aRCRD&preview=full";
function information(){
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200)
{
}
var bd = body['@graph'];
var model = [];
var i = i;
for (i = 0; i < Object.keys(bd).length; i++)
{
model[i] = bd[i].title;
}
return model;
});
}
[2]
因此,我在内联编辑器上编写了一些代码来检查它是否适用于这种值。
我在这段代码的最下面附加了一个函数。
当我输入“你好”时,它会显示“1 , 2”。效果很好。
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request');
process.env.DEBUG = 'dialogflow:debug';
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 hello(agent) {
var model2 = information();
agent.add(model2);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
let intentMap = new Map();
intentMap.set('hello', hello);
intentMap.set('Default Fallback Intent', fallback);
agent.handleRequest(intentMap);
});
function information(){
return ['1','2'];
}
[3]
最后,我尝试处理我的 API 调用。
但它没有任何价值。
它也有每个包模块。
我想知道为什么它在相同的情况下不起作用。
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request');
const url = "https://datos.madrid.es/portal/site/egob/menuitem.ac61933d6ee3c31cae77ae7784f1a5a0/?vgnextoid=00149033f2201410VgnVCM100000171f5a0aRCRD&format=json&file=0&filename=201132-0-museos&mgmtid=118f2fdbecc63410VgnVCM1000000b205a0aRCRD&preview=full";
process.env.DEBUG = 'dialogflow:debug';
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 hello(agent) {
var model2 = information();
agent.add(model2);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
let intentMap = new Map();
intentMap.set('hello', hello);
intentMap.set('Default Fallback Intent', fallback);
agent.handleRequest(intentMap);
});
function information(){
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200)
{
}
var bd = body['@graph'];
var model = [];
var i = i;
for (i = 0; i < Object.keys(bd).length; i++)
{
model[i] = bd[i].title;
}
return model;
});
}
解决方案
您没有显示您遇到的错误,但是您尝试执行的操作可能存在两个问题。
第一个是request
通过回调异步返回其信息。dialogflow-fulfillment 库要求任何执行异步操作的 Intent Handler 返回一个 Promise。
虽然您可以将其包装在 Promise 中,但更简单的是使用可request
直接与 Promise 一起使用的版本,例如request-promise-native。(例如,参见https://stackoverflow.com/a/49751176/1405634)
您的另一个问题是,如果您使用 Dialogflow 的内置代码编辑器,那么您正在使用 Cloud Functions for Firebase。默认情况下,仅允许在 Google 网络内部进行网络调用。为了访问外部地址,您需要确保您使用的是付费计划,例如Blaze 计划。但是,有一个免费层足以用于开发和大多数测试,甚至可能是轻型生产。
推荐阅读
- python - python项目中的导入错误(pipenv,pytest)
- python - Selenium Python 的 IE webdriver 加载网页并进入停止状态
- python - 如何使用 Selenium 和 Python 从表中的元素中提取值
- java - 使用 Stream API 和引用方法将集合对象从一种类型转换为另一种类型
- javascript - 如何在 React js 中导出组件以在另一个项目中使用?
- darklang-beta - 如何在 Darklang 中删除个人画布?
- python-3.x - 如何获取数据框中每一行的相关系数
- python - 在python中重新格式化列表
- android - 从另一个小部件 Flutter 更改文本字段的 textSize
- r - 如何映射(匹配)数据框中的列和行?