【问题标题】:Microsoft Bot Framework exit a dialogMicrosoft Bot Framework 退出对话框
【发布时间】:2017-04-27 04:46:09
【问题描述】:

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

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

这在本地完美运行,但是当我发布它时出现问题。它只是陷入了一个循环,我认为这是因为对话框没有正确退出。 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 传递,并在子 Dialog 创建后执行。 有人可以告诉我我这样做是否正确吗?或者,如果我需要更改某些内容。

【问题讨论】:

标签: c# botframework


【解决方案1】:

如果从当前对话框转发到 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
...

【讨论】:

    猜你喜欢
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2016-11-08
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多