如果用户类型退出,而不是选择选择(退出不是在选择中列出),我需要处理退出。默认情况下,Bot不允许输入未列出的内容。
你可以尝试实施
global message handler
每当用户用特定单词回复对话时执行代码的
Exit
. 下面的代码片段供您参考。
全局消息handlersbotmodule:
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();
}
}
ExitScorable:
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;
}
}