代理无法打印从 Dialogflow 中的 Axion 库请求收到的所有结果
Agent unable to print all results received from Axion library request in Dialogflow
我正在尝试打印从以下请求收到的所有结果(此代码无效):
function searchForProducts(agent) {
// category_name = 'Cooking' for example
const category_name = agent.parameters.category_name;
return new Promise((resolve, reject) => {
axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
let Categories = res.data[0];
if (Categories) {
for(var i=0;i<res.data.length;i++){
agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}\n\n${Categories.Name}`);
}
} else {
agent.add(`No items found in the selected category (${category_name})`);
}
resolve();
});
});
}
我面临的问题是代理只能通过以下代码打印结果(此记录有效但 return 只有一个 URL):
function searchForProducts(agent) {
const category_name = agent.parameters.category_name;
return new Promise((resolve, reject) => {
axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
let Categories = res.data[0];
if (Categories) {
agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}\n\n${Categories.Name}`);
} else {
agent.add(`No items found in the selected category (${category_name})`);
}
resolve();
});
});
}
我做错了什么?
============ 应用建议的解决方案后 ================
您好,从昨天开始我一直在测试,但没有找到确切的问题。这是我所做的:
- 我创建了新的 Intent,它会在收到“测试”后触发您的代码。
- 我用不同的方式测试了代码,这里是结果:
Category_Name = “Cooking”
:
- Dialogflow 代理测试: 有效(https://imgur.com/sov6Th5)。
- Web 代理测试: 未成功 (https://imgur.com/15qxgdR)。
- Dialogflow Web Messenger: 有效(https://imgur.com/5ajzd2j)。
- Twilio: 没用(https://imgur.com/fsrYtDG) and error message was (https://imgur.com/jP6TRbZ)。
但是,当我更改 Category_Name = “Small%20Appliances”
:
- Dialogflow 代理测试: 有效(https://imgur.com/undefined)。
- Web 代理测试: 有效(https://imgur.com/undefined)。
- Dialogflow Web Messenger: 有效(https://imgur.com/rCn8ksT)。
- Twilio: 有效(https://imgur.com/kfXGqTf)。
这里有一个link用于网络测试(我会保留Category_name=’Cooking’
)所以你可以看到结果:
https://bot.dialogflow.com/004077c2-d426-472c-89f0-4997e2955d59
你怎么看?
使用上面的代码,我能够遍历 agent.add()
并得到结果。我使用 test
作为匹配意图“测试意图”的用户输入。
这是您提供的代码的 29 个相似输出的片段:
完整代码如下:
index.js
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const axios = require('axios');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
function yourFunctionHandler(agent) {
const category_name = 'Cooking';
return new Promise((resolve, reject) => {
axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
console.log(res.data);
let Categories = res.data[0];
if (Categories) {
for(var i=0;i<res.data.length;i++){
agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}\n\n${Categories.Name}`);
}
} else {
agent.add(`No items found in the selected category (${category_name})`);
}
resolve();
});
});
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('test intent', yourFunctionHandler);
agent.handleRequest(intentMap);
});
package.json
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "10"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"firebase-functions": "^2.0.2",
"firebase-admin": "^5.13.1",
"googleapis": "^27.0.0",
"actions-on-google": "2.2.0",
"dialogflow-fulfillment": "0.6.1",
"axios": "0.21.1"
}
}
只是一个建议,因为您的 objective 是要显示某个“类别”下的所有产品,您可能想要遍历整个 res.data
。在此示例中,我仅使用了 category_name = 'Cooking'
并打印了指定类别下的所有产品。
function yourFunctionHandler(agent) {
const category_name = 'Cooking';
return new Promise((resolve, reject) => {
axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
let Categories = res.data;
if (Categories) {
for (const product of Categories ){
agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${product.ProductID}\n\n${product.Name}`);
}
} else {
agent.add(`No items found in the selected category (${category_name})`);
}
resolve();
});
});
}
这将导致:
我正在尝试打印从以下请求收到的所有结果(此代码无效):
function searchForProducts(agent) {
// category_name = 'Cooking' for example
const category_name = agent.parameters.category_name;
return new Promise((resolve, reject) => {
axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
let Categories = res.data[0];
if (Categories) {
for(var i=0;i<res.data.length;i++){
agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}\n\n${Categories.Name}`);
}
} else {
agent.add(`No items found in the selected category (${category_name})`);
}
resolve();
});
});
}
我面临的问题是代理只能通过以下代码打印结果(此记录有效但 return 只有一个 URL):
function searchForProducts(agent) {
const category_name = agent.parameters.category_name;
return new Promise((resolve, reject) => {
axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
let Categories = res.data[0];
if (Categories) {
agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}\n\n${Categories.Name}`);
} else {
agent.add(`No items found in the selected category (${category_name})`);
}
resolve();
});
});
}
我做错了什么?
============ 应用建议的解决方案后 ================
您好,从昨天开始我一直在测试,但没有找到确切的问题。这是我所做的:
- 我创建了新的 Intent,它会在收到“测试”后触发您的代码。
- 我用不同的方式测试了代码,这里是结果:
Category_Name = “Cooking”
:
- Dialogflow 代理测试: 有效(https://imgur.com/sov6Th5)。
- Web 代理测试: 未成功 (https://imgur.com/15qxgdR)。
- Dialogflow Web Messenger: 有效(https://imgur.com/5ajzd2j)。
- Twilio: 没用(https://imgur.com/fsrYtDG) and error message was (https://imgur.com/jP6TRbZ)。
但是,当我更改 Category_Name = “Small%20Appliances”
:
- Dialogflow 代理测试: 有效(https://imgur.com/undefined)。
- Web 代理测试: 有效(https://imgur.com/undefined)。
- Dialogflow Web Messenger: 有效(https://imgur.com/rCn8ksT)。
- Twilio: 有效(https://imgur.com/kfXGqTf)。
这里有一个link用于网络测试(我会保留Category_name=’Cooking’
)所以你可以看到结果:
https://bot.dialogflow.com/004077c2-d426-472c-89f0-4997e2955d59
你怎么看?
使用上面的代码,我能够遍历 agent.add()
并得到结果。我使用 test
作为匹配意图“测试意图”的用户输入。
这是您提供的代码的 29 个相似输出的片段:
完整代码如下:
index.js
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const axios = require('axios');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
function yourFunctionHandler(agent) {
const category_name = 'Cooking';
return new Promise((resolve, reject) => {
axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
console.log(res.data);
let Categories = res.data[0];
if (Categories) {
for(var i=0;i<res.data.length;i++){
agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}\n\n${Categories.Name}`);
}
} else {
agent.add(`No items found in the selected category (${category_name})`);
}
resolve();
});
});
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('test intent', yourFunctionHandler);
agent.handleRequest(intentMap);
});
package.json
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "10"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"firebase-functions": "^2.0.2",
"firebase-admin": "^5.13.1",
"googleapis": "^27.0.0",
"actions-on-google": "2.2.0",
"dialogflow-fulfillment": "0.6.1",
"axios": "0.21.1"
}
}
只是一个建议,因为您的 objective 是要显示某个“类别”下的所有产品,您可能想要遍历整个 res.data
。在此示例中,我仅使用了 category_name = 'Cooking'
并打印了指定类别下的所有产品。
function yourFunctionHandler(agent) {
const category_name = 'Cooking';
return new Promise((resolve, reject) => {
axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
let Categories = res.data;
if (Categories) {
for (const product of Categories ){
agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${product.ProductID}\n\n${product.Name}`);
}
} else {
agent.add(`No items found in the selected category (${category_name})`);
}
resolve();
});
});
}
这将导致: