In our previous post, we went over what Microsoft’s LUIS, or Language Understanding, is and showed how to create a new LUIS app as well as calling it using the APIs. In this post we will use the same LUIS app that we built but integrate it into a bot we will create from Microsoft’s Bot Framework.

By integrating LUIS into the Bot Framework we can allow our bots to be more intelligent and to help automate common tasks. We can easily create a bot and test it all in the Azure Portal.

Creating the Bot

To create a web app bot in Azure, in the Azure Portal go to “Create New Resource” and search for “bot”. Then click on “Web App Bot”.

Click on “Create” and you get a blade where you can start entering in the settings.

There are a few settings to pay particular attention to:

  • Bot name – This needs to be unique across Azure.
  • Pricing tier – Choose “F0” for the free tier.
  • Bot template – Choose “Language understanding”. This can be in C# or JavaScript under Node. The rest of the post will use the Node template.

Create a new App Service Plan and check to confirm the terms then click “Create” to let everything start deploying. It may take a couple of minutes before it finishes. Once it does, you can go to it and on the left blade select “Test in web chat”. Enter in some text to make sure your bot is up and running. If it returns similar to the below, you’re good to go!

Integrate and Use our LUIS App

To integrate our LUIS app into our bot we just have to update the app keys that our bot is using. In our bot resource, go to “Application Settings” and scroll down near the end of the “App settings” section you will see some LUIS items – LuisAPIKey and LuisAppId. We only need to update the LuisAppId to match the one we have with our current LUIS app. To get the app ID, go back to your LUIS app and click on the Settings in the upper navigation. Paste that and click “Save”.

Now all that’s left is to update the code to our bot to handle the intents and entities from our LUIS app. Back in the left pane of our bot, go to “Build” under “Bot Management” and click to open the online code editor. Being able to update our bot’s code in the browser is a really nice feature Azure provides us.

Feel free to peruse the code to get a better sense of what all it does. To implement our LUIS app, though, in the app.js file after the comment “Create your bot with a function to receive messages from the user” replace what’s there with the following code:

var bot = new builder.UniversalBot(connector, function (session, args) {
    session.send('You reached the default message handler. You said \'%s\'.', session.message.text);

bot.set('storage', tableStorage);

// Make sure you add code to validate these fields
var luisAppId = process.env.LuisAppId;
var luisAPIKey = process.env.LuisAPIKey;
var luisAPIHostName = process.env.LuisAPIHostName || '';

const LuisModelUrl = 'https://' + luisAPIHostName + '/luis/v1/application?id=' + luisAppId + '&subscription-key=' + luisAPIKey;

// Main dialog with LUIS
var recognizer = new builder.LuisRecognizer(LuisModelUrl);

    (session, args) => {
        // Resolve and store any CreateNote entities passed from LUIS.
        var intent = args.intent;
        var listName = builder.EntityRecognizer.findEntity(intent.entities, 'NoteName');

        // Add to the list if NoteName entity is detected by LUIS
        if (listName) {
            session.send('Ok, adding to the %s list.', listName.entity);
            // Put your code here for calling a service that adds to a list.
        } else {
            // Assuming default list to create
            session.send('Ok, creating a new default list');
            // Put your code here for calling the a service that creates a new default list.
    matches: 'CreateNote'

The main section is the bot.dialog function. We pass in a unique string ID, CreateNoteDialogand inside here it finds any entities. If it does it acts on the entity, but if not it just does the default of creating a new list. In the triggerAction method that’s chained on, it’s important to set matches to your intent name.

The online editor automatically saves changes to it, so to test we’ll go back to test in the web chat. It may take a minute or two for the bot to recognize the new changes, but it should now handle the utterances accordingly.

We just made our bot more intelligent!

In this post, we went over how to integrate a LUIS app that was already created a web app bot on Microsoft’s Bot Framework. Feel free to add more items to our intent and implement those into your bot. Perhaps have the bot recognize a list to append to a note. Or add a completely new intent. You can play around with the pre-built intents to get some ideas.