Introduction
Any business that does online transactions needs to provide some kind of chat support for the issues related to their business. This can be done by developing chat applications where the consumers directly chat with the agent of the business who then solves their problems. A new alternative to answering frequently asked questions is the use of the Chat Bot which is intelligent enough to understand the questions raised by the user and determine the solutions to them on a real-time basis. Or in the case of an administrator, if they are managing a lot of operative tasks or a lot of source code bases, then an intelligent bot is of great help.
For the bot to become intelligent, the important step is that the bot should understand the language in which humans communicate with the bot (e.g English, French, Hindi etc.). To facilitate this understanding Microsoft has provided developers with a powerful feature called as Language Understanding (LUIS). This feature can be used to understand the language by comparing the uttered sentence against the intents and the entities defined while creating the LUIS application.
When the bot is being developed, the LUIS app can be consumed from the Azure functions using the Bot Builder Framework SDK. This Azure Function Bot can then be coded to perform various function after detecting the intents behind the user commands.
What are Azure Functions?
As per Microsoft,
"Azure Functions is an event-driven, compute-on-demand experience that extends the existing Azure application platform with capabilities to implement code triggered by events occurring in Azure or third party service as well as on-premises systems. Azure Functions allows developers to take action by connecting to data sources or messaging solutions thus making it easy to process and react to events. Developers can leverage Azure Functions to build HTTP-based API endpoints accessible by a wide range of applications, mobile and IoT devices. Azure Functions is scale-based and on-demand, so you pay only for the resources you consume."
What is LUIS?
As per Microsoft,
"Language Understanding (LUIS) allows your application to understand what a person wants in their own words. LUIS uses machine learning to allow developers to build applications that can receive user input in natural language and extract meaning from it. A client application that converses with the user can pass user input to a LUIS app and receive relevant, detailed information back."
Key Concepts In LUIS
- Utterance
This is the actual command spoken/written by the User /APP - Intents
Intents describe the actual intention behind the command given by the user.; e.g. when the user says, "Find me all the Pushes made on the PublicWorks Repository", what the user wants is the list of the Pushes made on the PublicWorks repository, hence Push made is the intent here. - Entity
This is extra information that is present in the utterance which helps LUIS to understand the utterance more. In above, PublicWorks is the name of the repository and hence it is the entity which gives the name of the repository on which the Push is to be queried.
Scope
This article discusses the use of the .NET Bot Builder SDK along with the Azure function to create an Azure function bot which detects the user's intent and brings back the list of all the repositories from the GIT for that particular user.
Design
The design for the bot can be broken down into the following steps.
- Creating a LUIS application To Detect the Intents and Entities in the user utterance.
- Creating LUIS cognitive services account.
- Publishing the LUIS app.
- Creating an Azure function app to take appropriate actions based upon the user intents
- Registering the bot with various channels.
Creating a LUIS Application
The main reason behind creating a LUIS application is to make the bot intelligent so that it can understand what the user wants and then reply back accordingly. The LUIS application will help the bot to understand the intent from the statements where the user greets the bot to the statements where the user wants the bot to fetch the list of the repositories etc. Each of the Intents created should have as many different variations of the utterances so that when the LUIS application is trained, it will be more intelligent. This training process should be undertaken frequently to keep the bot evolving.
The following steps will highlight how to create the LUIS application.
Note
For each of the created intents, utterances should be provided to train the app. A minimum of five should be provided but the more the merrier.
- Log On to the LUIS portal at https://www.luis.ai
- Create a LUIS application named GitAssistantBotLuisApp. Refer to the sample screenshot below.

- Create an intent called WelcomeIntent as shown below. This intent will detect if the user is greeting the bot.

- Create an intent called as HelpIntent as per the following screenshot. This intent will detect if the user requires help from bot.

- Create an intent called as SupportedFunctionsIntent as shown below. This intent will detect if the user wants to know the functions supported by the bot.

- Create an intent called as ListRepositoryIntent. This intent will detect if the user wants to know the list of all the repositories under the account.

- Create an intent called as the ActionOnRepositoryIntent . This intent will detect if the user wants to perform any action on any particular repository.

