I’ll be speaking at Visual Studio Live!, San Diego, October 7-11 
 I’ll be presenting the following sessions:
 W11 - Building Business Applications Using Bots
 W19 - Using The Microsoft Cognitive Custom Vision Service
 As a speaker, I can offer $400 savings on the 4-day package.
 Register here: http://bit.ly/SDSPK27Home 

Using LUIS -Language Understanding Intelligent Service (MS Bot Framework V4 Preview Edition)

Jul 14

Written by:
7/14/2018 2:42 PM  RssIcon


The Microsoft Bot Framework V4 allows you to create intelligent bots that interact naturally wherever your users are (text/SMS to Skype, Slack, Office 365 mail and other popular services). In preview now, the Bot Builder V4 Preview SDK offers new features and is extensible with a pluggable middleware model.

In this article we will build on the example started in the following article: Using Dialogs (MS Bot Framework V4 Preview Edition). In this article we will cover integrating LUIS (Language Understanding Intelligent Service) into your Bot. Using Language Understanding Intelligent Service (LUIS) in your Bot allows you to create chat bots that are easier for your end-users to interact with.

Tutorial Series

Other articles in this series:



Why Use LUIS?

The Microsoft Language Understanding Intelligent Service (LUIS) allows you to create intelligent applications that allow your end-users to use natural language to perform actions.

The Language Understanding Intelligent Service (LUIS), allows developers to create language understanding models that are specific to their problem domain. To use LUIS, developers log into the LUIS website, enter a few utterances and their labels, and then deploy a model to an HTTP endpoint on Azure.

The utterances sent to the endpoint are logged and labeled. The website allows the developer (or any application administrator) to train the application by identifying issues, which can be resolved by either adding more labels or by giving hints to the machine learner in the form of features.

A developer can create and deploy a model in minutes, and then maintain and train it as the usage of the application grows.


Create The LUIS Application


To get started, go to: https://www.luis.ai, create an account, and sign in with your Azure account.


Select Create new app.


Give the app a name and click Done.


Select Intents then Add prebuilt domain intent.


Search for help and select Utilities.Help and click Done.

We are basically creating a LUIS app to detect when a person is asking for help.


Click Train.


Publish the app.


Scroll down the Publish page and copy and save the Subscription Key and the URI Location (you will need them later).

Note: Only use this Subscription Key for development, for Production see: Creating Endpoint Keys using Azure


Go to the Settings page and copy and save the Application ID (you will also need it later).

Update The Bot Code


Open the project created in the article: Using Dialogs (MS Bot Framework V4 Preview Edition) in Visual Studio 2017 (or higher).

Right-click on the project node and select Manage NuGet Packages.


Search for Microsoft.Bot.Builder.Ai.LUIS.

Select include prerelease.

Click on Microsoft.Bot.Builder.Ai.LUIS, then click the Install button.

(accept any other screens that appear and fully install the package)


Open the EchoBot.cs file.

Add the following using statements to the top of the file:


using Microsoft.Bot.Builder.Ai.LUIS;
using System.Threading;


Add the following code below the 

await DialogContext.Continue();


            // Create a LuisRecognizer
            // to pass the response through LUIS                    
            LuisRecognizer lRecognizer = new LuisRecognizer(new LuisModel(
                "** Your Application ID **",
                "** Your Subscription Key **",
                new System.Uri("** service endpoint **"),
                Microsoft.Cognitive.LUIS.LuisApiVersion.V2), new LuisRecognizerOptions() { Verbose = true });
            // Create a CancellationToken
            CancellationToken ct;
            // context.Activity.Text is the user utterance
            // Pass it through the LUIS lRecognizer passing the CancellationToken
            // Call LUIS and get an lResult 
            var lResult = await lRecognizer.Recognize(context.Activity.Text, ct);
            // Get the top Intent returned from the LUIS response
            var topIntent = lResult?.GetTopScoringIntent();
            // Get the Intent as a string
            string strIntent = (topIntent != null) ? topIntent.Value.intent : "";
            // Get the IntentScore as a double
            double dblIntentScore = (topIntent != null) ? topIntent.Value.score : 0.0;
            // Only proceed with LUIS if there is an Intent 
            // and the score for the Intent is greater than 95
            if (strIntent != "" && (dblIntentScore > 0.95))
                switch (strIntent)
                    case "None":
                        await context.SendActivity("Sorry, I don't understand.");
                    case "Utilities_Help":
                        await context.SendActivity("<here's some help>");
                        // Received an intent we didn't expect, so send its name and score.
                        await context.SendActivity(
                            $"Intent: {topIntent.Value.intent} ({topIntent.Value.score}).");



  • Enter the Application ID you saved earlier where it indicates ** Your Application ID ** (it must be between the quotes)
  • Enter the Subscription Key you saved earlier where it indicates ** Your Subscription Key ** (it must be between the quotes)
  • Enter the URI Location you saved earlier where it indicates ** service endpoint ** (it must be between the quotes)


To handle the situations where LUIS does not return an Intent with a high enough score, change the remaining code in the method to the following to wrap it into an else block:


                // Normal process using Dialogs
                if (!context.Responded)
                    if (!userstate.AskedForUserName)
                        // A dialog won't be started without calling the .begin command 
                        // passing the name of the dialog to be started
                        await DialogContext.Begin("greetings");
                        // Set AskedForUserName so we don't ask again
                        userstate.AskedForUserName = true;
                        // Echo back to the user whatever they typed.
                        await context.SendActivity(
                            $"Hello {userstate.UserName} you said '{context.Activity.Text}'");



Run The Updated Code


We can now run the application and connect to it with the Bot Framework Emulator (V4 PREVIEW) (see: Creating a Hello World! Bot (MS Bot Framework V4 Preview Edition) for information on how to do that).



Run the project, return to the Bot Emulator, click the Start Over button, and chat with the updated Bot.


Special Thanks

A special thanks to Vishwac Sena Kannan, for assistance. Without his help this post would not be possible (however, any errors are my own).



Azure Bot Service Documentation for the v4 SDK

How your bot gets messages from LUIS

Using LUIS for Language Understanding

Conversational AI: Best Practices for Building Bots



You can download the code from the Download page