5
votes

I am very new to dialogflow and WebAPIs, and having trouble with a simple dialogflow fulfillment webhook written in C# and hosted on Azure. I am using dialogflow V2.0 API version.

Currently my fulfillment works and returns a simple response but has no regard to the intent and parameters. I am trying to now parse the JSON get the intent do a simple select case and return the value of the parameters recevied. And this is giving me lot of trouble. The webhook link, my code and the error message returned in the "catch" block are given below

    public JsonResult Post(string value)
    {
        try
        {
            dynamic obj = JsonConvert.DeserializeObject(value);
            string Location = string.Empty;

            switch (obj.intent.displayName)
            {
                case "getstock":
                    Location = obj.outContexts[0].parameters[0].Location;
                    break;
            }

            WebhookResponse r = new WebhookResponse();
            r.fulfillmentText = string.Format("The stock at {0} is valuing Rs. 31 Lakhs \n And consists of items such as slatwall, grid and new pillar. The detailed list of the same has been email to you", Location);

            r.source = "API.AI";


            Response.ContentType = "application/json";
            return Json(r);
        } catch(Exception e)
        {
            WebhookResponse err = new WebhookResponse();
            err.fulfillmentText = e.Message;
            return Json(err);
        }
    }

The error message :

Value cannot be null.
 Parameter name: value

The above function is called via POST, you can use POSTMAN and you will get the JSON response.

Moreover i am using ASP.Net Web Api with Visual Studio 2017 with Controllers

1
How is this function being called? It looks like it is being called with a null for value. We'd need to see more to help understand what is going on. - Prisoner
it is being called by dialogueflow and i have no control over it - Mikdad Merchant
Can you update your question to provide information about how the URL you've given to dialogflow is routed to that particular function on Azure and in the C# framework you're using? - Prisoner
post the function has a http post attribute so it is called with post also i am sure the post is working because the web hook returns the response in the catch block - Mikdad Merchant
Can you share the answer if you got a solution? - Anita George

1 Answers

5
votes

First install the nuget package Google.Apis.Dialogflow.v2 and its dependencies. It'll save you a lot of work later on as it has dialogflow response/request c# objects which will make navigating the object graph easier.

Second add the using for the package using Google.Apis.Dialogflow.v2.Data;

Change your method to something like

 public GoogleCloudDialogflowV2WebhookResponse Post(GoogleCloudDialogflowV2WebhookRequest obj)
    {
            string Location = string.Empty;

            switch (obj.QueryResult.Intent.DisplayName)
            {
                case "getstock":
                Location = obj.QueryResult.Parameters["Location"].ToString();
                break;
            }

            var response = new GoogleCloudDialogflowV2WebhookResponse()
            {
                FulfillmentText = $"The stock at {Location} is valuing Rs. 31 Lakhs \n And consists of items such as slatwall, grid and new pillar. The detailed list of the same has been email to you",
                Source = "API.AI"
            };

            return response;
    }

I think your main issue in your code is "obj.outContexts[0]" outContexts isn't where you'll find your parameters, and unless you've setup an output content this will be null. You need to look in queryResult for your parameters.