Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel
Ezequiel Jadib 6b3df2c0b3
Update documentation links to point to new docs (#118)
7 years ago
..
0a0ac96754
Adding samples related to documentation
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
6b3df2c0b3
Update documentation links to point to new docs (#118)
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
1cac51aaa5
Merge pull request #119
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
0a0ac96754
Adding samples related to documentation
7 years ago
1cac51aaa5
Merge pull request #119
7 years ago

README.md

You have to be logged in to leave a comment. Sign In

Simple Task Automation Bot Sample

A sample bot showing how to do simple task automation scenarios.

Prerequisites

The minimum prerequisites to run this sample are:

  • The latest update of Visual Studio 2015. You can download the community version here for free.
  • The Bot Framework Emulator. To install the Bot Framework Emulator, download it from here. Please refer to this documentation article to know more about the Bot Framework Emulator.

Code Highlights

Dialogs model a conversational process, where the exchange of messages between bot and user is the primary channel for interaction with the outside world. Dialogs can be composed with other dialogs to maximize reuse, and a dialog context maintains a stack of dialogs active in the conversation.

The RootDialog class, which represents our conversation, is wired into the MessageController.Post() method. Check out the MessagesController class passing a delegate to the Conversation.SendAsync() method that will be used to construct a RootDialog and execute the dialog's StartAsync() method.

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
    if (activity.Type == ActivityTypes.Message)
    {
        await Conversation.SendAsync(activity, () => new RootDialog());
    }
    else
    {
        this.HandleSystemMessage(activity);
    }

    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

In the StartAsync() method, we are telling the bot to wait for a message from the user and call the MessageReceivedAsync resume method when the message ins received.

public async Task StartAsync(IDialogContext context)
{
    context.Wait(this.MessageReceivedAsync);
}

The Bot Framework comes with a number of built-in prompts encapsulated in the PromptDialog class, than can be used to collect input from a user. Check out the RootDialog class, in the MessageReceivedAsync method the usage of the PromptChoice dialog to asks the user to pick up an option from a list.

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
    PromptDialog.Choice(
        context, 
        this.AfterChoiceSelected, 
        new[] { ChangePasswordOption, ResetPasswordOption }, 
        "What do you want to do today?", 
        "I am sorry but I didn't understand that. I need you to select one of the options below",
        attempts: 2);
}

Once the user picks an option the PromptChoice dialog ends and return the result to the parent dialog (in this case the RootDialog) by calling to the ResumeAfter<T> delegate passed when calling to the child dialog. The IDialogContext.Call() method can be used to Call a child dialog and add it to the top of the stack transferring control to the new dialog.

Check out the AfterChoiceSelected resume method retrieving the user selection and the usage of of context.Call() to give control of the conversation to a new dialog depending on the selected option.

private async Task AfterChoiceSelected(IDialogContext context, IAwaitable<string> result)
{
    try
    {
        var selection = await result;

        switch (selection)
        {
            case ChangePasswordOption:
                await context.PostAsync("This functionality is not yet implemented! Try resetting your password.");
                await this.StartAsync(context);
                break;

            case ResetPasswordOption:
                context.Call(new ResetPasswordDialog(), this.AfterResetPassword);
                break;
        }
    }
    catch (TooManyAttemptsException)
    {
        await this.StartAsync(context);
    }
}

The ResetPasswordDialog uses a set of custom Prompts dialogs (classes inheriting from Prompt<T,U>) to ask the user for her phone number and her date of birth and validate their input. Prompts implement a retry mechanish and after X attemps they throw a TooManyAttempsException. Dialog exceptions can be handled in the ResumeAfter<T> delegate passed to the Call method

Once the child dialog finishes the IDialogContext.Done() should be called to complete the current dialog and return a result to the parent dialog.

The sample shows how to handle dialog exceptions by awaiting the result argument within a try/catch block and how the ResetPasswordDialog uses context.Done() to return if the reset operation was successful or not to the parent dialog

private async Task AfterDateOfBirthEntered(IDialogContext context, IAwaitable<DateTime> result)
{
    try
    {
        var dateOfBirth = await result;

        if (dateOfBirth != DateTime.MinValue)
        {
            await context.PostAsync($"The date of birth you provided is: {dateOfBirth.ToShortDateString()}");

            // Add your custom reset password logic here.
            var newPassword = Guid.NewGuid().ToString().Replace("-", string.Empty);

            await context.PostAsync($"Thanks! Your new password is _{newPassword}_");

            context.Done(true);
        }
        else
        {
            context.Done(false);
        }
    }
    catch (TooManyAttemptsException)
    {
        context.Done(false);
    }
}

Outcome

You will see the following result in the Bot Framework Emulator when opening and running the sample solution.

Sample Outcome

More Information

To get more information about how to get started in Bot Builder for .NET and Conversations please review the following resources:

Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...