Pass variable to dialog in Microsoft BotBuilder C# .NET Core

Viewed 885

I'm trying to build my bot using this example.

The flow now:

  1. User writes something foo.
  2. Bot enters dialog...

I'm stuck on getting user's first message foo inside the dialog.

1 Answers

You can get the user input from the turnContext like this:

string userInput = turnContext.Context.Activity.Text

Example:

 public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
 {
     string userInput = turnContext.Activity.Text;
 }

As for passing a variable to UserProfileDialog, you can do it in the following way:

await innerDc.BeginDialogAsync(nameof(DialogFlowDialog), userInput );

BeginDialogAsync accepts an optional argument (Object) to pass to the dialog being started.

In your UserProfileDialog, you can get that argument from the stepContext

Example:

 private static async Task<DialogTurnResult> TransportStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            string userInput = string.Empty;

            // Options contains the information the waterfall dialog was called with
            if (stepContext.Options != null)
            {
                userInput = stepContext.Options.ToString();
            }
        }

If you want to get the first message the user sends, you can always get it from the context, if you are using the 05.multi-turn-prompt you can get it this way inside your UserProfileDialog

   private static async Task<DialogTurnResult> TransportStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            // this contains the text message the user sent
            string userInput = stepContext.Context.Activity.Text;
            // WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
            // Running a prompt here means the next WaterfallStep will be run when the users response is received.
            return await stepContext.PromptAsync(nameof(ChoicePrompt),
                new PromptOptions
                {
                    Prompt = MessageFactory.Text("Please enter your mode of transport."),
                    Choices = ChoiceFactory.ToChoices(new List<string> { "Car", "Bus", "Bicycle" }),
                }, cancellationToken);
        }

or like this inside your DialogBot

  public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {

            // this contains the text message the user sent
            string userInput = turnContext.Activity.Text;

            await base.OnTurnAsync(turnContext, cancellationToken);

            // Save any state changes that might have occured during the turn.
            await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
            await UserState.SaveChangesAsync(turnContext, false, cancellationToken);
        }
Related