Botframework V4-如何结束对话?

时间:2019-02-10 18:24:21

标签: c# .net botframework

如何结束Microsoft Bot Framework v4(在C#中)的对话? 我使用了几种保留在Azure CosmosDB中的不同状态。 我希望我的机器人返回到该状态,因此当用户再次向该机器人发送消息时,它会以欢迎消息进行回复。

我尝试复制v3的方式,但没有成功

var msg = turnContext.Activity.CreateReply();
msg.Type = ActivityTypes.EndOfConversation;
msg.AsEndOfConversationActivity().Code = EndOfConversationCodes.CompletedSuccessfully;

await turnContext.SendActivityAsync(msg, cancellationToken);

结果是:

  

[18:11:27] <-endOfConversation

     

[18:11:27] POST200conversations.replyToActivity

     

[18:11:27] POST200directline.postActivity

有点结束了对话,但是并没有清除状态。

1 个答案:

答案 0 :(得分:0)

C#v4 SDK现在使用endDialogAsync()和cancelAllDialogAsync()帮助管理对话框状态。

endDialogAsync()结束堆栈上的当前对话框,将控件返回到父对话框(如果存在)或转弯处理程序。此外,可以在任何可访问对话框上下文的地方调用它。最佳做法是在每个对话框的末尾调用它。

cancelAllDialogAsync()从堆栈中删除所有对话框。

以下是摘录自BotBuilder-Samples Basic-Bot sample的摘录,显示这两个摘录均用作OnTurnAsyc流程的一部分:

// if no one has responded,
if (!dc.Context.Responded)
{
    // examine results from active dialog
    switch (dialogResult.Status)
    {
        case DialogTurnStatus.Empty:
            switch (topIntent)
            {
                case GreetingIntent:
                    await dc.BeginDialogAsync(nameof(GreetingDialog));
                    break;

                case NoneIntent:
                    default:
                    // Help or no intent identified, either way, let's provide some help.
                    // to the user
                    await dc.Context.SendActivityAsync("I didn't understand what you just said to me.");
                    break;
                    }

                    break;

                 case DialogTurnStatus.Waiting:
                    // The active dialog is waiting for a response from the user, so do nothing.
                    break;

                case DialogTurnStatus.Complete:
                    await dc.EndDialogAsync();
                    break;

                default:
                    await dc.CancelAllDialogsAsync();
                    break;
            }
     }
}

要重新启动对话/对话,您将需要在ConversationUpdate,ConversationRelationUpdate等上实现活动。请注意,这是特定于通道的,这意味着每个通道都将确定将触发哪种方法。

同样,此代码段显示了如何在OnTurnAsync流程上实现它:

// Processes ConversationUpdate Activities to welcome the user.
else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
    {
        if (turnContext.Activity.MembersAdded != null)
        {
            await SendWelcomeMessageAsync(turnContext, cancellationToken);
        }
    }

最后,有一个“删除”方法可以在您的状态访问器上调用。查看this sample,其中显示了如何清除状态。

希望有帮助!