Search code examples
c#botframeworkmicrosoft-teams

Issues continuing a dialog created proactively


I have been able to start a dialog proactively with an adaptive card using the code below.

[HttpGet]
    [Route("proactivetest")]
    public async Task ProactiveTest()
    {
        try
        {
            await _frameworkAdapter.CreateConversationAsync(
                TeamsChannelId,
                ServiceUrl,
                new MicrosoftAppCredentials(MicrosoftAppId, MicrosoftAppPassword),
                new ConversationParameters(true, null, null, string.Empty,
                    new Activity(
                        type: ActivityTypes.Message,
                        text: "test",
                        serviceUrl: ServiceUrl,
                        channelId: TeamsChannelId
                        ),
                    new Dictionary<string, string> { ["teamsChannelId"] = TeamsChannelId }),
                Callback, default);
        }
        catch(Exception Ex)
        {
            Console.WriteLine($"Exception: {Ex.Message}");
        }
    }

    public async Task Callback(ITurnContext turnContext, CancellationToken cancellationToken)
    {
        DialogContext dialogContext = await _DialogSet.CreateContextAsync(turnContext, cancellationToken);

        await dialogContext.BeginDialogAsync(nameof(AdaptiveCardTests), null, cancellationToken);

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

        return;
    }

But when replying with values in the adaptive card I am unable to continue the dialog. I have tried to Create a DialogContext from the DialogSet but it always returns DialogContext["null"] and then runs a new dialog.

        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {
        DialogContext dialogContext = await _DialogSet.CreateContextAsync(turnContext, cancellationToken);

        if (dialogContext.ActiveDialog != null)   
        {
            await dialogContext.ContinueDialogAsync(cancellationToken);
            return;
        }

        _Logger.LogInformation("Running dialog with Message Activity.");
        await dialogContext.BeginDialogAsync(nameof(AdaptiveCardTests), null, cancellationToken);
    }

If you then reply to this new dialog the above code works correctly and continues the dialog as expected.

Any assistance would be greatly appreciated.


Solution

  • I have been able to solve this problem, the channelId parameter on BotFrameworkAdapter.CreateConversationAsync() need to be set to "msteams" and not the Teams Channel Id which I originally had this set to.

    Below is the updated code that was working correctly.

    [HttpGet]
        [Route("proactivetest")]
        public async Task ProactiveTest()
        {
            try
            {
                await _frameworkAdapter.CreateConversationAsync(
                    "msteams",
                    ServiceUrl,
                    new MicrosoftAppCredentials(MicrosoftAppId, MicrosoftAppPassword),
                    new ConversationParameters(true, new ChannelAccount($"28:{MicrosoftAppId}", BotName, null, null), null, string.Empty,
                        new Activity(
                            type: ActivityTypes.Message,
                            text: "test",
                            serviceUrl: ServiceUrl,
                            channelId: "msteams"
                            ),
                        new Dictionary<string, string> { ["teamsChannelId"] = TeamsChannelId }),ProactiveCallback, default);
            }
            catch(Exception Ex)
            {
                Console.WriteLine($"Exception: {Ex.Message}");
            }
        }
    
        public async Task ProactiveCallback(ITurnContext turnContext, CancellationToken cancellationToken)
        {
            DialogContext dialogContext = await _DialogSet.CreateContextAsync(turnContext, cancellationToken);
    
            await dialogContext.BeginDialogAsync(nameof(AdaptiveCardTests), null, cancellationToken);
    
            await _Accessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
        }