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

梦想的初衷 提交于 2019-12-10 23:55:33

问题


I am trying to append extra information to the jsonrequest 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 php, so I am using phpto get ID value. I have tried to do the following in the ajaxrequest:

$.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.


回答1:


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.



来源:https://stackoverflow.com/questions/47342817/how-to-add-extra-fields-in-request-json-sent-to-dialogflow-former-api-ai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!