在 Microsoft Bot Framework 中区分是使用 context.Call() 还是 context.Forward() 调用了 IDialog

Differentiate whether IDialog was called using context.Call() or context.Forward() in Microsoft Bot Framework

我在使用 MS 机器人框架构建的机器人中有一个子对话框,其启动方式如下 - 标准方式:

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

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

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() 还是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);
}

不幸的是,Forward 实际上调用了 Call(之后又做了一些其他事情),因此您的对话框将无法区分。

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

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