Migrating the code from Bot Framework V3 to V4

2020-07-18 20:46发布

问题:

I have more question while migrating the dialog from V3 to V4. below is our code. In v3, we were using

Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(conversationContext.CurrentActivity, new RootDialog());

public class RootDialog : IDialog {
    public RootDialog()
    {
      .....
    }

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

    public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
    }

In the MessageReceivedAsync, we used the context.Wait(), context.Done() and context.PostAsync(). Can you recommend how to replace in the V4? And what's the alertnative for Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync in V4?

回答1:

These APIs are all gone. Here are the explanations of their replacements in V4:

context.Wait(…)

This method was used to tell the dialog system what method to invoke next on your class when a new activity arrived and is now gone. Instead you now subclass Dialog and override several methods for various lifecycle events:

  • BeginDialogAsync - called when the dialog is first pushed on the stack by bot code or another dialog calling BeginDialogAsync on the DialogContext.
  • ContinueDialogAsync - called when a new activity comes in and the bot calls ContinueDialog on the DialogContext.
  • ResumeDialogAsync - called when another dialog on the stack has completed and a dialog that was previously on the stack is now at the top of the stack.
  • RepromptDialogAsync - called when an explicit request has been made to reprompt the user. This is basically a way to tell the dialog that nothing has changed, but that it should pick up from where it left off again by sending whatever activity it last sent.
  • EndDialogAsync - called when the dialog has indicated its done and is being popped off the stack.

context.Done()/.Fail()

This was one of the way you reported the status of your dialog, but this is now accomplished by returning a DialogTurnResult from most of the aforementioned lifecycle methods. One of the properties is named Status and is of type DialogTurnStatus which has values that indicate the current state of the dialog. For example:

  • Waiting - the dialog sent some activities and is awaiting more input and should remain at the top of the stack.
  • Complete - the dialog has completed it's work and should be ended and popped off the stack. When this state is returned, callers can also investigate the output of the dialog (if it has one) which is passed back via the DialogTurnResult::Result property.
  • Cancelled - the dialog was cancelled part of the way through its work.

context.PostAsync()/Conversation.SendAsync

These were both used to respond back to the user. Both are now replaced by calling SendActivityAsync on the ITurnContext that is accessible via the Context property of the DialogContext instance that is passed into most of the aforementioned lifecycle methods as a parameter. NOTE: a couple of the lifecycle methods actually receive an ITurnContext parameter directly and then you just use that.