我正在尝试建立一个简单的聊天机器人,用户可以在其中说出不同的意图,并基于此有不同的对话框。目前,我有2种可能的意图及其对应的对话框:“ listBots”和“ runBot”。
我设置了我的机器人以从Luis获取意图,然后使用意图切换来确定它应该运行哪个对话框,这是我尝试执行此操作的代码:
public class MainChatbot : ActivityHandler
{
private readonly IOptions<Models.Configurations> _mySettings;
protected readonly IRecognizer _recognizer;
protected readonly BotState _conversationState;
public MainChatbot(ConversationState conversationState, IOptions<Models.Configurations> mySettings, ChatbotRecognizer recognizer)
{
_mySettings = mySettings ?? throw new ArgumentNullException(nameof(mySettings));
_recognizer = recognizer;
_conversationState = conversationState;
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var luisResult = await _recognizer.RecognizeAsync<Models.ChatbotIntent>(turnContext, cancellationToken);
Models.ChatbotIntent.Intent TopIntent = luisResult.TopIntent().intent;
await turnContext.SendActivityAsync(MessageFactory.Text($"Your Intention Is: {TopIntent.ToString()}"), cancellationToken);
switch (TopIntent)
{
case Models.ChatbotIntent.Intent.RunBot:
var RunBotOptions = new Models.RunBotOptions();
Dialog RunBotDialog = new RunBotDialog();
await RunBotDialog.RunAsync(turnContext, _conversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
break;
case Models.ChatbotIntent.Intent.ListBots:
Dialog ListBotDialog = new ListBotDialog();
await ListBotDialog.RunAsync(turnContext, _conversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
break;
default:
break;
}
return;
}
基本上是在我的OnMessageActivityAsync中,它只是调用Luis从用户输入中获取意图,然后打开该意图,根据情况,它会创建一个不同的对话框并启动它。至少在理论上。
在我的startup.cs中,我依赖注入了所有bot和对话框类。
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Add functionality to inject IOptions<T>
services.AddOptions();
// Add our Config object so it can be injected
services.Configure<Models.Configurations>(Configuration);
// Create the Bot Framework Adapter with error handling enabled.
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddTransient<IBot, Dialogs.MainChatbot>();
// Create the Conversation state. (Used by the Dialog system itself.)
var storage = new MemoryStorage();
var conversationState = new ConversationState(storage);
services.AddSingleton(conversationState);
// Register LUIS recognizer
services.AddSingleton<ChatbotRecognizer>();
// Dialogs
services.AddSingleton<Dialogs.RunBotDialog>();
services.AddSingleton<Dialogs.ListBotsDialog>();
}
这给我500错误,所以我不知道怎么了。我正在使用bot-framework v4。
似乎此代码按原样运行!不知道为什么昨天没用。我会将它留给将来可能需要答案的任何人。