代理无法在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}
${Categories.Name}`);
}
} else {
agent.add(`No items found in the selected category (${category_name})`);
}
resolve();
});
});
}
我面临的问题是工程师只能通过以下代码打印结果(此记录有效,但仅返回一个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}
${Categories.Name}`);
} else {
agent.add(`No items found in the selected category (${category_name})`);
}
resolve();
});
});
}
我做错了什么?
=应用建议的解决方案后=
您好,从昨天开始我正在测试,但是没有运气得到确切的问题。我这样做了:- 我创建了新的意图,它将在收到";Test";后触发您的代码。
- 我用不同的方法测试了代码,这里的结果是:
Category_Name = "Cooking"
:
-Dialogflow Agent测试:已工作(https://imgur.com/sov6Th5)。
-Web代理测试:未工作(https://imgur.com/15qxgdR)。
-Dialogflow Web Messenger:已工作(https://imgur.com/5ajzd2j)。
-Twilio:不工作(https://imgur.com/fsrYtDG),错误消息为(https://imgur.com/jP6TRbZ)。
但是,当我更改Category_Name = "Small%20Appliances"
时:
-Dialogflow Agent测试:已工作(https://imgur.com/undefined)。
-Web代理测试:已工作(https://imgur.com/undefined)。
-Dialogflow Web Messenger:已工作(https://imgur.com/rCn8ksT)。
-Twilio:已工作(https://imgur.com/kfXGqTf)。
这里是Web测试的链接(我将保留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}
${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"
}
}
只是一个建议,因为您的目标是显示某一类别&q;下的所有产品,因此您可能希望遍历整个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}
${product.Name}`);
}
} else {
agent.add(`No items found in the selected category (${category_name})`);
}
resolve();
});
});
}
这篇关于代理无法在Dialogflow中打印从Axion库请求接收的所有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!