发送第一条消息之前先访问用户/会话状态

时间:2019-02-19 12:14:39

标签: c# botframework direct-line-botframework

在Bot Framework V4中,it is explained可以通过在OnTurnAsync中创建对话框上下文来访问用户状态或对话状态。

var dc = await Dialogs.CreateContextAsync(turnContext);

或在对话框上下文类中使用访问器

var state = await HogehogeSettingAccessor.GetAsync(stepContext.Context);

但是,如何在将消息发送到对话框之前访问它们?

我目前正在开发Directline API,并希望在发送第一条消息之前先参考语言设置(例如,如果书面语言与设置不匹配,请忽略用户输入)。

private async Task OnMessageReceive(SocketMessage socketMessage)
{
    if (IsLanguageMatch(socketMessage)){
        await channel.SendMessageAsync(response);
    }
}

我该如何实现?

1 个答案:

答案 0 :(得分:1)

您可以让OnTurnAsync仅在满足条件的情况下启动第一个/主对话框。 当用户第一次向机器人发送消息时,将没有任何活动对话框。您可以利用该条件并在其中添加您的条件,只有在两个条件都满足的情况下才启动对话框:

DispatchQueue.main.async {
      //your code block
}

OnTurnAsync应该看起来像这样:

 // Getting the bot accessor state you want to use   

LanguageAccessor languageAccessor = await _accessors.LanguageAccessor.GetAsync(turnContext, () => new LanguageAccessor(), cancellationToken);

    // Every step sends a response. If no dialog is active, no response is sent and turnContext.Responded is null
    //where turnContext.Activity.Text is the message sent by the user

 if (!turnContext.Responded && ((languageAccessor.property)turnContext.Activity.Text))

另一种选择是将访问器对象发送到要使用它的Dialog,并使用Dialog的Waterfall第一步,如果满足验证则继续该对话框,否则结束该对话框。

瀑布步骤应如下所示:

        public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
                {

                    if (turnContext.Activity.Type == ActivityTypes.Message)
                    {
        // Establish dialog state from the conversation state.
                        DialogContext dc = await _dialogs.CreateContextAsync(turnContext, cancellationToken);

                        // Get the user's info.
                       LanguageAccessor languageAccessor = await _accessors.LanguageAccessor.GetAsync(turnContext, () => new LanguageAccessor(), cancellationToken);

                        await _accessors.UserInfoAccessor.SetAsync(turnContext, userInfo, cancellationToken);

                        // Continue any current dialog.
                        DialogTurnResult dialogTurnResult = await dc.ContinueDialogAsync();    

                        // Every dialog step sends a response, so if no response was sent,
                        // then no dialog is currently active and the Else if is entered.

                        if (!turnContext.Responded && ((languageAccessor.property)turnContext.Activity.Text))
                        {

                            //This starts the MainDialog if there's no active dialog when the user sends a message
                            await dc.BeginDialogAsync(MainDialogId, null, cancellationToken);


                        }

                       //Else if the validation is not passed
                       else if (!turnContext.Responded && ! 
                       ((languageAccessor.property)turnContext.Activity.Text))
                          { await turnContext.SendActivityAsync("Thank you, see you next time"); }
                          }
            }