Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dialogflow simple fulfillment webhook in c# not working

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

like image 383
Mikdad Merchant Avatar asked Jun 14 '18 11:06

Mikdad Merchant


People also ask

How do you use fulfillment Dialogflow?

Enable fulfillment responseNavigate to the Dialogflow console and click Intents. Click Schedule Appointment Intent. Scroll down to Fulfillment and turn on Enable webhook call for the intent.

What is webhook fulfillment?

Summary. Order Fufillment webhooks are used to send order-specific data to a customer after a checkout is completed. These work differently to other Paddle webhooks - for information about usage and setup please see our Order Fulfillment Overview. Fulfillment webhooks can be sent as either a GET or POST request.


1 Answers

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.

like image 180
Morph Avatar answered Sep 18 '22 15:09

Morph