19

I'm trying to send some parameters to dialogflow (api.ai) such as username, email, etc but I couldn't figure it out. The problem is I cannot get/set any specific data (such as username, email, etc.) with Dialogflow v2 Nodejs SDK. I tried to use queryParams.payload (v1: originalRequest) but It didn't work somehow. Also, I tried to trigger custom event with data but I couldn't get any event data on the response. Does someone know how to send some specific data for session talk on dialogFlow?

EXAMPLE OF PAYLOAD

  const projectId = 'test-bot-test-1111';
  const sessionId = user.uuid;
  const languageCode = 'en-GB';

  const sessionClient = new dialogFlow.SessionsClient();
  const sessionPath = sessionClient.sessionPath(projectId, sessionId);

  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        text: query,
        languageCode
      }
    },
    queryParams: {
      payload: {
        data: {
           username: 'bob',
           email: 'bob@test.com'
        }
      }
    }
  };

  let resultReq;

  console.log('request :: ', request, '\n\n');

  try {
    resultReq = await sessionClient.detectIntent(request);
  } catch (err) {
    // eslint-disable-next-line no-console
    return console.error('ERROR:', err);
  }

EXAMPLE OF EVENT

  const projectId = 'test-bot-test-1111';
  const sessionId = user.uuid;
  const languageCode = 'en-GB';

  const sessionClient = new dialogFlow.SessionsClient();
  const sessionPath = sessionClient.sessionPath(projectId, sessionId);

const request = {
    session: sessionPath,
    queryInput: {
      event: {
        name: 'custom_event',
        languageCode,
        parameters: {
          name: 'sam',
          user_name: 'sam',
          a: 'saaaa'
        }
      }
    },
    queryParams: {
      payload: {
        data: user
      }
    }
  };

  let resultReq;

  console.log('request :: ', request, '\n\n');

  try {
    resultReq = await sessionClient.detectIntent(request);
  } catch (err) {
    // eslint-disable-next-line no-console
    return console.error('ERROR:', err);
  }
the_bluescreen
  • 2,341
  • 2
  • 15
  • 28

1 Answers1

24

Dialogflow's v2 API uses gRPC and has a few quirks, one of which you've run into. If you look at the samples for the Node.js library you can see how to workaround this. You'll need to impliment a jsonToStructProto method to convert your JavaScript object to a proto struct or just copy the structjson.js file in the sample in this gist. Below is a fully working example using the structjson.js file:

// Imports the Dialogflow library
const dialogflow = require('dialogflow');

// Import the JSON to gRPC struct converter
const structjson = require('./structjson.js');

// Instantiates a sessison client
const sessionClient = new dialogflow.SessionsClient();

// The path to identify the agent that owns the created intent.
const sessionPath = sessionClient.sessionPath(projectId, sessionId);

// The text query request.
const request = {
  session: sessionPath,
  queryInput: {
    event: {
      name: eventName,
      parameters: structjson.jsonToStructProto({foo: 'bar'}),
      languageCode: languageCode,
    },
  },
};

sessionClient
  .detectIntent(request)
  .then(responses => {
    console.log('Detected intent');
    logQueryResult(sessionClient, responses[0].queryResult);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });
matthewayne
  • 3,335
  • 12
  • 15
  • Doesn't work for me, parameters are still empty, even though I do exactly the same – Vadorequest Dec 12 '18 at 22:58
  • Finally figured it out by using `request.queryParams.payload` to store my data, example there: https://stackoverflow.com/questions/51096251/how-to-set-a-custom-platform-in-dialogflow-nodejs-client – Vadorequest Dec 12 '18 at 23:10
  • 1
    Just updated. Here you go: https://gist.github.com/matthewayne/e9dbd9a428420c3af399cb03d6b526b9 – matthewayne Apr 17 '19 at 23:16
  • it's funny that in docs it says `Optional. This field can be used to pass custom data into the webhook associated with the agent. Arbitrary JSON objects are supported.` but it doesn't work https://cloud.google.com/dialogflow/docs/reference/rpc/google.cloud.dialogflow.v2?authuser=1#google.cloud.dialogflow.v2.QueryParameters – Reza Sep 04 '19 at 20:49
  • 1
    The pb-util package is now used in favor of structjson: https://stackoverflow.com/a/59537762/925478 – Myk Willis Jan 31 '20 at 21:00