我对dialogflow和WebAPI非常陌生,并且遇到了用C#编写并托管在Azure上的简单dialogflow实现Webhook的麻烦。我正在使用dialogflow V2.0 API版本。

目前,我的履行工作正常,并且返回的响应很简单,但与意图和参数无关。我现在尝试解析JSON,以做一个简单的select案例来获取意图,并返回改正后的参数值。这给我带来很多麻烦。下面给出了webhook链接,我的代码和在“catch”块中返回的错误消息。

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

错误消息:
Value cannot be null.
 Parameter name: value

上面的功能是通过POST调用的,您可以使用POSTMAN来获取JSON响应。

此外,我正在将带有Visual Studio 2017的ASP.Net Web Api与 Controller 一起使用

最佳答案

首先安装nuget包Google.Apis.Dialogflow.v2及其依赖项。它具有对话流响应/请求C#对象,这将使您更轻松地进行导航,因此稍后将为您节省大量工作。

其次为包using Google.Apis.Dialogflow.v2.Data;添加using

将您的方法更改为类似

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

我认为您代码中的主要问题是“obj.outContexts [0]” outContexts不是您可以在其中找到参数的地方,除非您已设置输出内容,否则它将为null。您需要在queryResult中查找您的参数。

关于c# - C#中的dialogflow简单实现webhook无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50856306/

10-12 04:40