Towerss
Towerss

Reputation: 674

How to add extra fields in request JSON sent to DialogFlow former Api.ai

I am trying to append extra information to the request that is sent from my application to DialogFlow. I am aware of the possibility to send data calling an intent by triggering its event from Sending Parameters in a Query Request, and I am using that method already for other functionality.

Basically, I am trying to add a value with the userID, and I need to retrieve that value in DialogFlow. I am keepeing track of the session in , so I am using to get ID value. I have tried to do the following in the request:

$.ajax({
  type: "POST",
  url: baseUrl + "query?v=20150910",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  headers: {
    "Authorization": "Bearer " + accessToken
  },
  //This folllowing line is the modified part:
  data: JSON.stringify({
    query: text,
    lang: "en",
    sessionId: "somerandomthing",
    userId: "100"
  }),
  success: function(data) {

  },
  error: function() {
    setResponse("Internal Server Error");
  }
});

And this one as well:

$.ajax({
  type: "POST",
  url: baseUrl + "query?v=20150910",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  headers: {
    "Authorization": "Bearer " + accessToken
  },
  //This folllowing line is the modified part:
  data: JSON.stringify({
    query: text,
    lang: "en",
    sessionId: "somerandomthing",
    userId: "100",
    parameters: {
      userId: "100"
    }
  }),
  success: function(data) {

  },
  error: function() {
    setResponse("Internal Server Error");
  }
});

I need that value in the request to process some data in the back-end based on it. If anyone knows how to do it, or has suggestions for a better approach that would be much appreciated.

There are discussion posts related to this in the DialogFlow Forums, but there is still no resolution. The feature might not be available, or it is not documented Link 1, Link 2, Link 3.

Upvotes: 2

Views: 1193

Answers (1)

Towerss
Towerss

Reputation: 674

I solved this issue with the help of @Svetlana from Dialogflow. The original post is Here, and here is an example from a JavaScript application with back-end in Node.js:

You would format your request like:

            $.ajax({
                type: "POST",
                url: baseUrl + "query?v=20150910",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                headers: {

                    "Authorization": "Bearer " + accessToken
                },
                data: JSON.stringify({originalRequest: {data: {exampleMessage: 'Test'}}, query: text, lang: 'en', sessionId: 'somerandomthing'}),
                success: function (data) {

                },
                error: function () {
                    setResponse("Internal Server Error");
                }
            });

and you would access the value in the webhook with:

var exampleMessage = req.body.originalRequest.data.exampleMessage;

or

var exampleMessage = req.body.originalRequest.data['exampleMessage'];

Hope this helps.

Upvotes: 2

Related Questions