Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send parameters to webhook on dialogflow sdk v2

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: '[email protected]'
        }
      }
    }
  };

  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);
  }
like image 695
the_bluescreen Avatar asked Nov 30 '17 22:11

the_bluescreen


People also ask

What is Webhook in Dialogflow?

Webhooks are services that host your business logic. During a session, webhooks allow you to use the data extracted by Dialogflow's natural language processing to generate dynamic responses, validate collected data, or trigger actions on the backend.

What are actions and parameters in Dialogflow?

Dialogflow sends an API interaction response for each step of slot filling. For each of these slot filling responses, the intent and action will be the same, and the parameters collected so far will be provided. When building an agent, you provide prompts that the agent will use to get parameter data from the end-user.

Is Dialogflow deprecated?

June 13, 2022. The Dialogflow ES Google Assistant integration will be removed on June 13, 2023. This is due to the Google Assistant Conversational Actions planned sunsetting.


1 Answers

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);
  });
like image 75
matthewayne Avatar answered Oct 21 '22 01:10

matthewayne