[英]How to use multiple time web scraping in Dialogflow fulfillment inline? and how to detect error at fulfillment inline code?
I have some problem about using Dialogflow fulfillment inline for multiple web scraping .我对使用Dialogflow 实现内联进行多个 web 抓取有一些问题。
Below are some parts of code that relate with web scraping.以下是与 web 抓取相关的部分代码。
When I can request 1st web scraping only (not call 2nd scraping by comment 2nd '.then'), it is success to retrieve 'targetUrl' from 1st scraping.当我只能请求第一次 web 抓取时(不通过评论 2nd '.then' 调用第二次抓取),从第一次抓取中检索 'targetUrl' 是成功的。 And it can this display at Response Message on Dialogflow.
它可以显示在 Dialogflow 上的响应消息中。
But When I use 2nd scraping, 'targetUrl' from 1st scraping cannot display at Response Message on Dialogflow.但是当我使用第二次抓取时,来自第一次抓取的“targetUrl”无法显示在 Dialogflow 的响应消息中。 The Response Message is message, which set at Intent Response section.
响应消息是消息,设置在 Intent Response 部分。 But messages are added at Fulfillment did not appear.
但是在 Fulfillment 添加的消息没有出现。
I don't know what is happened.我不知道发生了什么事。 So my questions are
所以我的问题是
1. How to use multiple time web scraping in Dialogflow fulfillment inline? 1.如何在Dialogflow实现内联中多次使用web抓取?
2. How to detect error at fulfillment inline code? 2. 如何在履行内联代码中检测错误?
Thanks in advance提前致谢
index.js 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 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"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.