C# 如何根据每个不同的luis意图启动不同的对话框?
我试图建立一个简单的聊天机器人,用户可以说不同的意图,并基于此,它将有不同的对话框。目前我有两个可能的意图和它们对应的对话框:“listBots”和“runBot” 我将我的机器人设置为从Luis获取意图,然后使用“打开意图”确定它应该运行哪个对话框,下面是我尝试执行此操作的代码:C# 如何根据每个不同的luis意图启动不同的对话框?,c#,botframework,azure-language-understanding,C#,Botframework,Azure Language Understanding,我试图建立一个简单的聊天机器人,用户可以说不同的意图,并基于此,它将有不同的对话框。目前我有两个可能的意图和它们对应的对话框:“listBots”和“runBot” 我将我的机器人设置为从Luis获取意图,然后使用“打开意图”确定它应该运行哪个对话框,下面是我尝试执行此操作的代码: public class MainChatbot : ActivityHandler { private readonly IOptions<Models.Configurations> _myS
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;
}
公共类MainChatbot:ActivityHandler
{
私有只读IOptions\u mySettings;
受保护的只读iRecogener\u识别器;
受保护的只读BotState\u会话状态;
public Main聊天机器人(会话状态会话状态、IOOptions mySettings、聊天机器人识别器识别器)
{
_mySettings=mySettings??抛出新的ArgumentNullException(nameof(mySettings));
_识别器=识别器;
_会话状态=会话状态;
}
受保护的重写异步任务OnMessageActivityAsync(ITurnContext turnContext,CancellationToken CancellationToken)
{
var luisResult=await_recognizer.RecognizeAsync(turnContext,cancellationToken);
Models.ChatbotIntent.Intent-topuntent=luisResult.topuntent().Intent;
等待turnContext.SendActivityAsync(MessageFactory.Text($“您的意图是:{TopIntent.ToString()}”)、cancellationToken);
交换机(TopIntent)
{
案例模型.ChatbotIntent.Intent.RunBot:
var RunBotOptions=new Models.RunBotOptions();
Dialog RunBotDialog=新建RunBotDialog();
等待RunBotDialog.RunAsync(turnContext,_conversationState.CreateProperty(nameof(DialogState)),cancellationToken);
打破
案例模型.ChatbotIntent.Intent.ListBots:
Dialog ListBotDialog=新建ListBotDialog();
等待ListBotDialog.RunAsync(turnContext,_conversationState.CreateProperty(nameof(DialogState)),cancellationToken);
打破
违约:
打破
}
返回;
}
基本上,在我的OnMessageActivitySync中,它只是简单地调用Luis从用户输入中获取意图,然后打开意图,根据具体情况,它创建一个不同的对话框并启动它。至少在理论上是这样
在我的startup.cs中,我注入了所有的bot和dialog类
// 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>();
}
//此方法由运行时调用。使用此方法可将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//添加功能以注入IOptions
services.AddOptions();
//添加我们的配置对象,以便可以注入它
服务。配置(配置);
//创建启用错误处理的Bot框架适配器。
services.AddSingleton();
//将bot创建为瞬态。在这种情况下,ASP控制器需要一个IBot。
services.AddTransient();
//创建对话状态。(由对话系统本身使用。)
var storage=newmemoryStorage();
var会话状态=新会话状态(存储);
服务。AddSingleton(会话状态);
//注册路易斯识别器
services.AddSingleton();
//对话
services.AddSingleton();
services.AddSingleton();
}
这给了我一个500错误,所以我不知道出了什么问题。我使用的是bot framework v4。这段代码似乎按原样工作!不确定昨天为什么不工作。我将把它留给将来可能寻找答案的任何人