Bots

TL;DR

Developing with LUIS is easy but slightly tedious. This post shares some tips for developing with LUIS and provides links to get started.

In part 1 I talked about my experience getting started with the Microsoft Bot Framework and getting responses from simple keywords. Which is great; but I want users to be able to have a conversation with my Bot, ALIE (another The 100 reference). In order to get my bot to understand natural language I used Microsoft’s LUIS, part of their cognitive services suite, and integrated it into my bot.

LUIS

LUIS (Language Understanding Intelligent Service) is a service in Microsoft’s extensive cognitve services suite. It provides extensive models from bing and cortana for developers to use in their application. LUIS also allows developers to create their own models and creates http endpoints that can be pinged to return simple JSON responses. Bellow is a Tutorial about how to use LUIS.
LUIS – Tutorial

Things to note

Video differences

LUIS-Features

LUIS has been updated since the release of this video so some differences:
The Model Features area on the right has been changed to reflect more of the features you can add:

Working with Intents

https://www.luis.ai/Help is a great resource. LUIS has very detailed documentation.

LUIS supports only one action per intent. Each action can include a group of parameters derived from entities. The parameter can be optional or required, LUIS assumes that an action is triggered only when all the required parameters are filled. These will be the main driving force of your bot responses, and actions come into play when publishing.

Publishing Model for Bot Framework and SLACK

Here is the link to how to publish your model: https://www.luis.ai/Help#PublishingModel.
What they neglect to mention is that when publishing for the Bot Framework or SLACK you need to be in preview mode to access those features since they are still in Beta. To get to the preview of LUIS click the button on the top right of the page, it takes about a minute to regenerate the page.

LUIS_Publish1

Publishing – Action needed

Now, this might next part might change soon since the beta keeps being improved upon. When I first wanted to publish the model with Bot Framework the service required me to make at least one of my Intents return an action.

Adding Action

In preview select an Intent. A window will popup. Select Add Action.

LUIS_AddingAction

Next check the Fulfilment Box.

The fulfillment type determines what type of response will be included in the JSON object. I’ve selected Writeline since all of the actions that I have on ALIEbot so far do not use the integrated services, like weather or time.

After you select a fulfillment type you can add parameters and an action setting (which is what will be returned in the JSON object).

In my application I returned the parameter XName which is the name of the character that was in the users response.

Integrating LUIS Code

Setting up LUIS class

Before you can receive responses from LUIS you need to import the appropriate LUIS libraries, and create a class that extends the Luis Dialog. This class must also include the serializable and Luis Model tags.

This should be LUIS cODE Model

The LUIS model ID and Key can be found when you are publishing your Application:

LUIS Intent Methods

Your class must now have methods that act upon the specific LUIS intents.

        //This needs to match the Intent Name from JSON
        [LuisIntent("XName")]
        public async Task XNameResponse(IDialogContext context, LuisResult result)
        {
            var entitiesArray = result.Entities;
            var reply = context.MakeMessage();
            foreach (var entityItem in result.Entities)
            {
                if (entityItem.Type == "Character")
                {

                    switch (entityItem.Entity)
                    {
                        case "raven":
                            reply.Text = "Raven the Best";
                            reply.Attachments = new List<Attachment>();
                            reply.Attachments.Add(new Attachment
                            {
                                Title = "Name: Raven Reyes",
                                ContentType = "image/jpeg",
                                ContentUrl = "URL_PIC_LINK",
                                Text = "It won't survive me"
                            });
                            break;
                        case "clarke":
                            reply.Text = "Clarke is the main character";
                            break;
                        default:
                            reply.Text = "I don't know this character";
                            break;
                    }
                    await context.PostAsync(reply);
                    context.Wait(MessageReceived);
                }
            }
        }

Summary

Once you have set up a LUIS model, you need to publish it. After it has been published your bot can then connect to it via the LUIS Model tag. Connecting the bot to LUIS will enable the bot to understand natural language your users will use, however you still need to code the responses with LUIS Intent tags for your Task or Post methods in the Bot Framework.

So that should be everything to get LUIS working. Remember that this content is all in Beta and is subject to change, but I’ll keep it updated as much as possible.

