Microsoft Bot Framework转发到对话框无法正常工作

时间:2016-11-15 12:27:56

标签: c# botframework

我有2个对话框。在第一个中,如果结果符合我的标准,我转发到第二个,代码如下所示:

/// <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);
    }
}

当我对此进行测试时,当第一个Dialog转发到第二个Dialog时,它会调用 StartAsync 方法,但永远不会调用 ActivityRecievedAsync 。 有谁知道为什么?

1 个答案:

答案 0 :(得分:3)

根据聊天中的讨论,请确保您运行的是最新版本的BotBuilder,因为我们可能会遇到以前版本中的已知问题。

此外(与问题无关,但作为一种良好做法)您可能想要更改

的签名
private async Task ActivityRecievedAsync(IDialogContext context, IAwaitable<object> result)

private async Task ActivityRecievedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)

让对话框为IDialog<object>,它并不表示对话框的第一个输入必须是对象。它只是表示当您执行object时,对话框将返回context.Done

此外,您可以使用转到LUIS的传入消息进行转发,而不是手动创建活动(请参阅this)。只需将intent方法的签名更改为

即可
public async Task ChooseCategory(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)

等待activity参数并在context.Forward

中使用它

一些有趣的资源: