饮歌长啸
如果用户键入退出,而不是选择选项(退出未列在选项中),我需要处理退出。默认情况下,Bot 不允许输入未列出的内容。您可以尝试实现全局消息处理程序,以便在用户使用特定单词回复对话时执行代码Exit。以下代码片段供您参考。GlobalMessageHandlersBotModule:public class GlobalMessageHandlersBotModule : Module{ protected override void Load(ContainerBuilder builder) { base.Load(builder); builder .Register(c => new ExitScorable(c.Resolve<IDialogTask>())) .As<IScorable<IActivity, double>>() .InstancePerLifetimeScope(); }}退出可评分:public class ExitScorable : ScorableBase<IActivity, string, double>{ private readonly IDialogTask task; public ExitScorable(IDialogTask task) { SetField.NotNull(out this.task, nameof(task), task); } protected override async Task<string> PrepareAsync(IActivity activity, CancellationToken token) { var message = activity as IMessageActivity; if (message != null && !string.IsNullOrWhiteSpace(message.Text)) { if (message.Text.ToLower().Equals("exit", StringComparison.InvariantCultureIgnoreCase)) { return message.Text; } } return null; } protected override bool HasScore(IActivity item, string state) { return state != null; } protected override double GetScore(IActivity item, string state) { return 1.0; } protected override async Task PostAsync(IActivity item, string state, CancellationToken token) { this.task.Reset(); } protected override Task DoneAsync(IActivity item, string state, CancellationToken token) { return Task.CompletedTask; }}