作者:双子汐晨_312 | 来源:互联网 | 2022-12-13 18:01
我对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
1> Morph..:
首先安装nuget包Google.Apis.Dialogflow.v2及其依赖项。有了对话流响应/请求c#对象,它将为您节省很多工作,这将简化对象图的导航。
第二个添加包的用法 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中查找您的参数。