Chat bots with the Microsoft Bot Framework

Chat bots are all the rage these days.

As someone who wrote one or two bots back in the day of IRC (mIRC scripts to be specific), this is a welcome renaissance. I'm planning to write a series on these bots (particularly because I haven't found a way to cover everything I feel you need to understand to be productive - and have fun! - in one post).

Bot framework

In Microsoft land, a chat bot is an ASP.NET Web API Controller which receives a JSON payload, acts on it and returns a JSON payload. The payloads in question are received from - and sent back to - Microsoft Bot Connector. The connector is responsible for talking to the channels where your bot will live. Example channels includeSlackSkypeFacebook Messenger, the Web (as a embeddable control).

An example chat bot API controller, might look something like this:

namespace Chatbot
{
    [BotAuthentication]
    public class MessagesController : ApiController
    {
        /// 
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// 
        public async Task Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message || activity.Type == ActivityTypes.ConversationUpdate)
            {
                await Conversation.SendAsync(activity, CustomerSupportExample.MakeRootDialog);
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }
    }
}

Essentially, the controller receives a JSON encoded Activity and passes that into the chatbot framework, which in the above case manages a dialog.

Cognitive services

To give the bots smarts, Microsoft offers a number of cognitive services. Using these, you can have your bot understand text in different languages, do sentiment analysis (is the feedback positive or negative - should we respond with a 'Great!' or "I'm sorry"?) and classify user input:
Bot: Let's talk about your eating habits
Human: I prefer greens
Bot: Ah, you are a vegetable fiend!

Getting started

Getting started with the Microsoft BOT Framework can be quite a challenge, however, so let's see what we can do to smoothen your ride.

Follow this guide to get a basic bot up and running that will respond with how many characters you've typed. Not super smart, but it's a start.

Tip! Configure your project not to open a browser upon every F5



Tip! Configure your bot emulator



You will want the local port not to be already taken by anything else (e.g. another IISExpress instance, a debugging tool (such as GapDebug) etc.)

You want to point the Bot URL to /api/messages in order for it to hit your MessagesController.

Finally, your Microsoft App Id and Password should match those in your web.config (but you already set up that in the above guide, right?).

Form flow

Once you have your bot up and running, I strongly recommending looking at the FormFlow offering.
With FormFlow, you set up pre-defined processes (or guided conversations) that you would like your human to navigate through, e.g.

Bot: Hello and welcome to Customer Support. I'm Botty. What is your name?
Human: Hi, my name is George.
Bot: Great to meet you, George! How can I help you today? You can ask me to  or to  amongst other things.

LUIS

With FormFlow under your belt, you can sprinkle some language understanding onto your bot, allowing it to not only understand the options you are presenting, but also understand more flexible language, such as 'Well, you know - I am hungry!'. Read more about it here.

More cognitive services

Finally, in this series, we'll look at hooking in translation and sentiment analysis. Read all about it here at a later date!

Is there anything special you'd like me to address with regards to the Microsoft BOT Framework, or bots in general? Let me know in the comments!

Comments

Popular posts from this blog

Auto Mapper and Record Types - will they blend?

Unit testing your Azure functions - part 2: Queues and Blobs

Testing WCF services with user credentials and binary endpoints