如何在发送到 DialogFlow former Api.ai 的请求 JSON 中添加额外字段

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

我正在尝试将额外信息附加到 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,并且我已经将该方法用于其他功能。

基本上,我试图用 userID 添加一个值,我需要在 DialogFlow 中检索该值。我在 , 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");
  }
});

还有这个:

$.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");
  }
});

我需要请求中的那个值来处理 back-end 中基于它的一些数据。如果有人知道该怎么做,或者对更好的方法有建议,我们将不胜感激。

DialogFlow论坛中有相关的讨论帖,但仍未解决。该功能可能不可用,或者没有记录 Link 1, Link 2, Link 3

我在 Dialogflow 的@Svetlana 的帮助下解决了这个问题。原来的 post 是 Here,这里是一个 JavaScript 应用程序的例子,后端在 Node.js:

您可以将您的请求格式化为:

            $.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");
                }
            });

您将通过以下方式访问 webhook 中的值:

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

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

希望这对您有所帮助。