继续当前对话框的Microsoft Bot Framework主动消息

问题描述 投票:0回答:1

在Microsoft bot框架中有主动消息的概念 - > https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-proactive-message?view=azure-bot-service-4.0&tabs=csharp

我在我的解决方案中使用多个不同的对话框,我将一些数据存储在数据库中,每次加载都会加载。根据数据库中的数据,修改状态对象,并根据该对象继续。

在我的情况下,用户A启动对话框,系统响应“我把你放入队列”然后一段时间后B开始他的对话并询问他是否应该与A配对。用户B确认后,用户A的对话框应该继续

我可以给他写一个简单的消息,如下所示,但我不知道如何简单地为匹配用户强制一个新的“转向”,这样对话框就会继续。

public class BasicBot : IBot
{
    // some properties

    public BasicBot(CustomBotState botState, BotServices services, UserState userState, ConversationState conversationState, ILoggerFactory loggerFactory, EndpointService endpointService)
    {
        // set some properties

        _conversationReferenceAccessor = _botState.CreateProperty<Dictionary<string, ConversationReference>>(nameof(MatchConversationReference));
        _dialogStateAccessor = _conversationState.CreateProperty<DialogState>(nameof(DialogState));
        _matchStateAccessor = _userState.CreateProperty<MatchState>(nameof(MatchState));

        var appId = string.IsNullOrWhiteSpace(endpointService.AppId) ? "1" : endpointService.AppId;

        Dialogs = new DialogSet(_dialogStateAccessor);
        Dialogs.Add(new MatchDialog(_matchStateAccessor, loggerFactory, services, appId, _conversationReferenceAccessor));
    }        

    private DialogSet Dialogs { get; set; }

    public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken)
    {
        var dialogContext = await Dialogs.CreateContextAsync(turnContext);

        if (turnContext.Activity.Type == ActivityTypes.Message)
        {
            var dialogResult = await dialogContext.ContinueDialogAsync();

            if (!dialogContext.Context.Responded)
            {
                var match = LoadMatchFromDatabase();
                await dialogContext.BeginDialogAsync(nameof(MatchDialog), match);
            }
        }

        // save the state of conversation, user, bot
    }
}
public class MatchDialog : ComponentDialog
{
    // some properties

    public MatchDialog(IStatePropertyAccessor<MatchState> stateAccessor, ILoggerFactory loggerFactory, BotServices services, string appId, IStatePropertyAccessor<Dictionary<string, ConversationReference>> _matchConversationPropertyAccessor)
            : base(nameof(MatchDialog))
    {
        // set some properties

        var waterfallSteps = new WaterfallStep[]
        {
                InitializeStateStepAsync,
                WaitForAnswer,
        };

        AddDialog(new WaterfallDialog(nameof(MatchDialog), waterfallSteps));
    }

    private async Task<DialogTurnResult> WaitForAnswer(WaterfallStepContext steps, CancellationToken cancellationToken)
    {
        var otherUser = await GetOtherUser(steps);
        var conversations = await GetMatchConversion(steps.Context);

        if (conversations.ContainsKey(otherUser.Identifier))
        {
            await steps.Context.Adapter.ContinueConversationAsync(AppId, conversations[otherUser.Identifier],
                   async (turnContext, token) =>
                   {
                       await turnContext.SendActivityAsync($"Found someone for you");

                   },
                   cancellationToken);
        }
    }

    private async Task<DialogTurnResult> InitializeStateStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        var state = await StateAccessor.GetAsync(stepContext.Context, () => null);
        var match = stepContext.Options as Match;

        if (state == null)
        {
            await StateAccessor.SetAsync(stepContext.Context, new MatchState() { Match = match });
        }
        else if (state.Match == null || match.Id != state.Match.Id)
        {
            state.Match = match;
        }

        return await stepContext.NextAsync();
    }

}

}

c# botframework
1个回答
0
投票

有几种方法可以做到这一点,这实际上取决于你的代码。基本上,在您发送用户B的主动消息的同一位置,您需要调用dc.ContinueDialogAsync()dc.RepromptDialogAsync()(如果适用)。

话虽如此,我认为最好的选择是分割你的Dialog。一个Dialog在队列中获取用户A.进入后,他们不再处于对话中。找到用户B后,它会向用户A发送新的对话框。

我或多或少用Sample 16. Proactive Messages完成了这个:

  1. 创建用户B后要调用的对话框
  2. CreateCallback()(此示例主动发送消息的地方)下,我在该方法的末尾添加了以下代码(由于某种原因,它不希望格式化为代码):

await turnContext.SendActivityAsync($“Job {jobInfo.TimeStamp}已完成。”);

var dc = await Dialogue.CreateContext Async(turnContext);

等待dc.BeginDialogAsync(nameof(MyDialog));

注意:为了测试,我在“运行”作业后为用户A创建了一个Dialog。对话框位于那里,直到用户B完成作业。紧接着为用户A启动了一个新对话框。

对你来说,这可能看起来像:

//sample how I write something into the other conversation
var conversations = await GetMatchConversion(steps.Context);
if (conversations.ContainsKey(otherUser.Identifier))
{
    await steps.Context.Adapter.ContinueConversationAsync(AppId, conversations[otherUser.Identifier],
           async (turnContext, token) =>
           {
               // Send the user a proactive confirmation message.
               await turnContext.SendActivityAsync($"{currentUser.Display()} I found a matching user...");
               var dc = await Dialogs.CreateContextAsync(turnContext);
               await dc.BeginDialogAsync(nameof(UserFoundDialog));
           },
           cancellationToken);
}
© www.soinside.com 2019 - 2024. All rights reserved.