Happy Hacking!

-TheNapping Kat

Unity

TL;DR

What channels you’re using matters! Test out on all your desired platforms before publishing code.

Testing out the limits of the bot framework I tried to create multi-line responses for my bot. The Text property of replies was Markdown, so, I thought it should be easy enough to implement. However, I quickly realized it didn’t always look the way I wanted. Here’s some tips to getting your responses to look just right =).

These examples were all using the

reply = context.MakeMessage();

and implemented the PostAsync method since my responses are all Tasks.

  await context.PostAsync(reply);
  context.Wait(MessageRecieved). 

Multi-Line Repsonses

You must use \n\n in the string.

Input:

reply.Text = "Hi I'm one line \n\n " +
"I'm line two" +
"I'm line three?" ; 

Output Web:

Output Facebook:

NewLine-FB

Lists

In a list you must have the new line syntax \n\n as well as an ‘*’ with a space after it, be careful here since ‘*’ are also used for italicization. You can also see that the spacing is slightly different between the two channels.

Input:

reply.Text = "Hi I'm one line \n\n" +
"* Item 1 \n\n" +
"* Item 2 " ; 

Output Web:

Output Facebook:

List-FB

Block Quote with Horizontal Rule

Quoted Text must have ‘>’ with one space after to denote that the next chunk of text will be a quote. The Horizontal Rule is marked by ‘—‘. We can especially see the limitations between channels even more in this example.

Input:

  reply.Text = "Block quote below bar \n\n" +
    "---" +
    "\n\n > Something about life. I'm an existential quote \n\n" + 
    "-BOT ";

Output Web:

HRQuote-Web

Output Facebook: 

HRQuote-FB

Headers / Bold, Italics and Strike Throughs

This time – drastic differences between Facebook versus the Web. Note that with headers you must type in \n\n after the header text or the entire string will be part of the first header syntax. And typing ‘ *** ‘ will get bold italics. However Facebook does not register ANY of these.

Input:

  reply.Text = "# Don't know if I need new lines \n\n" +
     "~~You **don't** *need* new lines~~ \n\n" +
     "***yes you do***";

Output Web:

Headers-Web

Output Facebook: 

Headers-FB

Links and Pictures in an Ordered List

Some more differences with Facebook and the Web, but less so. Remember to put \n\n after every item in your list and to leave a space after the ‘.’ following the number.

Input:

  reply.Text = "### List \n\n" +
     "1. Link: [bing](http://bing.com) \n\n" +
     "2. Image Link: ![duck](http://aka.ms/Fo983c)";

Output Web:

Links-Web

Output Facebook:

Links-FB

Hope this little guide helps.

Happy Hacking!

– TheNappingKat

Bots

Microsoft released their new Bot Framework early this year at the Build Conference. So, naturally, I wanted to create my own; eventually integrating it into a game. In this post I talk about some of my learnings and what the bot framework provides.

I decided to work with the Microsoft Bot Connector, part of the Microsoft Bot Framework, as a way to get my bot up and running on the most platforms as quickly as possible. I haven’t worked with bots in the past so this was my first dive into the territory. My bot was built in C# however, Microsoft’s Bot Framework can also be built in Node.js. My colleague Sarah wrote a post about getting started with Node here: https://blogs.msdn.microsoft.com/sarahsays/2016/06/01/microsoft-bot-framework-part-1/

The bot I wanted to create was a simple chat bot that I could build upon for interactivity with users. If your familar with The 100, you’ll figure out what my bot does. All the code for what I did can be looked at here: https://github.com/KatVHarris/ALIEbot

What I used

Microsoft Bot Framework is super powerful and makes it easy to create a bot of your own. You can use any of the following to get started:

  • Bot Connector
  • Bot Builder C#
  • Bot Builder Node.js

I used the Bot Connector, an easy way to create a single back-end and then publish to a bunch of different platforms called Channels.

I started out by following the steps in the getting started section of the docs and downloaded the Bot Template for Visual Studio here: http://docs.botframework.com/downloads/#navtitle

