我正在为我自己的目的,为Bot Framework V4(带有LUIS)破解GitHub'CoreBot'示例代码,并且在响应和瀑布对话框步骤的执行方式上遇到了麻烦。
我有一个顶层对话框。我的期望是,该对话框基于输入对LUIS进行初始调用,并基于该输入路由到其他对话框。目前,只能问候机器人并报告危险。我的对话框设置如下(忽略BookingDialog,它是示例的一部分)。
public MainDialog(IConfiguration configuration, ILogger<MainDialog> logger)
: base(nameof(MainDialog))
{
Configuration = configuration;
Logger = logger;
AddDialog(new TextPrompt(nameof(TextPrompt)));
AddDialog(new BookingDialog());
AddDialog(new HazardDialog());
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
{
MainStepAsync,
EndOfMainDialogAsync
}));
// The initial child Dialog to run.
InitialDialogId = nameof(WaterfallDialog);
}
我期望MainStepAsync被执行,并运行以下命令:
private async Task<DialogTurnResult> MainStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
CoreBot.StorageLogging.LogToTableAsync($"Main step entered. I am contacting LUIS to determine the intent");
string what_i_got = await LuisHelper.ExecuteMyLuisQuery(Configuration, Logger, stepContext.Context, cancellationToken);
CoreBot.StorageLogging.LogToTableAsync($"LUIS intent matched: {what_i_got}");
//await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text($"Hi! My name is Sorella. Your intent was {what_i_got}") }, cancellationToken);
switch (what_i_got)
{
case "Hazard":
StorageLogging.LogToTableAsync($"We have been asked to report a hazard");
StorageLogging.LogToTableAsync(stepContext);
var hazardDetails = new ResponseSet.Hazard();
return await stepContext.BeginDialogAsync(nameof(HazardDialog), hazardDetails, cancellationToken);
case "Greeting":
StorageLogging.LogToTableAsync($"We have been asked to provide a greeting. After this greeting the waterfall will end");
StorageLogging.LogToTableAsync(stepContext);
await stepContext.Context.SendActivityAsync(MessageFactory.Text("Hi there! :). What can I help you with today? "), cancellationToken);
return await stepContext.EndDialogAsync(null, cancellationToken);
default:
StorageLogging.LogToTableAsync($"We got an intent we haven't catered for. After this the waterfall will end");
StorageLogging.LogToTableAsync(stepContext);
return await stepContext.NextAsync(null, cancellationToken);
}
}
如果意图是危险,则开始危险对话框。否则,如果他们在问候机器人,请打个招呼并结束此顶级瀑布对话框。如果用户被路由到HazardDialog,则启动下一个Waterfall,设置如下:
public class HazardDialog : CancelAndHelpDialog
{
public HazardDialog()
: base(nameof(HazardDialog))
{
AddDialog(new TextPrompt(nameof(TextPrompt)));
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
{
GetInitialHazardInfoAsync,
GetHazardUrgencyAsync,
FinalStepAsync
}));
// The initial child Dialog to run.
InitialDialogId = nameof(WaterfallDialog);
}
首先要求他们提供危害的描述:
private async Task<DialogTurnResult> GetInitialHazardInfoAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
CoreBot.StorageLogging.LogToTableAsync("Entered hazard step 1. Asking for hazard type");
var hazardDetails = (ResponseSet.Hazard)stepContext.Options;
hazardDetails.HazardType = (string)stepContext.Result;
if (hazardDetails.HazardType == null)
{
CoreBot.StorageLogging.LogToTableAsync($"No hazard type provided. Asking");
return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("What kind of hazard would you like to report? Provide me a brief description") }, cancellationToken);
}
else
{
CoreBot.StorageLogging.LogToTableAsync($"Hazard provided. Moving on");
return await stepContext.NextAsync(hazardDetails.HazardType, cancellationToken);
}
}
那么紧急:
private async Task<DialogTurnResult> GetHazardUrgencyAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
CoreBot.StorageLogging.LogToTableAsync($"Entered hazard step 2. Asking for urgency");
var hazardDetails = (ResponseSet.Hazard)stepContext.Options;
hazardDetails.HazardType = (string)stepContext.Result;
var hazardAsJson = JsonConvert.SerializeObject(hazardDetails);
StorageLogging.LogToTableAsync(hazardAsJson);
if (hazardDetails.HarzardUrgency == null)
{
CoreBot.StorageLogging.LogToTableAsync($"No urgency provided. Asking");
return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text($"Thanks. So your hazard is {hazardDetails.HazardType}? How urgent is it?") }, cancellationToken);
}
else
{
CoreBot.StorageLogging.LogToTableAsync($"Urgency given. We're all done");
var guid = Guid.NewGuid();
var ticketId = "HAZ" + Convert.ToString(guid).ToUpper().Substring(1,4);
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Thanks! I've got all the informatio I need. I'll raise this with the API team on your behalf. Your Ticket ID is: {ticketId} "), cancellationToken);
return await stepContext.NextAsync(cancellationToken, cancellationToken);
}
}
如果我们同时具有紧急性和类型性,那么我们“举起票”并进入最后一步,该步骤刚刚结束堆栈。
private async Task<DialogTurnResult> FinalStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
CoreBot.StorageLogging.LogToTableAsync($"Entered hazard step 3. Final step");
var hazardDetails = (ResponseSet.Hazard)stepContext.Options;
hazardDetails.HarzardUrgency = (string)stepContext.Result;
var hazardAsJson = JsonConvert.SerializeObject(hazardDetails);
StorageLogging.LogToTableAsync(hazardAsJson);
return await stepContext.EndDialogAsync(hazardDetails, cancellationToken);
}
我的期望是要结束HarzardDialog,然后应返回到“ Parent”瀑布对话框的下一步,即EndOfMainDialogAsync,它只是说我们已经完成了,我还能做些什么来帮助您?
private async Task<DialogTurnResult> EndOfMainDialogAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
StorageLogging.LogToTableAsync($"Ending the main dialog");
StorageLogging.LogToTableAsync(stepContext);
await stepContext.Context.SendActivityAsync(MessageFactory.Text("Ok, I think we're all done with that. Can I do anything else to help?"), cancellationToken);
return await stepContext.EndDialogAsync(null, cancellationToken);
}
但是,在我实际的对话中,该流最终会出现异常,实际上(如果您查看下面的对话)从子瀑布中触发GetHazardUrgencyAsync,然后从父瀑布中触发MainStepAsync,然后从子瀑布中触发GetHazardUrgencyAsync。第二次?
更新:根据建议,我已经将WaterFallDialogs更新为具有唯一的名称,并重新测试。我仍然有错误的行为。参见下面的屏幕截图:
我的期望是,在描述了危害之后,我接下来被问到它有多紧急。相反,我在“块”中得到以下对话框响应。
我在这里迷路了。我会从代码中想象/想到,子瀑布对话框在返回父对话框之前需要完全实现/结束。
答案 0 :(得分:2)
对话框名称在漫游器中是全局唯一的。您的两个瀑布对话框均名为“ WaterfallDialog”。因此,您基本上是在即时交换瀑布。
将它们更改为唯一名称。
AddDialog(new WaterfallDialog("MainDialogWaterfall", new WaterfallStep[]
{
MainStepAsync,
EndOfMainDialogAsync
}));
AddDialog(new WaterfallDialog("HazardInfoWaterfallDialog", new WaterfallStep[]
{
GetInitialHazardInfoAsync,
GetHazardUrgencyAsync,
FinalStepAsync
}));
答案 1 :(得分:2)
在您的示例中,一切似乎都很好。我实际上进行了尝试,但没有发现任何问题,因此必须是您在问题中忽略的某种外部依赖性。
请参考此示例,该示例恰好使用您的代码并按预期工作。唯一的区别是它没有联系LUIS,但那应该没有任何区别。
存储库:https://github.com/jvanderbiest/Core-bot-troubleshooting
输出:
答案 2 :(得分:0)
您的问题:BOT的行为不当
解决方案: 运行bot时,在保存代码更改的同时,它将再次重新启动对话,这是在工作流程上再次启动对话框的原因。
只需保存然后重新启动对话=>它将正常工作