简体   繁体   English

将参数发送到 dialogflow sdk v2 上的 webhook

[英]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.我正在尝试向 dialogflow (api.ai) 发送一些参数,例如用户名、电子邮件等,但我无法弄清楚。 The problem is I cannot get/set any specific data (such as username, email, etc.) with Dialogflow v2 Nodejs SDK.问题是我无法使用 Dialogflow v2 Nodejs SDK 获取/设置任何特定数据(例如用户名、电子邮件等)。 I tried to use queryParams.payload (v1: originalRequest ) but It didn't work somehow.我尝试使用queryParams.payload (v1: originalRequest ) 但它以某种方式不起作用。 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?有人知道如何在 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);
  }

Dialogflow's v2 API uses gRPC and has a few quirks, one of which you've run into. Dialogflow 的 v2 API 使用 gRPC 并有一些怪癖,您已经遇到过其中之一。 If you look at the samples for the Node.js library you can see how to workaround this.如果您查看 Node.js 库的示例,您会看到如何解决此问题。 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 .您需要实现一个jsonToStructProto方法来将您的 JavaScript 对象转换为 proto 结构,或者只是复制structjson.js示例中的structjson.js文件 Below is a fully working example using the structjson.js file:下面是一个使用structjson.js文件的完整示例:

// 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);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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