** Note: It’s really important for Visual Studio to be updated in order to use this, as well as download the web tools in the Visual Studio Setup when you download.**

** Another Note: if you have never downloaded a template for Visual Studio before here are some instructions: http://docs.botframework.com/connector/getstarted/#getting-started-in-net. You’ll have to save the zip into the %USERPROFILE% folder on your computer. **

Set Up

Open a new project with the Bot Template, and install the nuget package for the Microsoft’s Bot Builder: install-package Microsoft.Bot.Builder

Message Controller

The file that dictates the flow of responses is the MessageController.cs in the “Controllers” folder. The class handles systems messages and allows you to control what happens when a message comes through.

Adding the following conditional statement to the Post function allows you to cater the response to the user.

Let’s create a simple response:

public async Task Post([FromBody]Message message)
{
    if (message.Type == "Message")
    {
        return message.CreateReplyMessage($"You said:{message.Text}");
    }
    else
    {
        return HandleSystemMessage(message);
    }
}

Now you can stick with this model and add in bits of functionality but I like to add a more powerful messaging system with Dialogs.

Dialogs

** Now there are slight differences between the BotConnector Dialogs for Node vs C#. Everything in this post pertains to the C# verison.**

Bot Builder uses dialogs to manage a bots conversations with a user. The great thing about dialogs is that they can be composed with other dialogs to maximize reuse, and a dialog context maintains a stack of dialogs active in the conversation.

To use dialogs all you need to do is add the [Serializable] tag and extend the IDialog<> from Microsoft.Bot.Connector;

Dialogs handle asynchronus communication with the user. Because of this the MessageController will instead use the Conversation class to create an async call to a Dialog Task that uses the context to create a ReplyMessage with more functionality. What does that all mean? It means that with dialogs, you can implement a conversation with a user asynchronously when certain keywords are triggered. For example if the user types in the keyword “reset” we can have a PromptDialog to add the confirmation. One of the most powerful ways of creating a an actual dialog with the user and the bot is to add Chain Dialogs.

Chain Dialogs

Explicit management of the stack of active dialogs is possible through IDialogStack.Call and IDialogStack.Done, explicitly composing dialogs into a larger conversation. It is also possible to implicitly manage the stack of active dialogs through the fluent Chain methods.To look at all the possible ways to respond to a user with Dialogs you can look at the EchoBot Sample in Github.

Publishing your Bot

Okay now that you have tested your bot and got it to respond to your user, how do we publish? Well steps to getting your bot on the bot connector are here: http://docs.botframework.com/directory/publishing/#navtitle

TIP 1: Update Visual Studio and tools

As I said earlier make sure all of your tools are on the latest update. My web tooling was not on the latest version when I first tried to publish my bot, so the directions were slightly different than the tutorial and caused issues later.

TIP 2: Don’t skip any of the steps

The first time I published my bot it didn’t work. I still have no idea why, but I believe it was because I missed a minor step in the creation process.

TIP 3: It should work immediately

Your bot should work immediately after you activate the web channel. If it doesn’t check your code again. My first bot was not working immediatley and I ended up just registering a new one with the same code. That worked.

TIP 4: Web disabled

If you look at my channel picture you can see that the web channel is registered but it’s status says “diabled”

Don’t worry about this. Your Bot web should still work.

TIP 5: Registering your bot

You don’t need to register your bot for it to work. Registering your bot will allow it to be in the publish gallery later. Make sure your bot does something useful before submitting as well. Simple chat bots do not count.

That’s it! You should have a bot published and all ready to Chat with.

Next Steps – LUIS

Okay so there are many ways that your bot can respond to your user. However specific keywords are needed and it’s less user friendly, and conversational than we would like. In order to make our bot respond to natural language that users will most likely be using to integrate LUIS. Which I’ll talk about in the next post with part 2.

Reading the Bot Framework Docs was extremely helpful when getting started, so if you haven’t looked at them I recommend you take a look here: http://docs.botframework.com/

The Microsoft Bot Framework is opensource so you can help contribute to the project here: Microsoft/BotBuilder. They also have more samples included in their source code.

Happy Hacking!

-TheNappingKat