我有一个简单的对话框,其中包含2个简单的瀑布步骤。 用户看到“今天我能为您提供什么帮助?”当它回答时,什么也没发生。我无法让Validate工作。
我想念什么吗?我正在使用SDK 4.1.5。
public ComplaintsDialog() : base(nameof(ComplaintsDialog))
{
var steps = new WaterfallStep[]
{
Ask,
Validate
};
AddDialog(new WaterfallDialog("flow", steps));
AddDialog(new TextPrompt("asking"));
}
private static async Task<DialogTurnResult> Ask(WaterfallStepContext sc, CancellationToken cancellationToken)
{
return await sc.PromptAsync("asking", new PromptOptions { Prompt = new Activity { Text = "How may I help you today?", Type= ActivityTypes.Message} }, cancellationToken);
}
private static async Task<DialogTurnResult> Validate(WaterfallStepContext sc, CancellationToken cancellationToken)
{
var answer = sc.Result;
await sc.Context.SendActivityAsync(answer.ToString());
return await sc.EndDialogAsync();
}
}
更新 我试图简化代码,这就是我目前直接从主机器人中调用ComplaintsDialog的方式。 看来当进入 await dc.ContinueDialogAsync(); 时,堆栈总是空的,所以它进入了一个循环,并一遍又一遍地启动ComplaintsDialog
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
// Create dialog context.
var dc = await _dialogs.CreateContextAsync(turnContext);
switch (turnContext.Activity.Type)
{
case ActivityTypes.Message:
// Continue outstanding dialogs.
await dc.ContinueDialogAsync();
// Begin main dialog if no outstanding dialogs/ no one responded.
if (!dc.Context.Responded)
{
await dc.BeginDialogAsync(nameof(ComplaintsDialog));
}
break;
case ActivityTypes.ConversationUpdate:
if (dc.Context.Activity.MembersAdded != null && dc.Context.Activity.MembersAdded.Any())
{
foreach (var newMember in dc.Context.Activity.MembersAdded)
{
if (newMember.Id != dc.Context.Activity.Recipient.Id)
{
await dc.BeginDialogAsync(nameof(WelcomeDialog));
}
}
}
break;
}
}
答案 0 :(得分:2)
您提供的代码示例看起来应该可以正常工作,因此问题可能出在其他地方。
我的猜测是,您正在从另一个对话框中在WaterfallStep内部启动ComplaintsDialog,因此请确保您正在像这样调用BeginDialogAsync方法:
return await stepContext.BeginDialogAsync(nameof(ComplaintsDialog));
代替:
await stepContext.BeginDialogAsync(nameof(ComplaintsDialog));
如果这不是错误,则可能需要更多信息
更新
您的问题出在OnTurnAsync方法上。您没有将新的回合保存为对话状态。交换机上的Message情况应如下所示:
case ActivityTypes.Message:
if (dc.ActiveDialog == null)
{
await dc.BeginDialogAsync(nameof(ComplaintsDialog), cancellationToken);
}
else
{
await dc.ContinueDialogAsync(cancellationToken);
}
await _accessors.ConversationState.SaveChangesAsync(turnContext);
break;
还有您的构造函数:
private readonly MyBotAccessors _accessors;
public MyBot(MyBotAccessors accessors, ILoggerFactory loggerFactory)
{
...
_accessors = accessors ?? throw new System.ArgumentNullException(nameof(accessors));
...
}