简体   繁体   English

如何根据每个不同的 luis 意图启动不同的对话?

[英]How can I start a different dialog based on each different luis intent?

I am trying to setup a simple chatbot where the user can say different intent and based on that it will have different dialog.我正在尝试设置一个简单的聊天机器人,用户可以在其中说出不同的意图,并且基于它会有不同的对话。 Currently I have 2 possible intent and their corresponding dialogs: "listBots" and "runBot".目前我有 2 个可能的意图及其相应的对话框:“listBots”和“runBot”。

I setup my bot to get the intent from Luis, then use switch on intent to determine which dialog it should run, here is my code that try to do this:我设置了我的机器人以从 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;
    }

Basically in my OnMessageActivityAsync, it simply invoke Luis to get the intent from the user input, then switch on the intent, based on the case, it create a different dialog and start it.基本上在我的 OnMessageActivityAsync 中,它只是调用 Luis 从用户输入中获取意图,然后打开意图,根据情况,它创建一个不同的对话框并启动它。 At least in theory.至少在理论上。

Here in my startup.cs, I dependency inject all the bot and the dialog classes.在我的 startup.cs 中,我依赖注入了所有机器人和对话框类。

    // 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>();
    }

This is giving me a 500 error, so I don't know what's wrong.这给了我一个 500 错误,所以我不知道出了什么问题。 I am using bot-framework v4.我正在使用机器人框架 v4。

It seems this code works as is!看来这段代码按原样工作! Not sure why it didn't work yesterday.不知道为什么它昨天不起作用。 I'll leave it up for anyone who might be looking for answer in the future.我会把它留给将来可能寻找答案的任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM