简体   繁体   English

Dialogflow无法从我的Webhook解析JSON响应似乎改变了字符编码

[英]Dialogflow fails to parse the JSON response from my webhook seems to change character encoding

I have a fulfillment webhook which I am trying to use to return a simple response to a test intent on Dialogflow. 我有一个实现网络挂钩,我试图使用它来返回对Dialogflow上测试意图的简单响应。

The webhook is an azure function and having tested it with postman I can see the JSON response being formatted correctly with the right headers etc. 该webhook是一个蔚蓝的函数,已经用邮递员对其进行了测试,我可以看到JSON响应使用正确的标头等正确格式化了。

This is the raw response which Dialogflow shows me is the error (Scroll right to see the error on the message) 这是Dialogflow向我显示的原始响应是错误(向右滚动以查看消息中的错误)

{
  "responseId": "6d2686c1-a87b-4f64-ac4d-f3a5e706763c",
  "queryResult": {
    "queryText": "Test",
    "parameters": {},
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            ""
          ]
        }
      }
    ],
    "intent": {
      "name": "projects/mytestappname-c0f76/agent/intents/3d70ef1d-9723-451f-b79d-b40f4e16d8b0",
      "displayName": "Test"
    },
    "intentDetectionConfidence": 1,
    "diagnosticInfo": {
      "webhook_latency_ms": 600
    },
    "languageCode": "en"
  },
  "webhookStatus": {
    "code": 3,
    "message": "Webhook call failed. Error: Failed to parse webhook JSON response: Expect message object but got: \"笀∀昀漀氀氀漀眀甀瀀䔀瘀攀渀琀䤀渀瀀甀琀∀㨀渀甀氀氀Ⰰ∀昀甀氀昀椀氀氀洀攀渀琀䴀攀猀猀愀最攀猀∀㨀嬀笀∀戀愀猀椀挀䌀愀爀搀∀㨀渀甀氀氀Ⰰ∀挀愀爀搀∀㨀渀甀氀氀Ⰰ∀挀愀爀漀甀猀攀氀匀攀氀攀挀琀∀㨀渀甀氀氀Ⰰ∀椀洀愀最攀∀㨀渀甀氀氀Ⰰ∀氀椀渀欀伀甀琀匀甀最最攀猀琀椀漀渀∀㨀渀甀氀氀Ⰰ∀氀椀猀琀匀攀氀攀挀琀∀㨀渀甀氀氀Ⰰ∀瀀愀礀氀漀愀搀∀㨀渀甀氀氀Ⰰ∀瀀氀愀琀昀漀爀洀∀㨀渀甀氀氀Ⰰ∀焀甀椀挀欀刀攀瀀氀椀攀猀∀㨀渀甀氀氀Ⰰ∀猀椀洀瀀氀攀刀攀猀瀀漀渀猀攀猀∀㨀渀甀氀氀Ⰰ∀猀甀最最攀猀琀椀漀渀猀∀㨀渀甀氀氀Ⰰ∀琀攀砀琀∀㨀笀∀琀攀砀琀∀㨀嬀∀吀栀椀猀 椀猀 愀 昀甀氀昀椀氀氀洀攀渀琀 洀攀猀猀愀最攀∀崀Ⰰ∀䔀吀愀最∀㨀渀甀氀氀紀Ⰰ∀䔀吀愀最∀㨀渀甀氀氀紀崀Ⰰ∀昀甀氀昀椀氀氀洀攀渀琀吀攀砀琀∀㨀∀吀栀椀猀 椀猀 昀甀氀昀椀氀氀洀攀渀琀 琀攀砀琀∀Ⰰ∀漀甀琀瀀甀琀䌀漀渀琀攀砀琀猀∀㨀渀甀氀氀Ⰰ∀瀀愀礀氀漀愀搀∀㨀笀∀最漀漀最氀攀∀㨀笀∀攀砀瀀攀挀琀唀猀攀爀刀攀猀瀀漀渀猀攀∀㨀琀爀甀攀Ⰰ∀爀椀挀栀刀攀猀瀀漀渀猀攀∀㨀笀∀椀琀攀洀猀∀㨀嬀笀∀搀椀猀瀀氀愀礀吀攀砀琀∀㨀渀甀氀氀Ⰰ∀猀猀洀氀∀㨀渀甀氀氀Ⰰ∀琀攀砀琀吀漀匀瀀攀攀挀栀∀㨀∀吀栀椀猀 椀猀 䐀愀瘀攀✀猀 吀攀猀琀椀渀最 吀攀砀琀∀Ⰰ∀䔀吀愀最∀㨀渀甀氀氀紀崀紀Ⰰ∀昀椀渀愀氀刀攀猀瀀漀渀猀攀∀㨀笀∀椀琀攀洀猀∀㨀嬀笀∀搀椀猀瀀氀愀礀吀攀砀琀∀㨀渀甀氀氀Ⰰ∀猀猀洀氀∀㨀渀甀氀氀Ⰰ∀琀攀砀琀吀漀匀瀀攀攀挀栀∀㨀∀吀栀椀猀 椀猀 䐀愀瘀攀✀猀 䘀椀渀愀氀 吀攀猀琀椀渀最 吀攀砀琀∀Ⰰ∀䔀吀愀最∀㨀渀甀氀氀紀崀紀紀紀Ⰰ∀猀漀甀爀挀攀∀㨀∀攀砀愀洀瀀氀攀⸀挀漀洀∀Ⰰ∀䔀吀愀最∀㨀渀甀氀氀紀\"."
  }
}

