Bridge Communications

Saturday, November 26, 2016

Building a Bot with Microsoft Bot Framework - Part V

This week we are going to add some intelligence to our bot.  It's not reasonable to try to hard code everything a person will ask your bot.  For this we are going to tap into Microsoft LUIS to do the heavy lifting for us.  LUIS helps us do the work of understanding what the user wants from our bot.  It has a couple of building blocks that are important to understand.

Intents are a way to figuring out what the user is asking, or what they intend.  In our bot we want to provide 3 different buckets in which to put the user requests.

1.  A sales bucket for people inquiring about features, product prices, etc.
2.  A support bucket for people who are wanting help with a particular problem.
3.  A miscellaneous bucket for everything else.

As you can see we created an intent for each of those items.  This is important later when we look at the code.

Entities are values that we want to pick out of requests.  They can be things like a number, date, or string.  In our case we are going to use entities to figure out which of our 3 major products the customer is either wanting sales or support info on.  It is important to type in some phrases and begin training your bot in LUIS before throwing it out for public consumption.  Over time, LUIS will suggest intents and entities for you to group based on what users are asking, and that process allows you make it even smarter over time.

As you can see here we highlight the word that is the entity, and select the category of intent from the pull down.

The Code
So now that we have LUIS built out, let's bring it to life in our code.  First add this line to the top;

using Microsoft.Bot.Builder.Luis;

Once you have that let's make sure we connect our bot to our LUIS.  First we need a class to store the results.

 public class LUISResult
        public string query { get; set; }
        public Intent[] intents { get; set; }
        public Entity[] entities { get; set; }

    public class Intent
        public string intent { get; set; }
        public float score { get; set; }

    public class Entity
        public string entity { get; set; }
        public string type { get; set; }
        public int startIndex { get; set; }
        public int endIndex { get; set; }
        public float score { get; set; }

Our message controller will need some code like this to hook back to LUIS and return our results.

  private static async Task<LUISResult> GetEntityFromLUIS(string Query)
            Query = Uri.EscapeDataString(Query);
            LUISResult Data = new LUISResult();
            using (HttpClient client = new HttpClient())
                string RequestURI = "<yourapp>&subscription-key=<yourkey>&q=" + Query;
                HttpResponseMessage msg = await client.GetAsync(RequestURI);

                if (msg.IsSuccessStatusCode)
                    var JsonDataResponse = await msg.Content.ReadAsStringAsync();
                    Data = JsonConvert.DeserializeObject<LUISResult>(JsonDataResponse);
            return Data;

You will need to insert your app key, and your LUIS subscription key.

Once you have that in place you can start coding your message handling logic.  Here is an example of how to handle the intents first.

LUISResult StLUIS = await GetEntityFromLUIS(message);
                    if (StLUIS.intents.Count() > 0)
                        switch (StLUIS.intents[0].intent)
                            case "SalesActivity":

Then how to handle the entities.

if (StLUIS.entities[0].entity == "skype" | StLUIS.entities[0].entity == "bloc" | StLUIS.query.Contains("lync"))
                                        reply = activity.CreateReply("It looks like you are interested in Bridge Operator Console for Skype for Business. If you need a quote [Quote Skype](  You can also [email us](, or call us at [+17012124797](tel:+17012124797).");

Next week we will take a tour of bot portal and show you how to connect your bot to your website, Skype, or Microsoft Teams to name a few.

Doug Routledge, C# Lync, Skype for Business, SQL, Exchange, UC, 
Full Stack Developer  BridgeOC Bridge Operator Console
Twitter - @droutledge @ndbridge

1 comment:

  1. can you please post an article on DIRECT LINE API integerating this bot in our own UI with jquery ajax call and not with HttpClient ot WebClient.