First, an important note. This content is for Bot Builder SDK v4 which is, at the time of writing this, in pre-release.

In preparing for some upcoming presentations on Creating Bots, I’m upgrading a bot from Bot Builder SDK v3 to v4. It’s new so there isn’t a ton of documentation or samples out there. But if you are building bots for Azure, now is the time to start learning about v4. It’s quite different than the predecessor.

With the old version, you could use the FormFlow feature to easily create “forms” that would gather information needed by the bot to accomplish a task. Consider that if a bot is taking a pizza order, it would need to ask the customer what kind of pizza, what size, find out about side orders and drinks, etc. We had access to the FormBuilder to easily compose a set of prompts to gather the data. Well, in v4, the FormFlow is gone. That’s ok, the new Bot Builder Framework is better Smile.

Again, I am still learning about the new version of the framework and I won’t begin to explain it all here. I found most of the docs very helpful and it was pretty easy to get going. And of course, you can see the source code for the framework on github. However, when it came to providing choices for the user, I couldn’t quite figure it out. I reached out for some help (being a Microsoft MVP has its privileges) and Gary Pretty came to the rescue, pointing me to a sample here. While that sample was helpful, I’ll provide more of an explanation.

So, here is the part about how it works…

The full solution is on github, check it out if you want.

The replacement for the FormFlow is to use a DialogContainer with a list of WaterfallSteps. You can then add a waterfall step for each question you want the user to answer. The questions are asked with the help of a prompt. There are prompts to collect text, numbers, dates, and more. I want to prompt the user to pick an item from a list, so I use the ChoicePrompt. In the constructor of the DialogContainer I have created a prompt:

    var auto = new ChoicePrompt(Culture.English) { Style = ListStyle.Auto };
    Dialogs.Add("auto", auto);

I’ve set the ListStyle to Auto. I’ll get into more detail on that later.

In my constructor I have a list of choices (List<Choice>) and ChoicePromptOptions which wrap the choices and a few other properties including the RetryPromptString.

    var choices = new List<Choice>();
    choices.Add(new Choice { Value = "Plain Pizza", Synonyms = new List<string> { "plain" } });
    choices.Add(new Choice { Value = "Pizza with Pepperoni", Synonyms = new List<string> { "4 Day", "workshop", "full" } });
    choices.Add(new Choice { Value = "Pizza with Mushrooms", Synonyms = new List<string> { "mushroom", "mushrooms", "shrooms" } });
    choices.Add(new Choice { Value = "Pizza with Peppers, Mushrooms and Brocolli", Synonyms = new List<string> { "vegtable", "veggie" } });
    choices.Add(new Choice { Value = "Pizza with Anchovies" });

    _choicePromptOptions = new ChoicePromptOptions { Choices = choices, RetryPromptString = "Sorry, that isn't on the list. Please pick again."};

I also created a WaterFallStep which is really just a method that looks like this:

    private async Task AutoPrompt(DialogContext dc, IDictionary<string, object> args, SkipStepFunction next)
    {
        await dc.Prompt("auto", "What kind of Pizza would you like?", _choicePromptOptions);
    }

Note that here I am telling the DialogContext to show the ChoicePrompt (referring to it by name “auto”), I supply text (“What kind of pizza…”) and the ChoicePromptOptions. This means I can reuse the prompt by providing different text and choices if I want to.

Back in my constructor, I add that waterfall step to my Dialogs collection:

    Dialogs.Add(Id, new WaterfallStep[]
    {
        AutoPrompt
    });

Check it out, it works! Here are some screen shots from the emulator:

image.

 

More Options

While we are here, let’s get into some other settings.

First there are the list style options:

image

 

Prompt Style Samples

I used Auto before, you can see the result above.

Here is a sample using Inline

image

