来自使用C#Azure函数的Google对话框实现的格式错误的响应

时间:2019-03-05 13:42:22

标签: c# azure azure-functions

我正在开发一个程序,该程序以https触发的Azure函数程序的形式接收Google行动请求(已实现)(这是我正在编写的部分)。 现在,我能够收到请求。但是,当我尝试返回响应时,Google模拟器说我将其作为响应:

{
"responseMetadata": {
"status": {
  "code": 10,
  "message": "Failed to parse Dialogflow response into AppResponse because of empty speech response",
  "details": [
    {
      "@type": "type.googleapis.com/google.protobuf.Value",
      "value": "{\"id\":\"dd9eda55-a5b3-4b8f-be38-2eb50ff840d4\",\"timestamp\":\
                "2019-03-05T13:37:36.852Z\",\"lang\":\"en-us\",\"result\":{},\
                "alternateResult\":{},\"status\":{\"code\":206,\"errorType\":\
                "partial_content\",\"errorDetails\":\"Webhook call failed. Error: Failed 
                 to parse webhook JSON response: Expect message object but got: \\\
                "笀∀昀甀氀昀椀氀氀洀攀渀琀吀攀砀琀∀㨀∀吀栀爀漀眀 戀愀渀愀渀愀 漀甀琀 䄀渀礀眀栀攀爀攀∀Ⰰ∀昀甀氀昀椀氀氀洀攀渀琀䴀攀猀猀愀最攀猀∀㨀嬀崀Ⰰ∀猀漀甀爀挀攀∀㨀∀∀Ⰰ∀瀀愀礀氀漀愀搀∀㨀渀甀氀氀Ⰰ∀漀甀琀瀀甀琀䌀漀渀琀攀砀琀猀∀㨀嬀崀Ⰰ∀昀漀氀氀漀眀甀瀀䔀瘀攀渀琀䤀渀瀀甀琀∀㨀渀甀氀氀紀\\\".\"},\"sessionId\":\"ABwppHGecG8tTLQ_DS7c3EL0l80w7YLzwAC8NFid9fVPnvYpD0s9QPo_Vnib3riGZ-8qDVI5vPnaKON1hVnC\"}"
    }
  ]
}
}
}

这是我收到请求的方式:

    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, /*"get",*/ "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request."); 

        string json = await req.ReadAsStringAsync();
    JsonParser jsonParser = new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true));
    googleRequest = jsonParser.Parse<WebhookRequest>(json);

此刻响应函数是这样的:

public OkObjectResult response(String output)
{
    WebhookResponse googleResponse = new WebhookResponse();
    googleResponse.FulfillmentText = output;

    return new OkObjectResult(googleResponse);//This is returned from the main function
}

此外,我正在使用ngrok与Google模拟器进行测试

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我有同样的问题。而不是使用

public OkObjectResult response(String output)
{
    WebhookResponse googleResponse = new WebhookResponse();
    googleResponse.FulfillmentText = output;

    return new OkObjectResult(googleResponse);//This is returned from the main function
}

尝试这样的事情:

    var returnString = googleResponse.ToString();
    return new ContentResult
    {
        Content = returnString,
        ContentType = "application/json",
        StatusCode = 200
    };  

请查看此link为什么在OkObjectResult上使用ContentResult