如何在 Dialogflow fulfillment inline 中使用多次网络抓取?以及如何检测执行内联代码中的错误?
How to use multiple time web scraping in Dialogflow fulfillment inline? and how to detect error at fulfillment inline code?
我在使用 Dialogflow fulfillment inline 进行多个网络抓取时遇到了一些问题。
以下是与网络抓取相关的部分代码。
当我只能请求第一次网络抓取时(不是通过评论 2nd '.then' 调用第二次抓取),从第一次抓取中检索 'targetUrl' 是成功的。它可以在 Dialogflow 上的响应消息中显示。
但是当我使用第二次抓取时,第一次抓取的 'targetUrl' 无法显示在 Dialogflow 的响应消息中。 Response Message是消息,在Intent Response部分设置。但是在Fulfillment添加的消息没有出现。
我不知道发生了什么。所以我的问题是
1.如何在 Dialogflow fulfillment inline 中使用多次网页抓取?
2。如何检测执行内联代码中的错误?
提前致谢
index.js
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const {Payload} = require('dialogflow-fulfillment');
const rp = require('request-promise-native');
const rp2 = require('request-promise-native');
const cheerio = require('cheerio');
.....
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
.....
.....
function loadElements(data) { // --> function for load data from 2nd web scraping
$ = cheerio.load(data);
resultPrizeElems = $('td.border-0');
}
function gathering2WebScrapList(agent) { // --> function for 2nd web scraping
return rp2(targetUrl).then((data) => { // -- Request 2nd web scraping use 'targetUrl value from 1st web scraping
loadElements(data); // cheerio load result from 2nd web scraping
agent.add(resultPrizeElems.length.toString()); // --> Add array size to 'agent' for checking but nothing happen at Dialogflow Response
return Promise.resolve(agent);
}).catch((error) => {
console.log(error);
});
}
function loadTargetDateElements(data) { // --> function for load data from 1st web scraping --> 'success'
$ = cheerio.load(data);
targetDateElems = $('div.radio');
}
function load1stWebScrapData(agent) {
return rp(`http://www.firstdomain.com/page.aspx`).then((data) => { // 'Success' to 1st web scraping
loadTargetDateElements(data); // use cheerio for load html element success
....
targetUrl = assign value from get something when 1st web scraping
....
agent.add(targetUrl); // add 'targetUrl' value to display on Response Message 'OK'
return Promise.resolve(agent);
}).then((agent) => { // Second 'then'
return gathering2WebScrapList(agent); // --> function call request 2nd web scraping
}).catch((error) => {
console.log(error);
});
}
intentMap.set('Load Data', load1stWebScrapData);
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": {
"actions-on-google": "^2.12.0",
"cheerio": "^1.0.0-rc.3",
"iconv-lite": "^0.6.0",
"firebase-admin": "^8.12.1",
"firebase-functions": "^3.7.0",
"dialogflow": "^1.2.0",
"dialogflow-fulfillment": "^0.6.1",
"request": "^2.88.2",
"request-promise-native": "^1.0.8"
}
}
要调试内联编辑器代码,您必须使用 Google 云控制台中的 Google Stackdriver。
我在使用 Dialogflow fulfillment inline 进行多个网络抓取时遇到了一些问题。
以下是与网络抓取相关的部分代码。
当我只能请求第一次网络抓取时(不是通过评论 2nd '.then' 调用第二次抓取),从第一次抓取中检索 'targetUrl' 是成功的。它可以在 Dialogflow 上的响应消息中显示。
但是当我使用第二次抓取时,第一次抓取的 'targetUrl' 无法显示在 Dialogflow 的响应消息中。 Response Message是消息,在Intent Response部分设置。但是在Fulfillment添加的消息没有出现。
我不知道发生了什么。所以我的问题是
1.如何在 Dialogflow fulfillment inline 中使用多次网页抓取?
2。如何检测执行内联代码中的错误?
提前致谢
index.js
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const {Payload} = require('dialogflow-fulfillment');
const rp = require('request-promise-native');
const rp2 = require('request-promise-native');
const cheerio = require('cheerio');
.....
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
.....
.....
function loadElements(data) { // --> function for load data from 2nd web scraping
$ = cheerio.load(data);
resultPrizeElems = $('td.border-0');
}
function gathering2WebScrapList(agent) { // --> function for 2nd web scraping
return rp2(targetUrl).then((data) => { // -- Request 2nd web scraping use 'targetUrl value from 1st web scraping
loadElements(data); // cheerio load result from 2nd web scraping
agent.add(resultPrizeElems.length.toString()); // --> Add array size to 'agent' for checking but nothing happen at Dialogflow Response
return Promise.resolve(agent);
}).catch((error) => {
console.log(error);
});
}
function loadTargetDateElements(data) { // --> function for load data from 1st web scraping --> 'success'
$ = cheerio.load(data);
targetDateElems = $('div.radio');
}
function load1stWebScrapData(agent) {
return rp(`http://www.firstdomain.com/page.aspx`).then((data) => { // 'Success' to 1st web scraping
loadTargetDateElements(data); // use cheerio for load html element success
....
targetUrl = assign value from get something when 1st web scraping
....
agent.add(targetUrl); // add 'targetUrl' value to display on Response Message 'OK'
return Promise.resolve(agent);
}).then((agent) => { // Second 'then'
return gathering2WebScrapList(agent); // --> function call request 2nd web scraping
}).catch((error) => {
console.log(error);
});
}
intentMap.set('Load Data', load1stWebScrapData);
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": {
"actions-on-google": "^2.12.0",
"cheerio": "^1.0.0-rc.3",
"iconv-lite": "^0.6.0",
"firebase-admin": "^8.12.1",
"firebase-functions": "^3.7.0",
"dialogflow": "^1.2.0",
"dialogflow-fulfillment": "^0.6.1",
"request": "^2.88.2",
"request-promise-native": "^1.0.8"
}
}
要调试内联编辑器代码,您必须使用 Google 云控制台中的 Google Stackdriver。