0
votes

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:) Screenhot

1
Here is an example [0]. Looks like the only meaningful difference between the two is that your use of ContextResult and that the JSON string is escaped instead of unescaped like you expected. Alter jsonResponse so the JSON string is no longer escaped. [0]github.com/googleapis/google-cloud-dotnet/blob/master/apis/… - matthewayne

1 Answers

0
votes

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