- Create an Entity called as ActionEntity. This entity is used to record the action detected in the user utterance. If user says "Bring me all the commits on AdventureWorks repository" the commits is detected as ActionEntity as it denotes the action that needs to be performed against the repository.

- Create an Entity called RepositoryEntity. This entity is used to record the action detected in the user utterance. If user says "Bring me all the commits on AdventureWorks repository" the AdventureWorks is detected as RepositoryEntity as it denotes the action that needs to be performed against the repository.

- Map the entities created above to the utterances added to the ActionOnRepositoryIntent . Refer to the sample screenshot.

- Train the App using the Train button in the portal.
Creating a LUIS Cognitive Service Account
In order to consume the LUIS app from the logic apps, the LUIS app must be published to the Resource group so that logic app connectors can use it, in order to do so, a LUIS cognitive services account need to be set up in Azure. Refer to the following steps to set up the account.
- Select the Cognitive Services blade.

- Create the Account.

Publishing the LUIS Application
Once an account for LUIS is created, the publication of the app becomes very easy. Navigate to the Publish Tab in the LUIS portal and select the environment (Production/Staging) and the time zone.


Once done, click on the publish button, this will publish the LUIS app into the LUIS account created earlier.
The LUIS app is now available to be consumed by the Azure Function App.
Creating Azure Function
Azure function can be created from the Azure portal and the code for the function app can be editied in the online editor itself.
Following are the steps that are to be undertaken to create the Azure function app bot.
- Log on to the Azure portal at https://www.portal.azure.com
- Select the FunctionApp Bot as shown below. It is available under the AI + Cognitive Services tab.

- Create the Bot by providing details asked. Refer a sample below.

- Once the bot is deployed, open the bot in Azure functions as shown in reference below.

