yst*_*van 1 c# json botframework azure-language-understanding
StackOverflow社区!
我有一个聊天机器人,并集成了LUIS.ai,使其更加智能.其中一个对话即将预约与主管(教师)一切都工作正常,字面上相同的代码.几个小时前,我遇到了一些奇怪的错误.
Exception: Type 'Newtonsoft.Json.Linq.JArray' in Assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' is not marked as serializable.
Run Code Online (Sandbox Code Playgroud)
如何重现错误?
如果用户的输入中缺少实体(教师和日期),则它工作正常,机器人构建表单,询问缺少的输入并显示建议的会议时间.
如果其中一个实体从输入中遗漏了它,它将构建一个表格并询问缺少的日期或教师实体,并显示建议的会议时间.
但
如果用户的输入同时包含实体:教师和日期,那么我收到错误.
这是我的WebApiConfig类:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Json settings
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Newtonsoft.Json.Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
};
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
当我试图从用户话语中获取实体时,我只遇到这个错误,这是一种builtin.dateTimeV2.
这种异步方法称为:
//From the LUIS.AI language model the entities
private const string EntityMeetingDate = "MeetingDate";
private const string EntityTeacher = "Teacher";
[LuisIntent("BookSupervision")]
public async Task BookAppointment(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
{
var message = await activity;
await context.PostAsync($"I am analysing your message: '{message.Text}'...");
var meetingsQuery = new MeetingsQuery();
EntityRecommendation teacherEntityRecommendation;
EntityRecommendation dateEntityRecommendation;
if (result.TryFindEntity(EntityTeacher, out teacherEntityRecommendation))
{
teacherEntityRecommendation.Type = "Name";
}
if (result.TryFindEntity(EntityMeetingDate, out dateEntityRecommendation))
{
dateEntityRecommendation.Type = "Date";
}
var meetingsFormDialog = new FormDialog<MeetingsQuery>(meetingsQuery, this.BuildMeetingsForm, FormOptions.PromptInStart, result.Entities);
context.Call(meetingsFormDialog, this.ResumeAfterMeetingsFormDialog);
}
Run Code Online (Sandbox Code Playgroud)
构建表单的其他方法:
private IForm<MeetingsQuery> BuildMeetingsForm()
{
OnCompletionAsyncDelegate<MeetingsQuery> processMeetingsSearch = async (context, state) =>
{
var message = "Searching for supervision slots";
if (!string.IsNullOrEmpty(state.Date))
{
message += $" at {state.Date}...";
}
else if (!string.IsNullOrEmpty(state.Name))
{
message += $" with professor {state.Name}...";
}
await context.PostAsync(message);
};
return new FormBuilder<MeetingsQuery>()
.Field(nameof(MeetingsQuery.Date), (state) => string.IsNullOrEmpty(state.Date))
.Field(nameof(MeetingsQuery.Name), (state) => string.IsNullOrEmpty(state.Name))
.OnCompletion(processMeetingsSearch)
.Build();
}
private async Task ResumeAfterMeetingsFormDialog(IDialogContext context, IAwaitable<MeetingsQuery> result)
{
try
{
var searchQuery = await result;
var meetings = await this.GetMeetingsAsync(searchQuery);
await context.PostAsync($"I found {meetings.Count()} available slots:");
var resultMessage = context.MakeMessage();
resultMessage.AttachmentLayout = AttachmentLayoutTypes.Carousel;
resultMessage.Attachments = new List<Attachment>();
foreach (var meeting in meetings)
{
HeroCard heroCard = new HeroCard()
{
Title = meeting.Teacher,
Subtitle = meeting.Location,
Text = meeting.DateTime,
Images = new List<CardImage>()
{
new CardImage() {Url = meeting.Image}
},
Buttons = new List<CardAction>()
{
new CardAction()
{
Title = "Book Appointment",
Type = ActionTypes.OpenUrl,
Value = $"https://www.bing.com/search?q=easj+roskilde+" + HttpUtility.UrlEncode(meeting.Location)
}
}
};
resultMessage.Attachments.Add(heroCard.ToAttachment());
}
await context.PostAsync(resultMessage);
}
catch (FormCanceledException ex)
{
string reply;
if (ex.InnerException == null)
{
reply = "You have canceled the operation.";
}
else
{
reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
}
await context.PostAsync(reply);
}
finally
{
context.Wait(DeconstructionOfDialog);
}
}
private async Task<IEnumerable<Meeting>> GetMeetingsAsync(MeetingsQuery searchQuery)
{
var meetings = new List<Meeting>();
//some random result manually for demo purposes
for (int i = 1; i <= 5; i++)
{
var random = new Random(i);
Meeting meeting = new Meeting()
{
DateTime = $" Available time: {searchQuery.Date} At building {i}",
Teacher = $" Professor {searchQuery.Name}",
Location = $" Elisagårdsvej 3, Room {random.Next(1, 300)}",
Image = $"https://placeholdit.imgix.net/~text?txtsize=35&txt=Supervision+{i}&w=500&h=260"
};
meetings.Add(meeting);
}
return meetings;
}
Run Code Online (Sandbox Code Playgroud)
这段代码最奇怪的是,GitHub上的社区是我的呐喊和尊重,因为我认为这是一个拥有大量文档和样本的绝佳平台.
简而言之,由于BotBuilder尚不支持builtin.datetimeV2.*实体,因此Resolution字典EntityRecommendation最终会带有值为type的条目JArray.将这些实体传递给a时会出现问题FormDialog.由于实体是对话框中的私有字段,当然,正如任何其他对话框被序列化一样,因为JArray类from Newtonsoft不被标记为可序列化,因此抛出异常.
添加对datetimeV2实体的支持的请求在这里.
我现在能想到的解决方法是手动提取DateTime实体的值,并将它传递给你传递给它Date的MeetingsQuery实例的字段,FormDialog并从result.Entities你传递给它的集合中删除DateTime实体FormDialog.
更新
这已在SDK中修复,您可以在此Pull请求中看到.