Dialogflow webhook 意图响应中的 OpenSearch 调用没有 return 数据
OpenSearch call in Dialogflow webhook intent response does not return data
我正在开发一个应用程序,用于从 Dialogflow 聊天的图书馆目录中检索数据。我没有收到任何错误,而且我有一个附加到该服务的计费帐户。意图代码在这里:
const {WebhookClient} = require('dialogflow-fulfillment');
const function = require('firebase-functions');
const agent = new WebhookClient({ request: request, response: response });
const catalogSearch = require("rss-to-json");
exports.libraryChat = functions.https.onRequest(request, response) => {
function catalog_search(agent) {
var itemSubject = agent.parameters["item-subject"] ? "+" + agent.parameters["item-subject"] : "";
var itemTitle = agent.parameters["item-title"] ? "+" + agent.parameters["item-title"] : "";
var chatResponse = "";
var itemList = new Array();
if (agent.parameters["author-name"]) {
var authorName = agent.parameters["author-name"]["name"] ? "+" + agent.parameters["author-name"]["name"] + " " + agent.parameters["author-name"]["last-name"] : "+" + agent.parameters["author-name"]["last-name"];
}
var searchString = "";
if (itemSubject.length > 0) { searchString = searchString + itemSubject; }
if (itemTitle.length > 0 ) { searchString = searchString + itemTitle; }
if (authorName.length > 0) { searchString = searchString + authorName; }
var url = "https://gapines.org/opac/extras/opensearch/1.1/-/rss2-full?searchTerms=site(GCHR-CCO)" + searchString + "&searchClass=keyword";
console.log(url);
catalogSearch.load(url, (err, jsonResponse) => {
if (!err) {
itemList = jsonResponse.items;
chatResponse = "The first ten items returned for your search are: ";
}
else {
chatResponse = "I'm sorry! I've encountered an error while retrieving that data!";
}
});
itemList.forEach( (title, index) => {
chatResponse = chatResponse + (index + 1).toString() + title.title;
});
agent.add(chatResponse);
}
let intentMap = new Map();
intentMap.set("Catalog Search", catalog_search);
}
来自意图的 JSON 响应是:
{
"responseId": "958f0d66-13ba-4bf5-bed8-83480da4c37e",
"queryResult": {
"queryText": "Do you have any books about goats?",
"parameters": {
"item-subject": "goats",
"item-title": "",
"author-name": ""
},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [
{
"text": {
"text": [
""
]
}
}
],
"intent": {
"name": "projects/library-chatbot/agent/intents/a5f8ad9b-ff73-49f7-a8c0-351da3bf4802",
"displayName": "Catalog Search"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"webhook_latency_ms": 3591
},
"languageCode": "en"
},
"webhookStatus": {
"message": "Webhook execution successful"
}
}
我已经通过一个单独的函数验证了 Opensearch 调用有效并生成了一个标题列表,但是当通过 Dialogflow Intent 访问时没有任何返回。我以为可能是免费账户的限制,但是添加账单信息并升级后错误依旧。
或者更确切地说,缺少没有生成响应的错误。我在这里遗漏了什么吗?
你的第三行:
const agent = new WebhookClient({ request: request, response: response });
正在尝试实例化新代理,但 request 和 response 未在该范围内定义。尝试将该行移动到 libraryChat 函数体内。
问题:DialogFlow 代理在处理 Opensearch 结果之前返回响应。
解决方案:首先,路由聊天意图而不使用内置的 .handleRequest
方法来调用 Opensearch,返回承诺。
function chooseHandler(agent) {
var intentRoute = "";
switch (agent.intent) {
case "Default Welcome Intent":
intentRoute = "welcome";
break;
case "Default Fallback Intent":
intentRoute = "fallback";
break;
case "Catalog Search":
intentRoute = "catalog";
break;
}
return new Promise ((resolve, reject) => {
if (intentRoute.length > 0) {
resolve(intentRoute);
}
else {
reject(Error("Route Not Found"));
}
});
}
function assignResponse(intentRoute, agent) {
switch (intentRoute) {
case "welcome":
agent.handleRequest(welcome);
break;
case "fallback":
agent.handleRequest(fallback);
break;
case "catalog":
catalog_response();
break;
default:
Error("Intent not found");
}
}
接下来,从 OpenSearch API 检索数据,返回承诺。
function catalog_search(agent) {
var itemSubject = agent.parameters["item-subject"] ? "+" + agent.parameters["item-subject"] : "";
var itemTitle = agent.parameters["item-title"] ? "+" + agent.parameters["item-title"] : "";
var itemType = agent.parameters["item-type"] ? "+" + agent.parameters["item-type"] : "";
var authorName = "";
if (agent.parameters["author-name"]) {
authorName = agent.parameters["author-name"]["name"] ? "+" + agent.parameters["author-name"]["name"] + " " + agent.parameters["author-name"]["last-name"] : "+" + agent.parameters["author-name"]["last-name"];
}
var searchString = "";
if (itemSubject.length > 0) { searchString += itemSubject; }
if (itemTitle.length > 0 ) { searchString += itemTitle; }
if (authorName.length > 0) { searchString += authorName; }
if (itemType.length > 0) { searchString += itemType; }
var url = "https://gapines.org/opac/extras/opensearch/1.1/-/rss2-full?searchTerms=site(GCHR-CCO)" + encodeURIComponent(searchString) + "&searchClass=keyword";
console.log(url);
console.log(encodeURIComponent(searchString));
var itemList = [];
var chatResponse = "this was untouched";
return new Promise((resolve, reject) => {
catalogSearch.load(url, (err, rss) => {
if (err) {
reject(Error("url failed!"));
}
else {
itemList = rss;
if (itemList.items.length > 0) {
if (itemList.items.length === 1) {
chatResponse = "The only item found for that search is ";
}
else if (itemList.items.length < 10) {
chatResponse = "The first few items in your search are ";
}
else {
chatResponse = "The first ten items in your search are ";
}
itemList.items.forEach( (item, index) => {
chatResponse = chatResponse + item.title.replace(/\//g, "").trim();
if (index + 1 < itemList.items.length) {
chatResponse += ", ";
} else {
chatResponse += ".";
}
});
}
else {
chatResponse = "We're sorry, there are no items matching that search at our library!";
}
console.log(rss);
resolve(chatResponse);
}
});
});
}
创建格式正确的 JSON 对意图的响应,并将其发回。
function catalog_response() {
catalog_search(agent).then((response) => {
res.json({ "fulfillmentText": response });
res.status(200).send();
return 1;
}), (error) => {
console.log("failed", error);
};
}
我正在开发一个应用程序,用于从 Dialogflow 聊天的图书馆目录中检索数据。我没有收到任何错误,而且我有一个附加到该服务的计费帐户。意图代码在这里:
const {WebhookClient} = require('dialogflow-fulfillment');
const function = require('firebase-functions');
const agent = new WebhookClient({ request: request, response: response });
const catalogSearch = require("rss-to-json");
exports.libraryChat = functions.https.onRequest(request, response) => {
function catalog_search(agent) {
var itemSubject = agent.parameters["item-subject"] ? "+" + agent.parameters["item-subject"] : "";
var itemTitle = agent.parameters["item-title"] ? "+" + agent.parameters["item-title"] : "";
var chatResponse = "";
var itemList = new Array();
if (agent.parameters["author-name"]) {
var authorName = agent.parameters["author-name"]["name"] ? "+" + agent.parameters["author-name"]["name"] + " " + agent.parameters["author-name"]["last-name"] : "+" + agent.parameters["author-name"]["last-name"];
}
var searchString = "";
if (itemSubject.length > 0) { searchString = searchString + itemSubject; }
if (itemTitle.length > 0 ) { searchString = searchString + itemTitle; }
if (authorName.length > 0) { searchString = searchString + authorName; }
var url = "https://gapines.org/opac/extras/opensearch/1.1/-/rss2-full?searchTerms=site(GCHR-CCO)" + searchString + "&searchClass=keyword";
console.log(url);
catalogSearch.load(url, (err, jsonResponse) => {
if (!err) {
itemList = jsonResponse.items;
chatResponse = "The first ten items returned for your search are: ";
}
else {
chatResponse = "I'm sorry! I've encountered an error while retrieving that data!";
}
});
itemList.forEach( (title, index) => {
chatResponse = chatResponse + (index + 1).toString() + title.title;
});
agent.add(chatResponse);
}
let intentMap = new Map();
intentMap.set("Catalog Search", catalog_search);
}
来自意图的 JSON 响应是:
{
"responseId": "958f0d66-13ba-4bf5-bed8-83480da4c37e",
"queryResult": {
"queryText": "Do you have any books about goats?",
"parameters": {
"item-subject": "goats",
"item-title": "",
"author-name": ""
},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [
{
"text": {
"text": [
""
]
}
}
],
"intent": {
"name": "projects/library-chatbot/agent/intents/a5f8ad9b-ff73-49f7-a8c0-351da3bf4802",
"displayName": "Catalog Search"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"webhook_latency_ms": 3591
},
"languageCode": "en"
},
"webhookStatus": {
"message": "Webhook execution successful"
}
}
我已经通过一个单独的函数验证了 Opensearch 调用有效并生成了一个标题列表,但是当通过 Dialogflow Intent 访问时没有任何返回。我以为可能是免费账户的限制,但是添加账单信息并升级后错误依旧。
或者更确切地说,缺少没有生成响应的错误。我在这里遗漏了什么吗?
你的第三行:
const agent = new WebhookClient({ request: request, response: response });
正在尝试实例化新代理,但 request 和 response 未在该范围内定义。尝试将该行移动到 libraryChat 函数体内。
问题:DialogFlow 代理在处理 Opensearch 结果之前返回响应。
解决方案:首先,路由聊天意图而不使用内置的 .handleRequest
方法来调用 Opensearch,返回承诺。
function chooseHandler(agent) {
var intentRoute = "";
switch (agent.intent) {
case "Default Welcome Intent":
intentRoute = "welcome";
break;
case "Default Fallback Intent":
intentRoute = "fallback";
break;
case "Catalog Search":
intentRoute = "catalog";
break;
}
return new Promise ((resolve, reject) => {
if (intentRoute.length > 0) {
resolve(intentRoute);
}
else {
reject(Error("Route Not Found"));
}
});
}
function assignResponse(intentRoute, agent) {
switch (intentRoute) {
case "welcome":
agent.handleRequest(welcome);
break;
case "fallback":
agent.handleRequest(fallback);
break;
case "catalog":
catalog_response();
break;
default:
Error("Intent not found");
}
}
接下来,从 OpenSearch API 检索数据,返回承诺。
function catalog_search(agent) {
var itemSubject = agent.parameters["item-subject"] ? "+" + agent.parameters["item-subject"] : "";
var itemTitle = agent.parameters["item-title"] ? "+" + agent.parameters["item-title"] : "";
var itemType = agent.parameters["item-type"] ? "+" + agent.parameters["item-type"] : "";
var authorName = "";
if (agent.parameters["author-name"]) {
authorName = agent.parameters["author-name"]["name"] ? "+" + agent.parameters["author-name"]["name"] + " " + agent.parameters["author-name"]["last-name"] : "+" + agent.parameters["author-name"]["last-name"];
}
var searchString = "";
if (itemSubject.length > 0) { searchString += itemSubject; }
if (itemTitle.length > 0 ) { searchString += itemTitle; }
if (authorName.length > 0) { searchString += authorName; }
if (itemType.length > 0) { searchString += itemType; }
var url = "https://gapines.org/opac/extras/opensearch/1.1/-/rss2-full?searchTerms=site(GCHR-CCO)" + encodeURIComponent(searchString) + "&searchClass=keyword";
console.log(url);
console.log(encodeURIComponent(searchString));
var itemList = [];
var chatResponse = "this was untouched";
return new Promise((resolve, reject) => {
catalogSearch.load(url, (err, rss) => {
if (err) {
reject(Error("url failed!"));
}
else {
itemList = rss;
if (itemList.items.length > 0) {
if (itemList.items.length === 1) {
chatResponse = "The only item found for that search is ";
}
else if (itemList.items.length < 10) {
chatResponse = "The first few items in your search are ";
}
else {
chatResponse = "The first ten items in your search are ";
}
itemList.items.forEach( (item, index) => {
chatResponse = chatResponse + item.title.replace(/\//g, "").trim();
if (index + 1 < itemList.items.length) {
chatResponse += ", ";
} else {
chatResponse += ".";
}
});
}
else {
chatResponse = "We're sorry, there are no items matching that search at our library!";
}
console.log(rss);
resolve(chatResponse);
}
});
});
}
创建格式正确的 JSON 对意图的响应,并将其发回。
function catalog_response() {
catalog_search(agent).then((response) => {
res.json({ "fulfillmentText": response });
res.status(200).send();
return 1;
}), (error) => {
console.log("failed", error);
};
}