简体   繁体   English

如何在 Dialogflow 实现内联中多次使用 web 抓取? 以及如何在履行内联代码中检测错误?

[英]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"
  }
}

To debug the inline editor code you have to use the Google Stackdriver from the Google Cloud console.要调试内联编辑器代码,您必须使用 Google Cloud 控制台中的 Google Stackdriver。

dialogfow 内联编辑器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在对话流实现中使用 OR 运算符 - How to use OR operator in dialogflow fulfillment 如何通过代码在DialogFlow实现中创建意图? - How to create intent in DialogFlow Fulfillment by code? 如何制作多语言Dialogflow Webhook /实现? - How to make a multiple language Dialogflow webhook/fulfillment? 如何使用dialogflow-fulfillment-nodejs与Express - How use dialogflow-fulfillment-nodejs with Express Dialogflow 检测意图实现 - Dialogflow Detect Intent Fulfillment 您可以在dialogflow内联编辑器中实现两个不同的实现吗? - Can you have two different fulfillment's in the dialogflow inline editor? 我可以在 Inline Fulfillment Dialogflow 编辑器中跨对话存储计数器吗? - Can I store a counter across conversation in Inline Fulfillment Dialogflow editor? 在使用内联编辑器实现的 DialogFlow Messenger 中,setContext() 干扰 setFollowupEvent() - in DialogFlow Messenger using inline editor for fulfillment, setContext() is interfering with setFollowupEvent() 如何使用履行代码在 Dialogflow 中设置 agent.parameters? - How to set agent.parameters in Dialogflow using fulfillment code? 如何使用Node.js或Python在对话框流实现中使用custom_payload - How to use custom_payload in dialogflow fulfillment using nodejs or python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM