C#中的dialogflow简单实现webhook无法正常工作 [英] dialogflow simple fulfillment webhook in c# not working

查看:143
本文介绍了C#中的dialogflow简单实现webhook无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

错误消息:

Value cannot be null.
 Parameter name: value

以上功能是通过POST调用的,可以使用POSTMAN和您将得到JSON响应。

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

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

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

推荐答案

首先安装nuget包Google.Apis.Dialogflow.v2及其依赖项。

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.

第二步为它添加用法,它将为您节省很多工作,因为它具有dialogflow响应/请求c#对象,这将使对象图的导航更加容易。包使用Google.Apis.Dialogflow.v2.Data;

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

将您的方法更改为类似

 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中查找您的参数。

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.

这篇关于C#中的dialogflow简单实现webhook无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