Botframework v4:Stepcontext选项 [英] Botframework v4: Stepcontext Option

查看:74
本文介绍了Botframework v4:Stepcontext选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,有人可以解释如何使用瀑布stepcontext.Option吗? 我一直在示例中看到它,但我不太了解如何使用它. 以下是

Hello can someone explain how to use the waterfall stepcontext.Option? I keep seeing it in examples but i can't quite understand how to use it. Here are example from this and this.

我计划重构我的整个代码,并在可能的情况下使用此选项.谢谢!

I am planning to refactor my whole code and would want to use this option if possible. Thank!

private static async Task<DialogTurnResult> TableStepAsync(
    WaterfallStepContext step,
    CancellationToken cancellationToken = default(CancellationToken))
{
    string greeting = step.Options is GuestInfo guest
            && !string.IsNullOrWhiteSpace(guest?.Name)
            ? $"Welcome {guest.Name}" : "Welcome";

    string prompt = $"{greeting}, How many diners will be at your table?";
    string[] choices = new string[] { "1", "2", "3", "4", "5", "6" };
    return await step.PromptAsync(
        TablePrompt,
        new PromptOptions
        {
            Prompt = MessageFactory.Text(prompt),
            Choices = ChoiceFactory.ToChoices(choices),
        },
        cancellationToken);
}

    private async Task<DialogTurnResult> SelectionStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    // Continue using the same selection list, if any, from the previous iteration of this dialog.
    List<string> list = stepContext.Options as List<string> ?? new List<string>();
    stepContext.Values[CompaniesSelected] = list;

    // Create a prompt message.
    string message;
    if (list.Count is 0)
    {
        message = $"Please choose a company to review, or `{DoneOption}` to finish.";
    }
    else
    {
        message = $"You have selected **{list[0]}**. You can review an additional company, " +
            $"or choose `{DoneOption}` to finish.";
    }

    // Create the list of options to choose from.
    List<string> options = _companyOptions.ToList();
    options.Add(DoneOption);
    if (list.Count > 0)
    {
        options.Remove(list[0]);
    }

    // Prompt the user for a choice.
    return await stepContext.PromptAsync(
        SelectionPrompt,
        new PromptOptions
        {
            Prompt = MessageFactory.Text(message),
            RetryPrompt = MessageFactory.Text("Please choose an option from the list."),
            Choices = ChoiceFactory.ToChoices(options),
        },
        cancellationToken);
}

在可能的情况下,我还想学习如何传递和获取像这样的值

If possible i would also like to learn how to pass and get values like this way in this example

    private static async Task<DialogTurnResult> RoomStepAsync(
    WaterfallStepContext step,
    CancellationToken cancellationToken = default(CancellationToken))
{
    // Save the name and prompt for the room number.
    string name = step.Result as string;
    ((GuestInfo)step.Values[GuestKey]).Name = name;
    return await step.PromptAsync(
        TextPrompt,
        new PromptOptions
        {
            Prompt = MessageFactory.Text($"Hi {name}. What room will you be staying in?"),
        },
        cancellationToken);
}

private static async Task<DialogTurnResult> FinalStepAsync(
    WaterfallStepContext step,
    CancellationToken cancellationToken = default(CancellationToken))
{
    // Save the room number and "sign off".
    string room = step.Result as string;
    ((GuestInfo)step.Values[GuestKey]).Room = room;

    await step.Context.SendActivityAsync(
        "Great, enjoy your stay!",
        cancellationToken: cancellationToken);

    // End the dialog, returning the guest info.
    return await step.EndDialogAsync(
        (GuestInfo)step.Values[GuestKey],
        cancellationToken);
}

}

现在这就是我保存值的方式.

Right now this is how i save the values.

var userstate = await (stepContext.Context.TurnState["BasicAccessors"] as BasicAccessors).BasicUserStateAccessor.GetAsync(stepContext.Context);
userstate.Name = value;

推荐答案

您到底想做什么? stepContext.Options是一个对象,您可以在调用Dialog时使用BeginDialog或ReplaceDialog来发送该对象. 例如:

What exactly are you trying to do? stepContext.Options is an object you can send when the Dialog is being called, either with BeginDialog or with ReplaceDialog. E.g:

await BeginDialogAsync(dialogId, sendobject, cancellationToken)

stepContext.Options是您通过调用的Dialog接收该对象的方式.

stepContext.Options is your way of receiving that object over the called Dialog.

例如,在第一个文档中,主Dialog调用每个子Dialog并将其发送给userInfo.Guest对象:

For example, in the first document, the main Dialog is calling each child Dialogs and sending them the userInfo.Guest object:

return await stepContext.BeginDialogAsync(TableDialogId, userInfo.Guest, cancellationToken);

被调用的对话框正在接收它,并将其转换为字符串作为验证:

And the called dialog is receiving it and casting it into a string as a validation:

string greeting = step.Options is GuestInfo guest
            && !string.IsNullOrWhiteSpace(guest?.Name)
            ? $"Welcome {guest.Name}" : "Welcome";

您可以剥离验证,它看起来像这样,请记住,这仅在要发送的对象(userInfo.Guest)不为null且可以转换为字符串的情况下有效:

You can strip the validation and it would look like this, having in mind this would only work if the object being sent (userInfo.Guest) is not null and can be casted into a string:

string greeting = (string)step.Options;

请记住: stepContext.Options;是一个对象,需要转换为正确的类型. 如果您不添加null/type验证,则转换可能会失败,并且您的漫游器可能会崩溃. 这是框架的功能,但机器人不需要它起作用,您可以使用其他方法通过方法或类发送对象.

Have in mind that: stepContext.Options; is an object and needs to be cast into the correct type. If you don't add null/type validation, the cast can fail and your bot can crash. This is a feature of the framework, but is not required by the bot to work and you can use other ways of sending objects through methods or classes.

这篇关于Botframework v4:Stepcontext选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