Microsoft Bot Framework Part 3: Conversation

Welcome to Part 3! Join me to learn more about how to build conversational dialog options into your bot.

Conversations

Bots are fun because we can type to them in plain English. The next step that makes it cool is getting them to talk back to you with different phrases. My previous blog posts Part 1 and Part 2 explained how to build a simple "Hello World" bot that responds to any message with the same reply: "Hello World." This is a great first bot to build, but holding an extended conversation with Hello-World-Bot is about as mentally stimulating as talking to Hodor.

[caption id="attachment_475" align="aligncenter" width="542"]HodorBot Someone created a 'Game of Thrones'-inspired bot that responds 'HODOR' to anything.[/caption]

 

As fun as that sounds, you'll probably want to start to learn different conversational options. If you want to take your bot from "Hello World" to "Hello yourself! What's your name?" then keep on reading.

Dialog Handlers

When users send messages to your bot, the framework tracks which dialog is currently active, and automatically routes the incoming message to the active dialog. What does that mean? For our Hello Bot, we’ve just added a single root ‘/’ dialog that responds to any message with “Hello World” and listens for a reply, so there is only one active dialog.

According to this Overview in the docs, Bot Builder uses dialogs to manage a bot's conversations with a user. It then goes on to say, "To understand dialogs, it's easiest to think of them as the equivalent of routes for a website." (That is, unless you are unfamiliar with website routing!) "All bots will have at least one root ‘/’ dialog, just like all websites typically have at least one root ‘/’ route." (Haha, ‘root ‘/’ route’ is fun to say.) "When the framework receives a message from the user, it will be routed to this root ‘/’ dialog for processing. For many bots, this single root ‘/’ dialog is all that’s needed, but just like websites often have multiple routes, bots will often have multiple dialogs." Okay, let's break it down. In order to visually understand these Dialog Objects, it might be useful to see them drawn out.

Closure

The simplest form of a dialog is a function that will process all messages received from the user.

 bot.dialog ( '/', function (session) {session.send ( 'Hello World'); } );

This drawing turns every set of {curly braces} and (parentheses) into enclosed bubbles. I use the fancy ƒ to denote a function.
Notice how the '/' route is self-contained.

Waterfall

If you built a new route called ‘/profile’ to ask the user their name, and redirected the user to that dialog, then any of the results returned from that dialog would be passed as input to the next step of the waterfall. "Hello! What is your name?" waterfalls into "Hello, Sarah!" because the bot remembers. To create a waterfall, just pass an array of functions in your call to add() with a pair of [square brackets].

 var bot = new builder.TextBot();
bot.add('/', function (session) {
    if (!session.userData.name) {
        session.beginDialog('/profile');
    } else {
        session.send('Hello %s!', session.userData.name);
    }
});
bot.add('/profile', [
    function (session) {
        builder.Prompts.text(session, 'Hi! What is your name?');
    },
    function (session, results) {
        session.userData.name = results.response;
        session.endDialog();
    }
]);

Command Dialog

You can add a CommandDialog to listen for the user to say something specific with ".matches('^something specific')" and ".onDefault()". functions_blue

All your parentheses, curly braces, and brackets have to properly enclose each route and their functions.

Further Reading

Microsoft Bot Framework is something I've been working on for a few weeks. I recently went to a hack-a-thon that helped give me a fresh perspective from people brand new to building bots. I got to hear what roadblocks they ran into that needed to be addressed.

  • If you want to capture what text a user types to your bot, you would use session.message.text.
  • With the BotConnectorBot, you can’t use bot.listenStdin(), because that listens to the console terminal. Just use bot.listen().
  • With a Twilio trial account, other people's phone numbers have to be added to a verified list before they're able to send SMS texts to the bot you built.

Check out NoWaySheCodes.com for more info on the difference between using a TextBot and a BotConnectorBot, developing from a Macbook, and how to send texts to a OneNote notebook on the fly!

Configure Channels

Now, if your Bot is up and running, you can configure it for a communication channel like Slack or SMS. Configuring channels is a combination of Microsoft Bot Framework workflow and conversation service workflow, and is unique for each channel you wish to configure. (The steps are so easy, they blog themselves. You don’t even need me for this part!)

ConfigureChannels

Once you’ve gone through the steps here, return to the channel page on the dev portal, click the checkbox for the channel you chose (if you haven’t already), and hit “save changes”.

That’s the end of configuration - your Bot is ready for users! They will have their own steps to follow to give the Bot permission to participate in their group/channel or get connection details like the SMS phone number or e-mail. They can do this in the Bot Directory page for your Bot (if you publish to the Bot Directory). The link to this is at the top of the Bot Details page in the dev portal.

Leave a comment or tweet for me @Saelia if you have any questions or roadblocks about Microsoft Bot Framework. Happy hacking!