List looks a lot like auto right? (I think Auto will change depending on the channel for your bot. List is just a list.

image

As the name suggests, None doesn’t provide any prompts at all.

image

Lastly, here is SuggestedAction. It provides a list of buttons for the user to click. Depending on your situation this can be a great option.

image

 

Also the RetryPromptString allows you to provide text to display if the user enters a value that doesn’t match up to one of the provided options. Note that if you don’t supply a RetryPromptString and in response to the pizza question, a user enters “hot dog”, the bot won’t react well. In my full sample code I have multiple WaterfallSteps and if a user enters an incorrect response, the bot resets the steps and starts from the beginning. Including RetryPromptString let’s the bot ask again and everything works out.

There are other ChoiceOptions for a ChoicePrompt. You can specify if the numbers should be displayed alongside the list items and customize character used to separate the items in the list such as using a comma or pipe.

image

User Input

The cool part about this is that regardless of which option you use, the user can respond in a variety of ways.

  • For any of these options, the user can simply enter a “3” to indicate “Pizza with Mushrooms”.
  • Users can also input “Pizza with Mushrooms” to select that option.
  • User can also input “mushroom” and get the right selection. That’s because I provided Synonyms for most of the choices. This is a really cool feature.
  • Interestingly, using a number, the text or a synonym all work for the None prompt as well.
  • And Of course, for the SuggestedAction type, users can click the button.

If you want to see all of the code, you can get it on github. I’ve created a sample that includes all of the prompts.

 

 

 

OK, I’m a little late at getting to this but I have just posted the code for my recent talk: Creating Awesome Chat Bots with the Bot Framework and C#.

To all that attended, thanks for joining me. I had a lot of fun preparing and presenting.

The code is here: https://github.com/schwammy/bot-demo

Unfortunately, this isn’t the easiest sample to get running. I think the code serves as a good example of some great things you can do with bots. However, if you want to actually use it, there are several steps that need to be done in advance. I’ve copied the text below from the readme file. As I say in a lot of my presentations, each step is pretty easy. However, putting them all together, especially for the first time, can be tricky. There are lots of good articles and videos on the web already for getting started with Bot Framework (and LUIS and QnA). I suggest reading up a bit and then follow my very basic instructions to get the code sample running.

Contact me if you have any questions or issues. Have Fun!

Getting Started

Before using this code you need to get set up

  1. Follow the instructions in the Prerequisites section here: https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-quickstart
  2. Install the emulator. The link is on the same page as above in the section: “Test your bot”

  3. You will also need:

Resources Setup

  1. After you create a LUIS account, create a LUIS app. You can leave it blank if you want. TechBashBot.sln contains a file LuisModel.json that can be imported to get started quickly.

  2. After you create a QnA Maker account, create a QnA service. You can import the questionsn and answers from TechBashBot.sln using the QnAMaker.tsv file.
  3. After you create your Azure Account, create a Web App Bot. Just add a resource and search “bot”, then choose Web App Bot

App Configuration

Once all of your resources are set up, you need to configure the code:

  1. Update the web.config file with the MicrosoftAppId and MicrosoftAppPassword for your new Web App Bot

  2. Update LuisDialog.cs by setting the new LUIS model id and subscription key
  3. Update QnADialog.cs by setting the QnA Service subscription key and knowledgebase id

The Microsoft Bot Framework makes it pretty easy to get started creating Chat Bots. If you haven’t gotten started yet, I recommend checking out this site: Bot Framework.

For a recent Bot that I created, we had the need for the Bot to expand. What I mean is, I want my bot chat UI to start out collapsed like a search box but then expand once a user starts talking to my bot.

7F9021F8-69F8-40A0-A26B-8EEC0CD32A1B

 

There are lots of examples for getting started with the Bot Framework. For this post, I will assume you already know how to do that. Hopefully you already know how to hook up the Web Chat control to communicate with your bot – here are some details about that.

I’ll start with the client-side code for this feature

For this feature, we will utilize the Web Chat’s backchannel using the DirectLine connection to the Bot. Then we can respond to events sent to the Web Chat from the bot. Here is the JavaScript needed to do this.

First, create a connection to the Bot with DirectLine:

        var directLine = new BotChat.DirectLine({ secret: &quot;YOUR KEY GOES HERE&quot; })

Next, subscribe to the event. All I am doing is listening for the event named “init” and when it occurs, add a class “fullSize” to the HTML element that hosts the bot.

        directLine.activity$
            .filter(isInitEvent)
            .subscribe(changeSize);

        function isInitEvent(activity) {
            return activity.type === &quot;event&quot; &amp;&amp; activity.name === &quot;init&quot;;
        }

        function changeSize(activity) {
            console.log(&quot;here&quot;)
            var container = document.getElementById(&quot;bot-chat-container&quot;);
            container.classList.add(&quot;fullSize&quot;);
        }

Lastly, create the Web Chat:

        BotChat.App({
            botConnection: this.directLine,
            user: { id: &#39;user&#39; },
            bot: { id: &#39;bot&#39; },
        }, document.getElementById(&quot;bot-chat-container&quot;));

I’m just using a little CSS to hide and show the rest of the Web Chat UI. Feel free to enhance this part a little, it could be better. Hopefully you get the idea.

        #bot-chat-container {
            border: 1px solid #333;
            height: 50px;
        }

        #bot-chat-container.fullSize {
            height: 300px;
        }

        .wc-header {
            display: none;
        }

        .fullSize .wc-header {
            display: block;
        }

        .wc-console svg {
            fill: black;
            margin: 11px;
        }

        /* These styles are used to hide the upload button...*/

        .wc-console label {
            display: none;
        }

        .wc-console .wc-textbox {
            left: 10px;
        }

All that is left to show of the UI is this DIV. But this isn’t too exciting:

<div id="bot-chat-container" />

Here is the Server-Side Code

This whole thing is based on an event coming to the Web Chat UI from the Bot on the server. I’m using C# and it is pretty simple stuff. When you create a Bot, the MessagesController is stubbed out for you to handle various Activity Types. In this case, I am concerned with ActivityType.ConversationUpdate. Check to see if the a new member is added to the conversation and if so, send event named “init”.

private async Task&lt;Activity&gt; HandleSystemMessage(Activity message)
        {
            if (message.Type == ActivityTypes.DeleteUserData)
            {
		// ...
            }
            else if (message.Type == ActivityTypes.ConversationUpdate)
            {
                using (var scope = Microsoft.Bot.Builder.Dialogs.Internals.DialogModule.BeginLifetimeScope(Conversation.Container, message))
                {
                    var client = scope.Resolve&lt;IConnectorClient&gt;();
                    if (message.MembersAdded.Any())
                    {
                        foreach (var newMember in message.MembersAdded)
                        {
                            if (newMember.Id != message.Recipient.Id)
                            {
                                var reply = message.CreateReply();
                                reply.Type = ActivityTypes.Event;
                                reply.Name = &quot;init&quot;;
                                await client.Conversations.ReplyToActivityAsync(reply);

                            }
                        }
                    }
                }
            }
		// ... etc. etc.

That’s all.