简体   繁体   English

区分是否使用Microsoft Bot Framework中的context.Call()或context.Forward()调用IDialog

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

I have a subdialog in a bot built using MS bot framework that starts as follows - the standard way: 我在使用MS bot框架构建的机器人中有一个subialog,其启动方式如下 - 标准方式:

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

This dialog is called using two different ways, depending on whether in the previous screen the user tapped a button that says "Flights" or immediately entered a flight number. 使用两种不同的方式调用此对话框,具体取决于在上一个屏幕中用户是否点击了一个“航班”按钮或立即输入了航班号。 Here is the code from the parent dialog: 以下是父对话框中的代码:

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

Question : how can I know (from within the FlightsDialog ) whether that dialog was called using context.Call() or context.Forward() ? 问题 :我如何知道(在FlightsDialog )是否使用context.Call()context.Forward()调用该对话框? This is because in the case of context.Forward() , StartAsync() shouldn't output the prompt asking the user to enter the flight number - they already did this. 这是因为在context.Forward()的情况下, StartAsync()不应该输出要求用户输入航班号的提示 - 他们已经这样做了。

The best idea I have is to save a flag in the ConversationData or user data, as below, and access it from the IDialog, but I thought there could be a better way? 我最好的想法是在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);
}

Unfortunately Forward actually calls Call (and then does some other stuff afterwards), so your Dialog wouldn't be able to differentiate. 不幸的是, 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);
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM