In previous articles, I have explained how to create and configure a LUIS app. In this article, we will discuss how to integrate this app in Bot application created using Microsoft Bot Framework.
We are going to build a bot which will reply as per the intents which we have added in the previous article.
Highlights of the article
- Publish app
- Consume LUIS app to extract intents and entities. You can pull code from here.
Prerequisite
- Create LUIS app - refer previous article.
- Be ready with bot framework dev environment - refer this article.
Publish LUIS app
Click on ‘Publish App’ option in left side bar menu. It will show the following page.
We can enable verbose flag and Bing spell check if required. You can select time zone as per requirement.


It will show success message and Endpoint url to access service over HTTP.

https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/{{app_id}}?subscription-key={{subscription_key}}&timezoneOffset=0&verbose=true&spellCheck=false&q=
To test output of service, I hit the above url with different values for parameter ‘q’.
q=Hi, Output - top scoring intent – Greet.Welcome
- {
- "query": "hi",
- "topScoringIntent": {
- "intent": "Greet.Welcome",
- "score": 0.792347133
- },
- }
- {
- "query": "bye",
- "topScoringIntent": {
- "intent": "Greet.Farewell",
- "score": 0.455509841
- },
- }
- {
- "query": "Search Amit",
- "topScoringIntent": {
- "intent": "Search.People",
- "score": 0.500412941
- },
- }
If you are ready with prerequisites for development with bot framework, then let’s create new dialog in Dialog folder as IntelligentDialog and new Controller as IntelligentController.

IntelligentController.cs
I have updated Post( ) method to invoke IntelligentDialog with parameter activity.
- public async Task < HttpResponseMessage > Post([FromBody] Activity activity) {
- if (activity.Type == ActivityTypes.Message) {
- await Conversation.SendAsync(activity, () => new Dialogs.IntelligentDialog(activity));
- } else {
- HandleSystemMessage(activity);
- }
- var response = Request.CreateResponse(HttpStatusCode.OK);
- return response;
- }
IntelligentDialog.cs
- [Serializable]
- [LuisModel(Constants.LUIS_EMPLOYEE_HELPER_APP_ID, Constants.LUIS_SUBSCRIPTION_KEY)]
- public class IntelligentDialog: LuisDialog < object > {
- private string userName;
- private DateTime msgReceivedDate;
- public IntelligentDialog(Activity activity) {
- userName = activity.From.Name;
- msgReceivedDate = activity.Timestamp ? ? DateTime.Now;
- }
- [LuisIntent("")]
- [LuisIntent("none")]
- [LuisIntent("None")]
- public async Task None(IDialogContext context, LuisResult luisResult) { ...
- }
- [LuisIntent("Greet.Welcome")]
- public async Task GreetWelcome(IDialogContext context, LuisResult luisResult) { ...
- }
- [LuisIntent("Greet.Farewell")]
- public async Task GreetFarewell(IDialogContext context, LuisResult luisResult) { ...
- }
- [LuisIntent("Search.People")]
- public async Task SearchPeople(IDialogContext context, LuisResult luisResult) { ...
- }
- }
We need to inherit IntelligentDialog from LuisDialog<object> to use LUIS intelligence. After inheritance, to integrate LUIS app we need to specify attribute LuisModel with LUIS app ID and your subscription key, for IntelligentDialog class. You can get these values from HTTP endpoint url.
I have added two private properties username and msgReceivedDate to IntelligentDialog class. Initialize those properties in its constructor (shown below). We are going to use these properties while communicating with user.
I have added methods for each Intent specified in LUIS app. To invoke those methods when intent matches, we need to specify LuisIntent attribute for each method with intent name as parameter.
For chat text from user ‘Hello Employee Helper’, intent will be matched with ‘Greet.Welcome’ and ‘GreetWelcome( )’ method will be invoked.
- [LuisIntent("Greet.Welcome")]
- public async Task GreetWelcome(IDialogContext context, LuisResult luisResult) {
- string response = string.Empty;
- if (this.msgReceivedDate.ToString("tt") == "AM") {
- response = $ "Good morning, {userName}. :)";
- } else {
- response = $ "Hey {userName}. :)";
- }
- await context.PostAsync(response);
- context.Wait(this.MessageReceived);
- }
For chat text from user ‘Thanks dude, talk to you later’, intent will be matched with ‘Greet.Farewell’ and ‘GreetFarewell( )’ method will be invoked. In this method, we are sending response on the basis of ‘AM’ or ‘PM’ of msgReceivedDate.
- [LuisIntent("Greet.Farewell")]
- public async Task GreetFarewell(IDialogContext context, LuisResult luisResult) {
- string response = string.Empty;
- if (this.msgReceivedDate.ToString("tt") == "AM") {
- response = $ "Good bye, {userName}.. Have a nice day. :)";
- } else {
- response = $ "b'bye {userName}, Take care.";
- }
- await context.PostAsync(response);
- context.Wait(this.MessageReceived);
- }
For chat text ‘Could you please find Akshay’ from user, intent will be matched with ‘Search.People’ and ‘SearchPeople( )’ method will be invoked. In this method, we check whether Entity ‘Person.Name’ is present in LuisResult. With TryFindEntity employee name is extracted from result.
- [LuisIntent("Search.People")]
- public async Task SearchPeople(IDialogContext context, LuisResult luisResult) {
- EntityRecommendation employeeName;
- string name = string.Empty;
- if (luisResult.TryFindEntity("Person.Name", out employeeName)) {
- name = employeeName.Entity;
- }
- await context.PostAsync($ "You have searched for {name}");
- context.Wait(this.MessageReceived);
- }
Hit F5 to test a bot application. It will host an application with IIS Express and open browser. To understand the control flow, insert breakpoints in Post( ) method of IntelligentController and each method of IntelligentDialog.
Now launch bot emulator app. Put URL as 'http://localhost:{{port no. from browser url}}/api/intelligent' to connect emulator with bot application. Keep App ID and App Password blank, click on connect.
Keep teaching (more intents to your LUIS app) and keep chatting!


