簡體   English   中英

如何在 dialogflow webhook 中創建上下文並保存參數

[英]How to create context and save parameters in dialogflow webhook

我想創建一個允許用戶發送 2 個參數的意圖,但是,似乎有些錯誤我無法理解。 它在 dialogflow 控制台中運行良好

這是我在控制台中的 dialogflow 代理:

在此處輸入圖像描述

這是我的 node.js function

function test(agent) {

      first = agent.parameters['first'];
      console.log("first");//nothing is returned so I think it does not enter the function 
      second = agent.parameters['second'];

      agent.context.set({
              "name": 'test',
              "lifespan": 2,
              "parameters": {
                "first":first,
                "second":second
              }
      });
      if (first != "") {
        agent.add("the 1st text is: " + first);
      } if (second != "") {
        agent.add("the 2nd text is" + second);
      }
}

聊天機器人的結果

在此處輸入圖像描述

我將衷心感謝您的幫助

根據您的要求,我嘗試在我這邊復制場景。 我從控制台和使用 Dialogflow Fulfillment Webhook 都嘗試了相同的方法,在這兩種情況下我都得到了預期的 output。

您可以參考下面提到的步驟:

  • 創建一個意圖測試
  • 將訓練短語添加到意圖中,例如“first”、“second”,並用@sys.any標記它們
  • 在“操作”和“參數”字段中,定義提示,例如“首先是什么”和“第二個是什么”。

在此處輸入圖像描述

  • 您需要為此意圖啟用 fulfillment。

根據您提供的代碼,您使用了“agent.context.set”字段。 由於您未在 Intent 中使用任何Input 和 Output 上下文,因此無需在 webhook 代碼中定義它們。

您可以參考下面的Node.js代碼。 我已將Node.js 10 客戶端庫用於對話流。

索引.js:

'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.slot = 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));

 function going(agent){
 const value1=agent.parameters[`first1`];
 const value2=agent.parameters[`second2`];
  agent.add(`we received ` +value1+ ` in first field and ` +value2 + ` in second field as per your input`)
 }  

 let intentMap = new Map();
 intentMap.set('test',going);
  agent.handleRequest(intentMap);
});

Package.json:

{
 "name" : "slot",
"description": "This is the webhook",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
  "node": "10"
},
 "dependencies": {
  "actions-on-google": "^2.2.0",
  "firebase-admin": "^5.13.1",
  "firebase-functions": "^2.0.2",
  "dialogflow": "^0.6.0",
  "dialogflow-fulfillment": "^0.6.1"
}
}

output: 在此處輸入圖像描述

當您在 Dialogflow 中使用 Webhook 時,要使用 Webhook 從 Intents 中獲取參數,我們使用agent.parameters[`parameter name`] function。該參數需要出現在 Dialogflow 控制台中,否則 Webhook 無法獲取繼續對話流的參數值。

在下面的屏幕截圖中,您可以使用agent.parameters[`parameter name`] function 檢查從 webhook 獲取的參數值。

在此處輸入圖像描述

我創建了一個示例網站,並在那里部署了 Dialogflow Messenger,它按預期工作。

您可以參考以下網站截圖: 在此處輸入圖像描述

您能否嘗試使用 webhook Node.js 代碼以及我在解決方案中提供的 package.json 文件。

如果這對您不起作用,請提供您的代理 Zip 文件以及完整的 Webhook 代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM