Microsoft Bot Framework主动消息,它将继续当前对话框

时间:2019-02-26 16:08:28

标签: c# botframework

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

}

}

1 个答案:

答案 0 :(得分:0)

有两种方法可以执行此操作,这实际上取决于您的代码。基本上,在发送发现用户B的主动消息的同一位置,需要调用dc.ContinueDialogAsync()dc.RepromptDialogAsync()(如果适用)。

话虽这么说,我认为最好的选择是分割对话框。一个对话框使用户A进入队列。进入后,它们将不再处于对话框中。找到用户B后,它将向用户A发送新对话框。

我或多或少通过Sample 16. Proactive Messages通过以下方式完成此操作:

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

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

     

var dc =等待Dialogs.CreateContextAsync(turnContext);

     

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

注意:为进行测试,我在用户“运行”作业后为用户A创建了一个对话框。对话框一直坐在那里,直到用户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);
}
相关问题