【问题标题】:Microsoft bot framework v.4 -- pass data from one dilogflow to anotherMicrosoft bot framework v.4 - 将数据从一个 dilogflow 传递到另一个
【发布时间】:2019-02-22 20:40:51
【问题描述】:

我正在尝试在 MS Bot Framework 4 中的 2 个对话框之间传递数据。但是,它不起作用。我使用了 stepContext.Values。但它不能将数据转发到另一个Dialog。我是 Microsoft bot Framework 中的一种新人。如果有人可以帮助我,将不胜感激。

第一个对话框:

public class Step1Dialog : WaterfallDialog
{
    public Step1Dialog(string dialogId, string dataA, IEnumerable<WaterfallStep> steps = null) : base(dialogId, steps)
    {
        AddStep(async (stepContext, cancellationToken) =>
        {              
            var choices = new List<Choice>();

            for (int i = 1; i<3; i++)
            {
                choices.Add(new Choice { Value = "" + i.ToString()});
            }

            PromptOptions _choicePromptOptions = new PromptOptions { Choices = choices, Prompt = stepContext.Context.Activity.CreateReply("What type of help you want? \n 1. Help 1\n 2. Help 2" ) };

            return await stepContext.PromptAsync("choicePrompt", _choicePromptOptions);

        });

        AddStep(async (stepContext, cancellationToken) =>
        {
            stepContext.Values["mamun1"].Equals("Mamun");

            var response = (stepContext.Result as FoundChoice)?.Value;

            if (response == "1")
            {
                return await stepContext.BeginDialogAsync(Help1.Id);
            }

            if (response == "2")
            {
                return await stepContext.BeginDialogAsync(Help2.Id);
            }

            return await stepContext.NextAsync();
        });

        AddStep(async (stepContext, cancellationToken) => { return await stepContext.ReplaceDialogAsync(Id); });
    }

    public static string Id => "step1Dialog";
    public static string data;
    public static Step1Dialog Instance { get;} = new Step1Dialog(Id,data);
}

第二个对话框:

public class Help1 : WaterfallDialog
{
    public Help1(string dialogId, string dataA, IEnumerable<WaterfallStep> steps = null) : base(dialogId, steps)
    {
        AddStep(async (stepContext, cancellationToken) =>
        {
            string msgFromPreviousDilog = (string) stepContext.Values["mamun1"];
            await stepContext.Context.SendActivityAsync($"Hi" + msgFromPreviousDilog );
            return await stepContext.EndDialogAsync();
        });

        AddStep(async (stepContext, cancellationToken) => { return await stepContext.ReplaceDialogAsync(Id); });
    }

    public static string Id => "help1";
    public static string data;
    public static Help1 Instance { get;} = new Help1(Id,data);
}

【问题讨论】:

  • 嗨,您能分享一下您从哪里获取代码吗?你在看教程还是什么?
  • 我应该提到Equals 执行比较,而不是分配
  • 嗨,这是我从 Microsoft 默认示例中获得的标准对话框编码。
  • 什么默认示例?您是说这是 VSIX 模板的一部分吗?哪一个?
  • 是的,它的 VSIX 模板。但我根据自己的需要进行了编辑。

标签: c# dialog chat botframework bots


【解决方案1】:

这里有几个选项。我将解释其中两个。

选项 1:对话框选项

首先,您将数据作为BeginDialogAsync 的参数传递:

return await stepContext.BeginDialogAsync(Help1.Id, "Mamun");

然后您将使用DialogInstance 的状态检索数据:

string msgFromPreviousDilog = (string)stepContext.ActiveDialog.State["options"];

请注意,您作为对话框选项传递的数据在回合之间保持不变。

选项 2:转向状态

如果您不需要数据在轮次之间保持不变,您始终可以使用轮次状态。你可以像这样添加数据来改变状态:

stepContext.Context.TurnState.Add("mamun1", "Mamun");

你可以像这样检索它:

string msgFromPreviousDilog = (string)stepContext.Context.TurnState["mamun1"];

【讨论】:

    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 2017-04-16
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多