3

我在使用 MS bot 框架构建的 bot 中有一个子对话框,其开头如下 - 标准方式:

    public async Task StartAsync(IDialogContext context)
    {
        var msg = "Let's find your flights! Tell me the flight number, city or airline.";
        var reply = context.MakeMessage();
        reply.Text = msg;
        //add quick replies here
        await context.PostAsync(reply);
        context.Wait(UserInputReceived);
    }

使用两种不同的方式调用此对话框,具体取决于用户在上一个屏幕中是点击显示“航班”的按钮还是立即输入航班号。这是父对话框中的代码:

else if (response.Text == MainOptions[2]) //user tapped a button
{
    context.Call(new FlightsDialog(), ChildDialogComplete);
}
else //user entered some text instead of tapping a button
{
    await context.Forward(new FlightsDialog(), ChildDialogComplete,
                          activity, CancellationToken.None);
}

问题:我如何(从内部FlightsDialog)知道该对话框是否是使用context.Call()or调用的context.Forward()?这是因为在 的情况下context.Forward()StartAsync()不应输出要求用户输入航班号的提示 - 他们已经这样做了。

我最好的想法是在ConversationData或用户数据中保存一个标志,如下所示,并从 IDialog 访问它,但我认为可能有更好的方法?

public static void SetUserDataProperty(Activity activity, string PropertyName, string ValueToSet)
{
    StateClient client = activity.GetStateClient();
    BotData userData = client.BotState.GetUserData(activity.ChannelId, activity.From.Id);
    userData.SetProperty<string>(PropertyName, ValueToSet);
    client.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
}
4

1 回答 1

4

不幸的是Forward实际上调用Call(然后做了一些其他的事情),所以你的 Dialog 将无法区分。

void IDialogStack.Call<R>(IDialog<R> child, ResumeAfter<R> resume)
{
    var callRest = ToRest(child.StartAsync);
    if (resume != null)
    {
        var doneRest = ToRest(resume);
        this.wait = this.fiber.Call<DialogTask, object, R>(callRest, null, doneRest);
    }
    else
    {
        this.wait = this.fiber.Call<DialogTask, object>(callRest, null);
    }
}

async Task IDialogStack.Forward<R, T>(IDialog<R> child, ResumeAfter<R> resume, T item, CancellationToken token)
{
    IDialogStack stack = this;
    stack.Call(child, resume);
    await stack.PollAsync(token);
    IPostToBot postToBot = this;
    await postToBot.PostAsync(item, token);
}

来自https://github.com/Microsoft/BotBuilder/blob/10893730134135dd4af4250277de8e1b980f81c9/CSharp/Library/Dialogs/DialogTask.cs#L196

于 2016-11-02T16:39:00.733 回答