C# 中断以在Botframework上运行新对话框
我正在尝试将代码从旧的Bot Framework V4(去年1月)迁移到新的Bot Framework V4设计。我喜欢在用户输入时调用对话框。“开始”。我可以在以前的设计上轻松做到这一点,但现在不行。这也是现在的文本。如果我想使用路易斯意图怎么办?多谢各位C# 中断以在Botframework上运行新对话框,c#,botframework,C#,Botframework,我正在尝试将代码从旧的Bot Framework V4(去年1月)迁移到新的Bot Framework V4设计。我喜欢在用户输入时调用对话框。“开始”。我可以在以前的设计上轻松做到这一点,但现在不行。这也是现在的文本。如果我想使用路易斯意图怎么办?多谢各位 protected override async Task<DialogTurnResult> OnBeginDialogAsync( DialogContext i
protected override async Task<DialogTurnResult> OnBeginDialogAsync(
DialogContext innerDc,
object options,
CancellationToken cancellationToken = default(CancellationToken))
{
var result = await InterruptAsync(innerDc, cancellationToken);
if (result != null)
{
return result;
}
return await base.OnBeginDialogAsync(innerDc, options, cancellationToken);
}
protected override async Task<DialogTurnResult> OnContinueDialogAsync(
DialogContext innerDc,
CancellationToken cancellationToken)
{
var result = await InterruptAsync(innerDc, cancellationToken);
if (result != null)
{
return result;
}
return await base.OnContinueDialogAsync(innerDc, cancellationToken);
}
private async Task<DialogTurnResult> InterruptAsync(
DialogContext innerDc,
CancellationToken cancellationToken)
{
if (innerDc.Context.Activity.Type == ActivityTypes.Message)
{
var text = innerDc.Context.Activity.Text.ToLowerInvariant();
switch (text)
{
case "help":
case "?":
await innerDc.Context.SendActivityAsync($"Show Help...",
cancellationToken: cancellationToken);
return new DialogTurnResult(DialogTurnStatus.Waiting);
case "get started":
return await innerDc.BeginDialogAsync(nameof(DialogA));
case "cancel":
case "quit":
await innerDc.Context.SendActivityAsync($"Cancelling",
cancellationToken: cancellationToken);
return await innerDc.CancelAllDialogsAsync();
}
}
return null;
}
BeginDialogaSync上的受保护重写异步任务(
DialogContext innerDc,
对象选项,
CancellationToken CancellationToken=默认值(CancellationToken))
{
var结果=等待中断异步(innerDc,cancellationToken);
如果(结果!=null)
{
返回结果;
}
return wait base.OnBeginDialogAsync(innerDc,options,cancellationToken);
}
受保护的覆盖异步任务OnContinueDialogAsync(
DialogContext innerDc,
取消令牌(取消令牌)
{
var结果=等待中断异步(innerDc,cancellationToken);
如果(结果!=null)
{
返回结果;
}
返回wait base.OnContinueDialogAsync(innerDc,cancellationToken);
}
专用异步任务中断异步(
DialogContext innerDc,
取消令牌(取消令牌)
{
if(innerDc.Context.Activity.Type==ActivityTypes.Message)
{
var text=innerDc.Context.Activity.text.ToLowerInvariant();
开关(文本)
{
案例“帮助”:
案例“?”:
等待innerDc.Context.SendActivityAsync($“显示帮助…”),
cancellationToken:cancellationToken);
返回新的DialogTurnResult(DialogTurnStatus.Waiting);
案例“开始”:
return wait innerDc.BeginDialogAsync(nameof(DialogA));
“取消”案例:
“退出”案例:
等待innerDc.Context.SendActivityAsync($“取消”,
cancellationToken:cancellationToken);
return wait innerDc.CancelAllDialogsAsync();
}
}
返回null;
}
DialogA
namespace practiceNewBot.Dialogs
{
public class DialogA : CancelAndHelpDialog
{
public DialogA() : base(nameof(DialogA))
{
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
{
FirstStepAsync,
}));
// The initial child Dialog to run.
InitialDialogId = nameof(WaterfallDialog);
}
private async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"start of dialog a"), cancellationToken);
return await stepContext.PromptAsync(
nameof(ChoicePrompt),
new PromptOptions
{
Prompt = MessageFactory.Text($"choose a dialog"),
Choices = new List<Choice>
{
new Choice
{
Value = "Dialog A Child",
Synonyms = new List<string>
{
"dialog a child",
},
},
new Choice
{
Value = "Dialog B Child",
Synonyms = new List<string>
{
"dialog b chilc",
},
},
new Choice
{
Value = "Cancel",
Synonyms = new List<string>
{
"cancel",
},
},
},
RetryPrompt = MessageFactory.Text("Please choose one of the options."),
},
cancellationToken: cancellationToken);
}
private async Task<DialogTurnResult> SecondStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var response = (stepContext.Result as FoundChoice).Value.ToLower();
switch (response)
{
case "dialog a child":
return await stepContext.BeginDialogAsync(nameof(DialogA), cancellationToken: cancellationToken);
case "dialog b child":
return await stepContext.BeginDialogAsync(nameof(DialogB), cancellationToken);
case "cancel":
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Ok, Cancelled."));
return await stepContext.CancelAllDialogsAsync(cancellationToken: cancellationToken);
default:
break;
}
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"end of dialog a"), cancellationToken);
return await stepContext.EndDialogAsync();
}
}
}
namespace practiceNewBot.Dialogs
{
公共类对话框A:CancelAndHelpDialog
{
public DialogA():base(nameof(DialogA))
{
AddDialog(新建WaterWallDialog)(名称(WaterWallDialog),新建WaterWallStep[]
{
FirstStepAsync,
}));
//要运行的初始子对话框。
InitialDialogId=nameof(WaterWallDialog);
}
专用异步任务FirstStepAsync(WaterAllStepContext stepContext,CancellationToken CancellationToken)
{
等待stepContext.Context.SendActivityAsync(MessageFactory.Text($“对话框a的开始”),cancellationToken;
return wait stepContext.PromptAsync(
姓名(ChoicePrompt),
新提示
{
Prompt=MessageFactory.Text($“选择对话框”),
选项=新列表
{
新选择
{
Value=“Dialog A Child”,
同义词=新列表
{
“对话一个孩子”,
},
},
新选择
{
Value=“Dialog B Child”,
同义词=新列表
{
“对话b chilc”,
},
},
新选择
{
Value=“取消”,
同义词=新列表
{
“取消”,
},
},
},
RetryPrompt=MessageFactory.Text(“请选择其中一个选项”),
},
cancellationToken:cancellationToken);
}
专用异步任务SecondStepAsync(WaterWallStepContext stepContext,CancellationToken CancellationToken)
{
var response=(stepContext.Result作为FoundChoice.Value.ToLower();
开关(响应)
{
案例“对话子项”:
return wait-stepContext.BeginDialogAsync(nameof(DialogA),cancellationToken:cancellationToken);
案例“对话b子项”:
return wait-stepContext.BeginDialogAsync(nameof(DialogB),cancellationToken);
“取消”案例:
wait stepContext.Context.SendActivityAsync(MessageFactory.Text($“确定,已取消”);
返回等待stepContext.CancelAllDialogsAsync(cancellationToken:cancellationToken);
违约:
打破
}
Wait stepContext.Context.SendActivityAsync(MessageFactory.Text($“对话a结束”),cancellationToken;
返回wait-stepContext.EndDialogAsync();
}
}
}
您也可以发布DialogA的代码吗?我已经添加了它。那么FirstStepAsync
中的代码呢?我假设您正在返回一个对话框turnsult
?为了调试该问题,我们还需要查看此代码。@MattStannett谢谢。我现在无法使用计算机。明天将更新这个问题。@MattStannett我已经更新了。你也可以发布DialogA的代码吗?我已经添加了。那么FirstStepAsync
中的代码呢?我假设您正在返回一个对话框turnsult