LUIS/机器人框架多个对话框,将意向处理移动到另一个对话框



我的目标是使用其 C# SDK 将对话框和 LUIS 实现到Microsoft机器人框架应用程序中。我试图关注这个线程https://github.com/Microsoft/BotBuilder/issues/127 及其相关帖子(最后引用),但无法让我的代码在实践中工作。这是我的RootDialog类。请注意,我创建了一个处理"GetProduct"意图的方法,当它获得此意图时,它应该使用上下文将LuisResult转发到ProductsDialog。Forward() 方法,但我看到的只是它直接转到 ResumeAfter 方法,ProductsDialogComplete。现在,这可能是我失败的地方,但我找不到显示多个 LUIS 对话框的示例。

public class RootDialog : LuisDialog<object>
{
[LuisIntent("GetProduct")]
private async Task GetProduct(IDialogContext context, LuisResult result)
{
await context.PostAsync("Calling ProductsDialog...");
await context.Forward(Chain.From(() => new ProductsDialog()), ProductsDialogCompleted, context.Activity, CancellationToken.None);
}
private async Task ProductsDialogCompleted(IDialogContext context, IAwaitable<object> result)
{
var res = await result;
context.PostAsync("ProductsDialogCompleted" + result);
context.Wait(this.MessageReceived);
}
}
public class ProductsDialog : LuisDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync("Entered ProductsDialog");
context.Wait(this.MessageReceived);
}
[LuisIntent("None")]
private async Task None(IDialogContext context, LuisResult result)
{
context.Done(true);
}
}

预期行为如下

  1. 用户触发 GetProduct 意向
  2. 机器人创建一个新对话框并转到 StartAsync 方法,在该方法中等待另一个用户输入
  3. 用户触发"无"意向
  4. 对话框关闭,返回 true 并触发产品对话框已完成。

似乎我没有正确绑定对话框。我该如何解决这个问题?

编辑:添加了消息控制器,版本为3.8.1

[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new RootDialog());
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing tha the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}
return null;
}
}

尝试从context.Forward调用中删除Chain.From(()。不知道为什么要添加它,但它根本不应该存在。

尝试:

await context.Forward(new ProductsDialog(), ProductsDialogCompleted, context.Activity, CancellationToken.None);

顺便说一句,如果您转发的消息击中None意图,则ProductsDialogCompleted方法将被击中,因为您正在执行context.Done,这基本上结束了ProductsDialog

此外,请记住StartAsync方法存在于LuisDialog<T>基类中,因此需要添加override关键字。

我有同样的问题,但我使用的是较新版本的机器人框架,更具体地说,V4。

这是我发现的:

  • BeginDialogAsyncoptions参数采用一个对象数组,然后可以在对话框中访问这些对象。
// Get skill LUIS model from configuration.
localizedServices.LuisServices.TryGetValue("MySkill", out var luisService);
if (luisService != null)
{
// Get the Luis result. 
var result = innerDc.Context.TurnState.Get<MySkillLuis>(StateProperties.SkillLuisResult);
var intent = result?.TopIntent().intent;
// Behavior switched on intent. 
switch (intent)
{
case MySkillLuis.Intent.MyIntent:
{
// result is passed on to my dialog through the Options parameter. 
await innerDc.BeginDialogAsync(_myDialog.Id, result);
break;
}
case MySkillLuis.Intent.None:
default:
{
// intent was identified but not yet implemented
await innerDc.Context.SendActivityAsync(_templateEngine.GenerateActivityForLocale("UnsupportedMessage"));
break;
}
}
}

从第二个对话框中,我们可以通过上下文访问对象,并根据需要执行任何强制转换等。就我而言,这是一个瀑布对话框,所以我使用了stepContext.options

最新更新