0
votes

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".

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:

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. At least in theory.

Here in my startup.cs, I dependency inject all the bot and the dialog classes.

    // 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. I am using bot-framework v4.

1

1 Answers

1
votes

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.