Microsoft Bot Framework退出对话框

时间:2016-12-12 10:33:00

标签: c# botframework

我有3个对话框,有 InitialDialog StepOneDialog ,最后是 EndDialog 。每个Dialog通过执行以下操作来调用它的子:

await context.Forward(dialog, ResumeAfter, new Activity { }, CancellationToken.None);

这在本地完美运行,但是当我发布它时会出现问题。它只是陷入循环中,我认为这是因为Dialog没有正确退出。 InitialDialog 继承 LuisDialog 。 我有一个方法的代码:

[LuisIntent("")]
public async Task None(IDialogContext context, LuisResult result)
{

    // Create our response
    var response = $"Sorry I did not understand your question.";

    // Post our response back to the user
    await context.PostAsync(response);

    // Exit the application
    context.Done(this);
}

如您所见,我调用context.Done(this)退出对话框,但我不确定这是否正确。在其他示例中,他们似乎使用context.Wait(MessageReceived);

我的其他对话框实现IDialog<object>,因此我无法调用 MessageReceived 。所以在这种情况下,我设置了一个这样的方法:

private async Task ResumeAfter(IDialogContext context, IAwaitable<object> result) => context.Done(this);

我这样调用:

private async Task GetAnswer(IDialogContext context, IAwaitable<string> result)
{

    // Get our quest
    var questions = _group.Questions;
    var length = questions.Count;
    var question = _group.Questions[_currentQuestion];
    var selectedAnswer = await result;

    // Assign our answer to our question
    foreach (var answer in question.Answers)
        if (answer.Text == selectedAnswer)
            question.Answer = answer;

    // If we have an answer, filter the products
    if (question.Answer != null)
        _productProvider.Score(await GetCurrentProducts(), _groups);

    // Increase our index
    _currentQuestion++;

    // If our current index is greater or equal than the length of the questions
    if (_currentQuestion == length)
    {

        // Create our dialog
        var dialog = _dialogFactory.CreateSecondStepDialog(_dialogFactory, _groupProvider, _questionProvider, _productProvider, await GetCurrentProducts());

        // Otherwise, got to the next step
        await context.Forward(dialog, ResumeAfter, new Activity { }, CancellationToken.None);
        return;
    }

    // Ask our next question
    await AskQuestion(context, null);
}

如您所见,context.Forward将方法作为Delegate传递,并在创建子对话框后执行。 有人可以告诉我,如果我这样做吗?或者,如果我需要改变一些东西。

1 个答案:

答案 0 :(得分:0)

如果从当前对话框转发到Luis对话框消息,您可以使用:

var _message = context.MakeMessage();
_message.Text = "Custom user query";
await context.Forward(new MyRootLuisDialog(), MyRootLuisDialogComplete, _message);

如果打开Luis(子)对话框,您可以使用:

context.Call(new MyRootLuisDialog(), MyRootLuisDialogComplete); 

回调示例:

public virtual async Task MyRootLuisDialogComplete(IDialogContext context, IAwaitable<object> response)
{
    var _response = (MyDialogData)await response;
    ...
}

子对话框退出:

...
context.Done(_dialogData); // type of _dialogData is MyDialogData
...