简体   繁体   中英

How to create context and save parameters in dialogflow webhook

I want to create an intent that allow user to send 2 parameters, however, something seems to be wrong that I can't figure. It works fine in dialogflow console

Here is my dialogflow agent inside the console:

在此处输入图像描述

Here is my 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);
      }
}

The result of the chatbot

在此处输入图像描述

I would really appreciate your help

As per your requirement I tried replicating the scenario on my end. I tried the same both from Console and using Dialogflow Fulfillment Webhook and in both cases I am getting the expected output.

You can refer to the below mentioned steps:

  • Create one Intent test
  • Add training phrases to the intent like "first","second" and tag them with @sys.any
  • In the Action and Parameter field define the prompts like "What is first" for first and "what is second" for second.

在此处输入图像描述

  • You need to enable the fulfillment for this intent.

As per the code you have provided, you have used the “agent.context.set” field. Since you are not using any Input and Output Contexts in your Intent, it is not required to define them in your webhook code.

You can refer to the below Node.js code. I have used the Node.js 10 client library for dialogflow.

Index.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: 在此处输入图像描述

When you are using a Webhook in Dialogflow, to fetch the parameters from the Intents using webhook, we use the agent.parameters[`parameter name`] function. The parameter needs to be present in the Dialogflow console or else the Webhook cannot fetch the Parameter values to continue the conversation flow.

In the below screenshot you can check the parameter value is fetched from the webhook using the agent.parameters[`parameter name`] function.

在此处输入图像描述

I created a sample website and there I have deployed the Dialogflow Messenger and it is working as expected.

You can refer to the below website screenshot: 在此处输入图像描述

Could you please try using the webhook Node.js code along with the package.json file I have provided in the solution.

If this does not work for you, then please provide your Agent Zip file along with the full Webhook code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM