如何在发送到DialogFlow以前的Api.ai的请求JSON中添加额外字段 [英] How to add extra fields in request JSON sent to DialogFlow former Api.ai

查看:121
本文介绍了如何在发送到DialogFlow以前的Api.ai的请求JSON中添加额外字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将其他信息附加到在查询请求中发送参数,而我已经将该方法用于其他功能.

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.

基本上,我试图在userID中添加一个值,我需要在DialogFlow中检索该值.我在的问题中跟踪会话,因此我正在使用的问题来获取ID值.我已尝试在请求:

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

这也是:

$.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中的某些数据.如果有人知道该怎么做,或者有更好的方法的建议,将不胜感激.

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.

在DialogFlow论坛中有与此相关的讨论帖子,但仍然没有解决方案.该功能可能不可用,或没有记录在链接2

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.

推荐答案

我在Dialogflow的@Svetlana的帮助下解决了此问题.原始帖子为这里,下面是一个带有Node.js后端的JavaScript应用程序的示例:

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

,您将使用以下命令访问Webhook中的值:

and you would access the value in the webhook with:

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

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

希望这会有所帮助.

这篇关于如何在发送到DialogFlow以前的Api.ai的请求JSON中添加额外字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