Botframework v4:Stepcontext选项

时间:2019-02-21 04:37:30

标签: c# botframework

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

我打算重构我的整个代码,并在可能的情况下使用此选项。谢谢!

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

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

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

}

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

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

2 个答案:

答案 0 :(得分:1)

@Dante的答案是正确的,但我会让它更容易理解:

假设您有一个ParentDialogChildDialog

如果您有以下课程:

public class OptionsPassed
{
    public string ParameterToPass { get; set; }
}
  1. 通过以下方式调用您的子对话框:await stepContext.BeginDialogAsync(nameof(ChildDialog), new OptionsPassed { ParameterToPass = "ValueToPass" });

  2. 然后,您可以使用{ ParameterToPass: "ValueToPass" }访问ChildDialog中任何地方的stepContext.Options.ParemeterToPass。例如,如果您想查看传入的值是什么:

ChildDialog的第一步:

private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
    var passed = stepContext.Options as OptionsPassed;
    await stepContext.Context.SendActivityAsync($"You passed in {passed.ParameterToPass}");
    return await stepContext.NextAsync();
}

答案 1 :(得分:0)

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

await BeginDialogAsync(dialogId, sendobject, cancellationToken)

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

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

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

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

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

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

string greeting = (string)step.Options;

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