Function App Code
The function App project for the bot contains following C# script files.
- run.csx
This file is the main driving file for the azure function app and contains the code for accepting and replying back with the messages, responds to the user. - BasicLuisDialog.csx
This file has the logic to select the messages based upon the intent detected. - GitHelper.csx
This class contains the helper methods which can be used to consume the GitHub api. In this example the helper contains only the method to get the repositories related to the account (based upon a standrard user access token).
The reader should get familar with the concepts of bot and the classes and the Interfaces used to understand the code posted below. The user is urged to refer to the See Also section to get the idea about the classes and interfaces and then return to the code.
run.csx
- #r "Newtonsoft.Json"#
- load "BasicLuisDialog.csx"
- using System;
- using System.Net;
- using System.Threading;
- using Newtonsoft.Json;
- using Microsoft.Bot.Builder.Azure;
- using Microsoft.Bot.Builder.Dialogs;
- using Microsoft.Bot.Connector;
- public static async Task < object > Run(HttpRequestMessage req, TraceWriter log) {
- log.Info($ "Webhook was triggered!");
- // Initialize the azure bot
- using(BotService.Initialize()) {
- // Deserialize the incoming activity
- string jsonContent = await req.Content.ReadAsStringAsync();
- var activity = JsonConvert.DeserializeObject < Activity > (jsonContent);
- // authenticate incoming request and add activity.ServiceUrl to MicrosoftAppCredentials.TrustedHostNames
- // if request is authenticated
- if (!await BotService.Authenticator.TryAuthenticateAsync(req, new [] {
- activity
- }, CancellationToken.None)) {
- return BotAuthenticator.GenerateUnauthorizedResponse(req);
- }
- if (activity != null) {
- // one of these will have an interface and process it
- switch (activity.GetActivityType()) {
- case ActivityTypes.Message:
- await Conversation.SendAsync(activity, () => newBasicLuisDialog());
- break;
- case ActivityTypes.ConversationUpdate:
- var client = new ConnectorClient(new Uri(activity.ServiceUrl));
- IConversationUpdateActivity update = activity;
- if (update.MembersAdded.Any()) {
- var reply = activity.CreateReply();
- var newMembers = update.MembersAdded ? .Where(t => t.Id != activity.Recipient.Id);
- foreach(var newMember in newMembers) {
- reply.Text = "Welcome";
- if (!string.IsNullOrEmpty(newMember.Name)) {
- reply.Text += $ " {newMember.Name}";
- }
- reply.Text += "!";
- await client.Conversations.ReplyToActivityAsync(reply);
- }
- }
- break;
- case ActivityTypes.ContactRelationUpdate:
- case ActivityTypes.Typing:
- case ActivityTypes.DeleteUserData:
- case ActivityTypes.Ping:
- default:
- log.Error($ "Unknown activity type ignored: {activity.GetActivityType()}");
- break;
- }
- }
- return req.CreateResponse(HttpStatusCode.Accepted);
- }
- }
The above code is auto generated when the bot gets created.
BasicLuisDialog.csx
- #load "GitHelper.csx"
- using System;
- using System.Configuration;
- using System.Threading.Tasks;
- using Microsoft.Bot.Builder.Azure;
- using Microsoft.Bot.Builder.Dialogs;
- using Microsoft.Bot.Builder.Luis;
- using Microsoft.Bot.Builder.Luis.Models;
- // For more information about this template visit http://aka.ms/azurebots-csharp-luis
- [Serializable]
- public class BasicLuisDialog: LuisDialog < object > {
- public BasicLuisDialog(): base(new LuisService(new LuisModelAttribute(ConfigurationManager.AppSettings["LuisAppId"], ConfigurationManager.AppSettings["LuisAPIKey"], domain: ConfigurationManager.AppSettings["LuisAPIHostName"]))) {}
- [LuisIntent("None")]
- public async Task NoneIntent(IDialogContext context, LuisResult result) {
- await this.ShowLuisResult(context, result);
- }
- // Go to https://luis.ai and create a new intent, then train/publish your luis app.
- // Finally replace "Greeting" with the name of your newly created intent in the following handler
- [LuisIntent("WelcomeIntent")]
- public async Task WelcomeIntent(IDialogContext context, LuisResult result) {
- await this.ShowLuisResult(context, result);
- }
- [LuisIntent("HelpIntent")]
- public async Task HelpIntent(IDialogContext context, LuisResult result) {
- await this.ShowLuisResult(context, result);
- }
- [LuisIntent("SupportedFunctionsIntent")]
- public async Task SupportedFunctionsIntent(IDialogContext context, LuisResult result) {
- await this.ShowLuisResult(context, result);
- }
- [LuisIntent("ActionOnRepositoryIntent")]
- public async Task ActionOnRepositoryIntent(IDialogContext context, LuisResult result) {
- await this.ShowLuisResult(context, result);
- }
- [LuisIntent("ListRepositoryIntent")]
- public async Task ListRepositoryIntent(IDialogContext context, LuisResult result) {
- await this.ShowLuisResult(context, result);
- }
- private async Task ShowLuisResult(IDialogContext context, LuisResult result) {
- string detectedIntent = result.Intents[0].Intent;
- string messageToUser = string.Empty;
- switch (detectedIntent) {
- case "WelcomeIntent":
- messageToUser = $ "Hola Amigo, I am chat bot of the Wonderland account.";
- break;
- case "HelpIntent":
- messageToUser = $ "Fear Not!! I can help you with activties like getting repositories in wonderland account. \n I can get the list of commit on a particular reposotory too!";
- break;
- case "ListRepositoryIntent":
- string repositoryList = await GetRepositories();
- messageToUser = $ "I will now bring you the list of the repositories in Wonderland. \n" + repositoryList;
- break;
- case "ActionOnRepositoryIntent":
- messageToUser = $ "I will bring you the {result.Entities[0].Entity} for the respository {result.Entities[1].Entity}";
- break;
- case "SupportedFunctionsIntent":
- messageToUser = $ "I can perform following operations. \n 1. Get all the repositories in WOnderland \n 2. Get the list of commits, pulls on any particular repository present in Wonderland";
- break;
- case "None":
- messageToUser = $ "I detected no intent in your command";
- break;
- }
- await context.PostAsync(messageToUser);
- context.Wait(MessageReceived);
- }
- }







Join the conversation! Your thoughts help the community grow.