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
Publish LUIS app
Click on ‘Publish App’ option in left side bar menu. It will show the following page.
Here, you can choose Endpoint Key. Select purchased key. If you have not purchased any key, then select Bootstrap key, which is for experimental purpose.
In ‘Publish Settings’, you can select Endpoint slots as Staging or Production. If you select it as Staging, it will be available over http but you can’t access it though chat bot app. For our development, select it as Production.
We can enable verbose flag and Bing spell check if required. You can select time zone as per requirement.
Under ‘Add Key Association’, we can add key required for BingSpellChecker, if enabled above.
Click on Train >> Publish.
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
- },
- }
q=Bye, Output: top scoring intent – Greet.Farewell
- {
- "query": "bye",
- "topScoringIntent": {
- "intent": "Greet.Farewell",
- "score": 0.455509841
- },
- }
q=Search Amit, Output: top scoring intent – Search.People, entity – Amit, type – Person.Name
- {
- "query": "Search Amit",
- "topScoringIntent": {
- "intent": "Search.People",
- "score": 0.500412941
- },
- }
Perfect! Our app is able to extract right intent and entity. Now, we will setup bot application.
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!