Articles → MICROSOFT BOT FRAMEWORK → Waterfall Model In Microsoft Bot Framework

Waterfall Model In Microsoft Bot Framework






What Is A Waterfall Model?





Implementation




  1. Create a new class "RootDialog" that implements "ComponentDialog".
  2. public class RootDialog : ComponentDialog{
    }


  3. In "Startup.cs", add the following code.
  4. services.AddSingleton<RootDialog>();
    services.AddTransient<IBot, Karan_EchoBot.Bots.EchoBot<RootDialog>>();


  5. Change the definition of “EchoBot”.
  6. public class EchoBot < T >: ActivityHandler where T: Microsoft.Bot.Builder.Dialogs.Dialog {
      private IConfiguration _configuration;
      private UserState _userState;
      private ConversationState _conversationState;
    
      private Microsoft.Bot.Builder.Dialogs.Dialog _dialog;
    
      private IStatePropertyAccessor < UserData > _userData;
      private IStatePropertyAccessor < ConversationData > _conversationData;
    
      public EchoBot(IConfiguration configuration, UserState userState, ConversationState conversationState, T dialog) {
        _configuration = configuration;
        _userState = userState;
        _conversationState = conversationState;
    
        this._dialog = dialog;
    
        _userData = userState.CreateProperty < UserData > ("UserData");
        _conversationData = conversationState.CreateProperty < ConversationData > ("ConversationData");
      }
    }


  7. In the "OnMessageActivityAsync" method, call the "RootDialog" class.
  8. protected override async Task OnMessageActivityAsync(ITurnContext < IMessageActivity > turnContext, CancellationToken cancellationToken) {
      await _dialog.RunAsync(turnContext, _conversationState.CreateProperty <DialogState> ("DialogState"), cancellationToken);
    }


  9. Implement a waterfall model in "RootDialog.cs".
  10. using Microsoft.Bot.Builder;
    using Microsoft.Bot.Builder.Dialogs;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Karan_EchoBot.Dialog {
      public class RootDialog: ComponentDialog {
        public RootDialog(): base(nameof(RootDialog)) {
          var steps = new WaterfallStep[] {
            FirstStep,
            SecondStep
          };
    
          AddDialog(new WaterfallDialog("WaterFallId", steps));
        }
    
        private async Task < DialogTurnResult > SecondStep(WaterfallStepContext stepContext, CancellationToken cancellationToken) {
          await stepContext.Context.SendActivityAsync(MessageFactory.Text("This is the second message"), cancellationToken);
          return await stepContext.EndDialogAsync(null);
        }
    
        private async Task < DialogTurnResult > FirstStep(WaterfallStepContext stepContext, CancellationToken cancellationToken) {
          await stepContext.Context.SendActivityAsync(MessageFactory.Text("This is the first message"), cancellationToken);
          return Microsoft.Bot.Builder.Dialogs.Dialog.EndOfTurn;
        }
      }
    }

Output


Picture showing the output of waterfall model in microsoft bot framework

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Sunday, December 27, 2020

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250