Search code examples
dialogflow-esdialogflow-cx

How to use Dialogflow CX API pass parameters to webhook?It seemed that detectIntent() set session queryParams does not work


According to the google dialogflow cx document: https://cloud.google.com/dialogflow/cx/docs/concept/parameter https://cloud.google.com/dialogflow/cx/docs/reference/rest/v3/QueryParameters

click to show the reference I know we can use api to set session parameters. So I want pass the parameters to webhook by API way.

Step 1:Front-end, use detectintent() API, fill the queryParams item.
Step 2:Google dialogflow cx server will set parameters as the session parameter.
Step 3:Webhook receive the google-side fucnction call.We can found all of the session parameters from the http request.

As far as I am concerned, I can only receive the variables set in the DialogFlow Agent, but I have not received any parameters set via detectintent() API. I think I must have done something wrong, who can tell me how to? thanks.

My code as below(Nodejs code):

  const sessionPath = client.projectLocationAgentSessionPath(
    projectId,
    location,
    agentId,
    sessionId
  );
 
  var mapParameters = new Map([['Michael', 95], ['Bob', 75], ['Tracy', 85]]);
 
 const request = {
    session: sessionPath,
    
    "queryInput": {
      
      "text": {
        "text": query,
      },
      languageCode,
    },

    'queryParams': {
      'timeZone': 'America/Los_Angeles',
      'parameters': {
        "fields":mapParameters
      }
    },

 };
const [response] = await client.detectIntent(request);

Solution

  • The problem has been solved. It needs to be serialized/transformed.

    So for the parameters' type mentioned in document, It need to be sent to DialogFlow As a "struct".

    Depending on your protocol or client library language, this is a map, associative array, symbol table, dictionary, or JSON object composed of a collection of (MapKey, MapValue) pairs:

    The right structure as bleow:

    {
      session: 'projects/xxxxx...........',
      queryInput: {
        text: { text: 'hello world!' },
        languageCode: 'en'
      },
      queryParams: {
        timeZone: 'America/Los_Angeles',
        parameters: {
          fields: {
            ID: { kind: 'numberValue', numberValue: 5 },
            Email: { kind: 'stringValue', stringValue: '[email protected]' },
            Phone: { kind: 'stringValue', stringValue: '7789511xxx' },
            Domain: { kind: 'stringValue', stringValue: 'xxxxxx.com' }
          }
        }
      }
    }