Savvy SequeiraPosted Jul 30, 2019, 5:23 AM
Hi Akshay, I am working on a Luis Chatbot. While testing it locally on Bot Emulator, I am not being able to connect to the bot. It shows" Taking longer than usual to connect". I have selected the ngrok in the settings as well but still no luck. Could you please help me with it.
Raja BaradPosted Oct 29, 2018, 5:04 AM
Hello,how to connect to sql database when one of luis intent is being hit, build a form and store the information back into database (visual studio). please help.. i will be grateful
rashik rashPosted Mar 14, 2018, 4:27 AM
I connected to my LUIS app using the published url. But it gives error code:500 while calling using httpclient. What might be the possible error? Thanks in advance.
Mansour RakiaPosted Mar 6, 2018, 6:54 AM
Hello what is the error please "Exception: The response status code does not indicate success: 404 (Resource Not Found).[File type 'text / plain']"
lalitha kantipudiPosted Jan 2, 2018, 12:00 PM
Hello, I am working on chatbot using Luis. It should identify the emotion in the text and should reply with an image or a video. How can I add them?
prasanna rajPosted Dec 21, 2017, 4:42 AM
Hi Akshay, sorry for asking lot of questions, is there any way to achieve auto complete , finally we want to use in Skype Bot.
prasanna rajPosted Dec 21, 2017, 2:10 AM
Hi Akshay, thanks for replay , is there any option using @symbol to populate data's from database or Json, please refer this link "https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/bots/bots-cards". am trying for auto complete past three days , please check the link and let me know if possible or not.
prasanna rajPosted Dec 20, 2017, 5:22 AM
Hi Akshay, help me is there any way to do autocomplete (from database or Json) in Bot framework emulator ?
prasanna rajPosted Dec 19, 2017, 2:53 AM
Hi Akshay Deshmukh, Nice job man, its usefull to me , i want to do autocomplete with same , i checked Knowledge Exploration Service API , i dont know how to use in .net framework , please give me suggestion .
Ananth BabuPosted Dec 1, 2017, 4:26 AM
Hi Akshay, correct me if I am wrong, I have seen the BotAuthentication attribute in controller class not from dialog, even I removed from there also I am getting the same error.
Ananth BabuPosted Nov 30, 2017, 11:15 AM
Hi Akshay, I am getting 401 un-authorized error message while running the bot emulator even the key has been given.