我试图在MS Bot Framework 4的2个对话框之间传递数据。但是,它不起作用。我已经使用stepContext.Values。但是它不能将数据转发到另一个对话框。我是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);
}
答案 0 :(得分:0)
您在这里有一些选择。我将解释其中的两个。
首先,您将数据作为BeginDialogAsync
的参数传递:
return await stepContext.BeginDialogAsync(Help1.Id, "Mamun");
然后,您将使用DialogInstance
的状态来检索数据:
string msgFromPreviousDilog = (string)stepContext.ActiveDialog.State["options"];
请注意,您作为对话框选项传递的数据在回合之间仍然存在。
如果不需要数据在转弯之间保持不变,则可以始终使用转弯状态。您可以像这样添加数据以转换状态:
stepContext.Context.TurnState.Add("mamun1", "Mamun");
您可以像这样检索它:
string msgFromPreviousDilog = (string)stepContext.Context.TurnState["mamun1"];