15

I am creating a bot using Microsoft bot framework , the bot will be receiving orders for a restaurant , I want to know how can I handle multiple dialogs , like for example the customer makes the first order , then i want the bot to ask do you want something else? then the customer says yes/no , incase of yes to repeat the same dailog again with keeping the state of the first one , what I am seeing in the documentation now is only one conversation and one dialog.

Thanks a lot

Ezequiel Jadib
  • 14,767
  • 2
  • 38
  • 43
Mazen Abu Taweelih
  • 637
  • 1
  • 9
  • 24

3 Answers3

15

To manage multiple dialogs you need to use the Dialog Chains. You can either manage the stack of dialogs explicitly (using Call/Done) or implicitly using the Chain fluent methods. Here is sample of how to use it.

If the set of things that the user can select are already predefined I would then recommend using FormFlow. The Pizza & Sandwich samples are good examples of how to handle orders with a predefined set of options.

patridge
  • 26,385
  • 18
  • 89
  • 135
Ezequiel Jadib
  • 14,767
  • 2
  • 38
  • 43
  • do you have a full tutorial on chains? I am not able to get it from the framework help page – Mazen Abu Taweelih May 11 '16 at 17:46
  • I don't. You might to look to the EchoBot sample (https://github.com/Microsoft/BotBuilder/tree/master/CSharp/Samples/EchoBot) and the Facebook sample (https://github.com/Microsoft/BotBuilder/tree/master/CSharp/Samples/SimpleFacebookAuthBot) to get a better idea of how to use Chains. The anonymous method might confuse you. Think about it as a real chain. The output of the first item of the chain will be he input of the second item of the chain. Chain.PostToChain() .ContinueWith(Callback1) .PostToUser() .ContinueWith(Callback2) – Ezequiel Jadib May 11 '16 at 17:54
  • Dialog Chains appears to be a C# solution. What is the equivalent for Node.js? – Howard May 25 '16 at 20:01
  • 1
    The closer I think it's Waterfall http://docs.botframework.com/builder/node/dialogs/overview/#waterfall – Ezequiel Jadib May 26 '16 at 09:13
  • Would you please be able to explain how the Call/Done functions operate and where passed in values are sent? – blueprintchris Oct 26 '16 at 10:05
  • Check this http://stackoverflow.com/questions/37522294/calling-forms-from-dialogs/37525465#37525465 – Ezequiel Jadib Nov 15 '16 at 11:11
  • Thanks for this great answer, some or most of the links provided are now returning 404. – Gnyasha Aug 16 '20 at 16:23
1

With version V4 of Microsoft Bot Framework, FormFlow needs to be replaced by Waterfall Dialog. Here we can use stepContext.Values (dictionary) to maintain state across waterfall steps and present user with choice prompt for Yes or No response and then repeat the waterfall dialog in case of Yes response else end the dialog in last waterfall step.

Add below waterfall in constructor of your base Component dialog and repeat the waterfall as per user choice.

WaterfallStep[] myWaterfallDialog = new WaterfallStep[]
{ 
    this.waterfallStepToGetUserOrder,
    .......
    this.promptUserForChoiceStep,
    this.EndDialogStep
}
AddDialog(new WaterfallDialog("mydialog", myWaterfallDialog);
0

The above answers are good but I noticed some of the links provided are no longer working. Here is how I managed to navigate among different dialogs

    public MakeChoiceDialog() : base (nameof(MakeChoiceDialog))
    {
        AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
        AddDialog(new LoginDialog());
        AddDialog(new SignUpDialog());

        AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
            {
                LoginStepAsync,
                LoginSignUpStepAsync,
                //Other Steps here
            }));

        InitialDialogId = nameof(WaterfallDialog);
    }

The method call will be

    private async Task<DialogTurnResult> LoginSignUpStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        string userChoice = ((FoundChoice)stepContext.Result).Value;
        var msg = string.Empty;
        switch (userChoice)//You can use if statement here
        {
            case "Login":
                return await stepContext.BeginDialogAsync(nameof(LoginDialog), null, cancellationToken);
            default:                  
              return await stepContext.BeginDialogAsync(nameof(SignUpDialog), null, cancellationToken);
        }
        return await stepContext.EndDialogAsync(null, cancellationToken);
    }

In the Startup.cs add the following

services.AddSingleton<LoginDialog>();
services.AddSingleton<SignUpDialog>();
Gnyasha
  • 658
  • 11
  • 19