Search code examples
botframework

Start a Dialog after a proactive message


I'm currently using this sample: https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/16.proactive-messages and the proactive messages are working fine, but I want to start a dialog that i'm already have. I dont know how to call an specific dialog after send the proactive message

My controller which trigger the proactive message:

[Route("api/statutory")]
[ApiController]
public class StatutoryController : ControllerBase
{
    private readonly IBotFrameworkHttpAdapter _adapter;
    private readonly string _appId;
    private readonly ConcurrentDictionary<string, ConversationReference> _conversationReferences;

    private readonly BotState _userState;
    private readonly BotState _conversationState;

    public StatutoryController(IBotFrameworkHttpAdapter adapter, IConfiguration configuration, ConcurrentDictionary<string, ConversationReference> conversationReferences, ConversationState conversationState,
    UserState userState)
    {
        _adapter = adapter;
        _conversationReferences = conversationReferences;
        _appId = configuration["MicrosoftAppId"] ?? string.Empty;

        _conversationState = conversationState;
        _userState = userState;

    }

    public async Task<IActionResult> Get()
    {
        foreach (var conversationReference in _conversationReferences.Values)
        {
            await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, BotCallback, default(CancellationToken));
        }



        // Let the caller know proactive messages have been sent
        return new ContentResult()
        {
            Content = "<html><body><h1>Statutory Reconciliation Proactive messages have been sent.</h1></body></html>",
            ContentType = "text/html",
            StatusCode = (int)HttpStatusCode.OK,
        };
    }

    private async Task BotCallback(ITurnContext turnContext, CancellationToken cancellationToken)
    {
        await turnContext.SendActivityAsync("We are preparing your spreadsheet please wait...");
        await turnContext.SendActivityAsync("Your reconciled spreadsheet is ready, please click on the download link");

       //at here o want to call a dialog, somenthing like this: 
       //var StatutoryDialog = new StatutoryReconciliationDialog();
       //await turnContext.StartDialog(StatutoryDialog);

I see some other threads here at stack overflow about this subject, but they are from almost 2 years ago and some of the codes are different now in the samples. I will be trully grateful if someone can help me. Thanks in advance.


Solution

  • So, after a few days i figured out how to fix this problem.

    First you need to change your bot class, in this case i changed my "ProactiveBot.cs"

    from this:

    ProactiveBot: ActivityHandler 
    

    to this:

    ProactiveBot<T> : ActivityHandler where T : Dialog
    

    After that i just create a local variable of the current context instance and begin the dialog. I do this in the proactive controller, afeter send the message

    await turnContext.SendActivityAsync("We are preparing your spreadsheet please wait...");
    
    var dc = await new DialogSet(null).CreateContextAsync(turnContext);
    await dc.BeginDialogAsync(nameof(StatutoryDialog));
    

    Where "StatutoryDialog" is the name of you dialog class.