继续主动创建的对话的问题

Issues continuing a dialog created proactively

我已经能够使用下面的代码主动与自适应卡片开始对话。

[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;
    }

但是当用自适应卡片中的值回复时,我无法继续对话。我试图从 DialogSet 创建一个 DialogContext 但它总是 returns DialogContext["null"] 然后运行一个新的对话框。

        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);
    }

如果您随后回复这个新对话框,上面的代码将正常工作并按预期继续对话框。

如有任何帮助,我们将不胜感激。

您能否查看此文档以获取 Send proactive notifications to users. Also have a look into the C# sample 主动消息的代码

我已经解决了这个问题,BotFrameworkAdapter.CreateConversationAsync() 上的 channelId 参数需要设置为 "msteams" 而不是我最初设置的 Teams 频道 ID。

以下是可以正常工作的更新代码。

[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);
    }