However in the same window it shows me what my actual fullfilment response was which is this 但是,在同一窗口中,它显示了我实际的完整响应是什么

{
  "followupEventInput": null,
  "fulfillmentMessages": [
    {
      "basicCard": null,
      "card": null,
      "carouselSelect": null,
      "image": null,
      "linkOutSuggestion": null,
      "listSelect": null,
      "payload": null,
      "platform": null,
      "quickReplies": null,
      "simpleResponses": null,
      "suggestions": null,
      "text": {
        "text": [
          "This is a fulfillment message"
        ],
        "ETag": null
      },
      "ETag": null
    }
  ],
  "fulfillmentText": "This is fulfillment text",
  "outputContexts": null,
  "payload": {
    "google": {
      "expectUserResponse": true,
      "richResponse": {
        "items": [
          {
            "displayText": null,
            "ssml": null,
            "textToSpeech": "This is Dave's Testing Text",
            "ETag": null
          }
        ]
      },
       "finalResponse": {
        "items": [
          {
             "displayText": null,
             "ssml": null,
            "textToSpeech": "This is Dave's Final Testing Text",
            "ETag": null
           }
        ]
      }
    }
  },
  "source": "example.com",
  "ETag": null
}

These are the headers on the response 这些是响应的标题

"Content-Type": "application/json; charset=utf-8", "Date": "Wed, 30 May 2018 07:58:36 GMT", "Server": "Kestrel", "Transfer-Encoding": "chunked", “内容类型”:“应用程序/ json; charset = utf-8”,“日期”:“星期三,2018年5月30日格林尼治标准时间”,“服务器”:“红est”,“传输编码”: “分块”,

I've put the response through a validator and it's definitely valid JSON. 我已经通过验证器发送了响应,它肯定是有效的JSON。

The issue isn't the content of the response JSON, I can fix that later if it's incorrect, the issue is that it can't even begin to parse it. 问题不是响应JSON的内容,如果不正确,我以后可以修复,问题是它甚至无法开始解析它。

Does anyone have any idea why Dialogflow can't read it at all? 有谁知道为什么Dialogflow根本无法读取它?

I was getting the same error. 我遇到了同样的错误。 DialogFlow was not able to parse the JSON response, displaying some weird Chinese characters. DialogFlow无法解析JSON响应,显示一些奇怪的汉字。 And like you said, the fulfillment response tab would display the message I was sending back! 就像您说的那样,履行响应选项卡将显示我发回的消息!

The only difference is I'm not using Azure functions. 唯一的区别是我没有使用Azure函数。 My webhook is a .NET Core 2 Web Api and I was returning an OkObjectResult which was definitely producing a valid JSON response. 我的webhook是.NET Core 2 Web Api,并且我返回的OkObjectResult肯定会产生有效的JSON响应。

I haven't figured out why but I managed to get DialogFlow to parse the message by not returning an object but with the following: 我还没有弄清楚为什么,但是我设法通过不返回对象而是通过以下操作使DialogFlow解析消息:

HttpContext.Response.WriteAsync("{ \"fulfillmentText\" : \"This is a message from my webhook !\" }");

The root problem is actually ASP.NET Core by default uses transfer-encoding: chunked for ActionResult and for some reason Dialogflow does not support parsing chunked transfer (most clients should handle it transparently). 根本问题实际上是ASP.NET Core默认情况下使用transfer-encoding: chunkedActionResult transfer-encoding: chunked ,并且由于某些原因Dialogflow不支持解析分块传输(大多数客户端应透明地处理)。 You can verify this by looking at the raw server response using a network debugger. 您可以通过使用网络调试器查看原始服务器响应来验证这一点。

Unfortunately there doesn't seem to be an easy way to disable chunked responses in ASP.NET Core 2 ( there was in Core 1 ). 不幸的是,似乎没有一种简单的方法来禁用ASP.NET Core 2中的分块响应( Core 1中有 )。

The workaround is to use a ContentResult response type which seems to only return buffered responses so it will not use chunked transfer. 解决方法是使用ContentResult响应类型,该类型似乎仅返回缓冲的响应,因此将不使用分块传输。 But obviously you'll need to handle your own JSON serialization so it should look something like 但是显然您需要处理自己的JSON序列化,因此它看起来应该像

    [HttpGet]
    public ContentResult Test()
    {
        var response = new()
        {
            Hello = "World"
        };

        return Content(JsonConvert.SerializeObject(response), "application/json");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM