Dialogflow Webhook Response c# gives error on invo

2019-08-17 17:34发布

问题:

I'm trying to create a webhook for Dialogflow in C# (on Azure). Everytime I see the same example but my DialogFlows keeps geting an error with this response"

Here's what I did:

  • Created a new ASP.Net Web Project (WebAPI)
  • installed NuGet Google.Cloud.DialogFlow V2 (v1.0.0.beta02)
  • updated System.Net.Http to 4.3.3

Created a new controller

        [System.Web.Http.HttpPost]
    public dynamic DialogAction([FromBody] WebhookRequest dialogflowRequest)
    {
        var intentName = dialogflowRequest.QueryResult.Intent.DisplayName;
        var actualQuestion = dialogflowRequest.QueryResult.QueryText;
        var testAnswer = $"Dialogflow Request for intent {intentName} and question {actualQuestion}";
        var parameters = dialogflowRequest.QueryResult.Parameters;
        var dialogflowResponse = new WebhookResponse
        {
            FulfillmentText = testAnswer,
            FulfillmentMessages =
            { new Intent.Types.Message
                { SimpleResponses = new Intent.Types.Message.Types.SimpleResponses
                    { SimpleResponses_ =
                        { new Intent.Types.Message.Types.SimpleResponse
                            {
                                DisplayText = testAnswer,
                                TextToSpeech = testAnswer,
                            }
                        }
                    }
                }
            }                
        };
        var jsonResponse = dialogflowResponse.ToString();
        return new ContentResult
        {
            Content = jsonResponse,
            ContentType = "application/json"                
        };
  • Published the app to Azure so there's a webhook URl. Now, when I test it in dialogflow, the response is:

"Webhook call failed. Error: Failed to parse webhook JSON response: Cannot find field: Content in message google.cloud.dialogflow.v2.WebhookResponse."

Which I do not understand.....what am I missing here?

(here's the screenshot of the response:)

回答1:

The solution to this problem is to return JsonResult instead of the ContentResult.

   [System.Web.Http.HttpPost]
public JsonResult DialogAction([FromBody] WebhookRequest dialogflowRequest)
{
    var intentName = dialogflowRequest.QueryResult.Intent.DisplayName;
    var actualQuestion = dialogflowRequest.QueryResult.QueryText;
    var testAnswer = $"Dialogflow Request for intent {intentName} and question {actualQuestion}";
    var parameters = dialogflowRequest.QueryResult.Parameters;
    var dialogflowResponse = new WebhookResponse
    {
        FulfillmentText = testAnswer,
        FulfillmentMessages =
        { new Intent.Types.Message
            { SimpleResponses = new Intent.Types.Message.Types.SimpleResponses
                { SimpleResponses_ =
                    { new Intent.Types.Message.Types.SimpleResponse
                        {
                            DisplayText = testAnswer,
                            TextToSpeech = testAnswer,
                        }
                    }
                }
            }
        }                
    };
    var jsonResponse = dialogflowResponse.ToString();
    return Json(jsonResponse);