首页 > 解决方案 > 在 DialogFlow 中添加对知识库的附加链接响应

问题描述

借助 DialogFlow 中的知识库,您可以上传基本的常见问题解答。第一列是问题,第二列是答案。通常我们需要让答案提供额外的链接。例如,“你的问题的答案是否定的,菠萝不适合做披萨”。更多信息

我想提供的是答案,后面是丰富的响应链接。由于电子表格中不能有第三列来添加链接,我怎样才能干净地做到这一点?当然,我可以将链接作为答案文本的一部分,但这并不那么漂亮。

我能够复制这个问题,然后提供一个链接作为第二个答案(即 $Knowledge.Answer(2)),但是有些答案没有链接,我不能让它成为有条件的。

我假设我可以在 Fulfillment 中执行此操作,但我不确定可以返回答案 ($Knowledge.Answer(1)) 的实际代码,然后有条件地添加带有链接的丰富响应。

标签: dialogflow-es

解决方案


我终于解决了。

首先,我在回答中使用了一些分隔符(这里只是“Z$Z”)。

例如,在 csv 文件中,我将以下内容放在第二列中:

你的问题的答案是否定的,菠萝不吃披萨$Z$更多信息$Z$https://www.mashed.com/183299/the-most-controversial-pizza-toppings-explained/

这样,当我得到满足时,我就可以解析答案。这是代码的一部分:

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  // save off the answer to parse
  let answer = JSON.stringify(request.body.queryResult.fulfillmentText);   
  
  function faqHandler(agent) {
    let answerAlt = answer.slice(1,-1); // remove first and last double quote
    let answerParse = answerAlt.split("Z$Z"); // parse the answer
    if( answerParse.length == 1) {
      // no links
      agent.add(answerParse[0].toString());
    } else {
      // display the content with the text and links
      // you can use different ways here to display but you have access to
      // the answer, title, and url as follows
      // answerParse[0].toString(), answerParse[1].toString(), answerParse[1].toString()
    }
  }
  ...
  intentMap.set('Knowledge.KnowledgeBase.ID_OF_KNOWLEDGEBASE',faqHandler);

推荐阅读