微软Bot框架转发到对话框工作不正常
本文关键字:对话框 工作 不正常 转发 Bot 框架 微软 | 更新日期: 2023-09-27 18:17:55
我有两个对话框。在第一个中,如果结果符合我的标准,我将转发给第二个,代码如下所示:
/// <summary>
/// Intent for choosing a category
/// </summary>
/// <param name="context">The current context</param>
/// <param name="result">The Luis result</param>
/// <returns></returns>
[LuisIntent("Choose category")]
public async Task ChooseCategory(IDialogContext context, LuisResult result)
{
// Get our category
var category = TryFindCategory(result);
var response = "The category you have chosen is not in the system just yet.";
switch (category)
{
case "camera":
// If our category is a camera, forward to our QuestionDialog
await context.Forward(new QuestionDialog(), ResumeAfter, new Activity { Text = result.Query }, CancellationToken.None);
break;
default:
// If we have an unknown category, update the response message
if (!string.IsNullOrEmpty(category)) response = $"Sorry, PiiiCK does not deal with { category.Pluralise() } just yet.";
// Post our response back to the user
await context.PostAsync(response);
// Execute the message recieved delegate
context.Wait(MessageReceived);
break;
}
}
/// <summary>
/// Delgate function for resuming after a response have been recieved from LUIS
/// </summary>
/// <param name="context">The current context</param>
/// <param name="result">The Luis result</param>
/// <returns></returns>
private async Task ResumeAfter(IDialogContext context, IAwaitable<object> result)
{
context.Wait(MessageReceived);
}
我已经设置了我的问题对话框如下:
[Serializable]
public class QuestionDialog : IDialog<object>
{
// Private properties
private readonly QuestionGroupService _questionGroupService;
private IList<QuestionGroup> _questionGroups;
/// <summary>
/// Gets a list of QuestionGroups
/// </summary>
/// <returns>A list of QuestionGroups</returns>
public async Task<IList<QuestionGroup>> QuestionGroups() => _questionGroups ?? (_questionGroups = await _questionGroupService.ListAllAsync());
/// <summary>
/// Default constructor
/// </summary>
public QuestionDialog()
{
_questionGroupService = new QuestionGroupService(new UnitOfWork<DatabaseContext>());
}
/// <summary>
/// Start our response
/// </summary>
/// <param name="context">The current context</param>
/// <returns></returns>
public async Task StartAsync(IDialogContext context) => context.Wait(ActivityRecievedAsync);
/// <summary>
/// When our message is recieved we execute this delegate
/// </summary>
/// <param name="context">The current context</param>
/// <param name="result">The result object</param>
/// <returns></returns>
private async Task ActivityRecievedAsync(IDialogContext context, IAwaitable<object> result)
{
// Get our activity
var activity = await result as Activity;
// If it contains test
await context.PostAsync("This is my first question?");
context.Wait(GroupOneAnswerAsync);
}
/// <summary>
///
/// </summary>
/// <param name="context">The current context</param>
/// <param name="result">The result object</param>
/// <returns></returns>
private async Task GroupOneAnswerAsync(IDialogContext context, IAwaitable<object> result)
{
// Get our activity
var activity = await result as Activity;
// If it contains test
await context.PostAsync("This is my second question?");
context.Wait(GroupTwoAnswerAsync);
}
private async Task GroupTwoAnswerAsync(IDialogContext context, IAwaitable<object> result)
{
// Get our activity
var activity = await result as Activity;
// If it contains test
await context.PostAsync("This is my third question?");
context.Wait(GroupThreeAnswerAsync);
}
private async Task GroupThreeAnswerAsync(IDialogContext context, IAwaitable<object> result)
{
// Get our activity
var activity = await result as Activity;
// If it contains test
await context.PostAsync("This is my third question?");
context.Wait(GetResultsAsync);
}
private async Task GetResultsAsync(IDialogContext context, IAwaitable<object> result)
{
// Get our activity
var activity = await result as Activity;
// If it contains test
await context.PostAsync("These are my results");
context.Wait(ActivityRecievedAsync);
}
}
当我对此进行测试时,当第一个对话框转发到第二个对话框时,它调用StartAsync方法,但ActivityRecievedAsync从未调用。有人知道为什么吗?
根据您在聊天中的讨论,请确保您运行的是最新版本的BotBuilder,因为我们可能会遇到以前版本中的已知问题。
另外(与问题无关,但作为一种良好的实践),您可能希望更改
的签名private async Task ActivityRecievedAsync(IDialogContext context, IAwaitable<object> result)
private async Task ActivityRecievedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
让你的对话框是IDialog<object>
并不意味着对话框的第一个输入必须是一个对象。它只是说当你执行一个context.Done
时,对话框将返回一个object
。
另外,您可以使用转到LUIS的传入消息来执行转发,而不是手动创建活动(参见此)。只需将intent方法的签名更改为
public async Task ChooseCategory(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
等待活动参数并在上下文中使用它。转发
一些有趣的资源:
- 关于多对话框的一个很好的例子
- 从对话框调用表单