Send parameters to webhook on dialogflow sdk v2

前端 未结 1 576
天涯浪人
天涯浪人 2020-12-05 10:40

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 (suc

相关标签:
1条回答
  • 2020-12-05 11:21

    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);
      });
    
    0 讨论(0)
提交回复
热议问题