Martín La Rosa
Martín La Rosa

Reputation: 810

How to send a proactive dialog with Bot Framework

I want to initiate a dialog proactively with BotBuilder in C#. Proactive message works fine, but I want to initiate a dialog. They way I would do it is with the dialogSet within my Bot class, but in this case I'm on another class executing the callback and don't have access to the dialogSet. What's the correct approach to do this?

Upvotes: 1

Views: 642

Answers (1)

Martín La Rosa
Martín La Rosa

Reputation: 810

I just found what the problem was. I solved it by:

  1. Creating a dialogset in the class where I'm handling the proactive trigger. For that I inject the DialogState accessor
  2. Added only the dialog I needed to be triggered, I'm assuming this needs to match with the one I have in the Bots dialogSet. I'll need to refactor this so that I obtain the dialog from the same place in both this class and the bot, so I don't have duplicated code.
  3. Get the dialogSet's context and begin the dialog.
  4. Very important.... Save the changes of the DialogState, if not the answer wont be handled correctly.

var _dialogSet = new DialogSet(accessors.DialogStateAccessor);

    _dialogSet.Add(new CrazyDialog("CrazyDialog"));

    DialogContext dc = await _dialogSet.CreateContextAsync(turnContext, cancellationToken);

    await dc.BeginDialogAsync("CrazyDialog", cancellationToken);

    await accessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);

Upvotes: 5

Related Questions