我是 dialogflow 和 WebAPI 的新手,在使用用 C# 编写并托管在 Azure 上的简单 dialogflow fulfillment webhook 时遇到了麻烦。我使用的是 dialogflow V2.0 API 版本。
目前我的实现工作正常并返回一个简单的响应,但不考虑意图和参数。我现在正在尝试解析 JSON 以获得意图做一个简单的选择案例并返回接收到的参数值。这给我带来了很多麻烦。下面给出了 webhook 链接、我的代码和“catch” block 中返回的错误消息
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 响应。
此外,我正在使用带有 Controller 的 Visual Studio 2017 的 ASP.Net Web Api
最佳答案
首先安装 nuget 包 Google.Apis.Dialogflow.v2 及其依赖项。它会在以后为您节省大量工作,因为它具有 dialogflow 响应/请求 C# 对象,这将使导航对象图更容易。
其次为包添加 using 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 不是您可以找到参数的地方,除非您设置了输出内容,否则它将为空。您需要在 queryResult 中查找您的参数。
关于c# - c# 中的 dialogflow 简单实现 webhook 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50856306/