Microsoft Bot Framework: using a LuisDialog for processing intents

- 2 mins

In the previous post, I blogged about integrating a Holographic 2D app with Bot Framework and LUIS.

I also spent some time going through these great samples for some presentations and found a very nice implementation for handling the Bot messages when LUIS intents are recognised.

Instead of using the exposed LUIS endpoint and parsing the returned JSON, the framework already provides a specific LuisDialog<> type which can be used for handling the various intents, in order to make the code cleaner and more extensible.

I've then modified the HoloLensBotDemo sample and added a new RootLuisDialog:

[LuisModel("YourModelId", "YourSubscriptionKey")]
[Serializable]
public class RootLuisDialog : LuisDialog<object>
{
    [LuisIntent("")]
    [LuisIntent("None")]
    public async Task None(IDialogContext context, LuisResult result)
    {
        var errorResult = "Try something else, please.";
        await context.PostAsync(errorResult);
        context.Wait(this.MessageReceived);
    }

    [LuisIntent("FavouriteTechnologiesIntent")]
    public async Task FavouriteTechnologiesIntent(IDialogContext context, IAwaitable&lt;IMessageActivity&gt; activity, LuisResult result)
    {
        await context.PostAsync("My favourite technologies are Azure, Mixed Reality and Xamarin!");
        context.Wait(this.MessageReceived);
    }

    [LuisIntent("FavouriteColorIntent")]
    public async Task FavouriteColorIntent(IDialogContext context, IAwaitable&lt;IMessageActivity&gt; activity, LuisResult result)
    {
        await context.PostAsync("My favourite color is Blue!");
        context.Wait(this.MessageReceived);
    }

    [LuisIntent("WhatsYourNameIntent")]
    public async Task WhatsYourNameIntent(IDialogContext context, IAwaitable&lt;IMessageActivity&gt; activity, LuisResult result)
    {
        await context.PostAsync("My name is Davide, of course :)");
        context.Wait(this.MessageReceived);
    }

    [LuisIntent("HelloIntent")]
    public async Task HelloIntent(IDialogContext context, IAwaitable&lt;IMessageActivity&gt; activity, LuisResult result)
    {
        await context.PostAsync(@"Hi there! Davide here :) This is my personal Bot. Try asking 'What are your favourite technologies?'");
        context.Wait(this.MessageReceived);
    }
}

And this is all the code now needed for the Bot.

The updated source code is available on GitHub.

Happy coding!

Davide Zordan

Davide Zordan

Senior Software Engineer

rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora