dialogflow simple fulfillment webhook in c# not working

拈花ヽ惹草 提交于 2019-12-01 17:58:42

问题


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:


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.



来源:https://stackoverflow.com/questions/50856306/dialogflow-simple-fulfillment-webhook-in-c-sharp-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!