简而言之,我想知道如何永远停止Microsoft示例git repo循环上的多提示示例。
我一直在查看V4文档,但找不到任何解决方案所暗示的内容。
Microsoft已发布了一个示例,以利用此处提供的多提示对话框: https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/05.multi-turn-prompt
如果您克隆并运行该活动,则运行如下;
您:发送一条消息
机器人:以打开对话框作为响应
您:回复对话框
机器人:进入对话框的下一步
....
运行到最后一步
return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
机器人在瀑布式步骤中发送最终消息,并等待来自用户的另一条消息。当用户发送任何消息时,机器人会再次激活对话框瀑布。
很显然,由于DialogBot.cs
中的此方法,每次用户发送消息时,机器人都会进入对话框。
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
Logger.LogInformation("Running dialog with Message Activity.");
// Run the Dialog with the new message Activity.
await Dialog.Run(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
}
我想停止这种情况,只运行一次对话框,一旦对话框第一次结束,就不应再次激活该对话框。
答案 0 :(得分:0)
仔细研究Botbuilder-Samples存储库中的示例45.state-management。它演示了如何引用状态中的设置值来确定对话框流程。阅读位于here的“保存用户和对话数据”文档也对您有好处。
简而言之,您需要执行以下操作:
在 Startup.cs 文件中,添加以下内容。准备进行配置以满足您的需求。
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Create the credential provider to be used with the Bot Framework Adapter.
services.AddSingleton<ICredentialProvider, ConfigurationCredentialProvider>();
// Create the Bot Framework Adapter with error handling enabled.
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
// Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
services.AddSingleton<IStorage, MemoryStorage>();
// Create the User state.
services.AddSingleton<UserState>();
// Create the Conversation state.
services.AddSingleton<ConversationState>();
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddTransient<IBot, StateManagementBot>();
}
}
在您的“ bot.cs” 文件中,执行以下操作。同样,这是一个示例,请根据您的需求进行调整。首先,设置状态对象(_conversationState,_userState)。完成后,您可以根据对话框流使用这些值设置和调用状态值,以帮助执行下一个操作。在此示例中,引用状态是为了确定是否应再次询问用户名。
namespace Microsoft.BotBuilderSamples
{
public class StateManagementBot : ActivityHandler
{
private BotState _conversationState;
private BotState _userState;
public StateManagementBot(ConversationState conversationState, UserState userState)
{
_conversationState = conversationState;
_userState = userState;
}
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
await base.OnTurnAsync(turnContext, cancellationToken);
// Save any state changes that might have occured during the turn.
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await _userState.SaveChangesAsync(turnContext, false, cancellationToken);
}
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
await turnContext.SendActivityAsync("Welcome to State Bot Sample. Type anything to get started.");
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
// Get the state properties from the turn context.
var conversationStateAccessors = _conversationState.CreateProperty<ConversationData>(nameof(ConversationData));
var conversationData = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationData());
var userStateAccessors = _userState.CreateProperty<UserProfile>(nameof(UserProfile));
var userProfile = await userStateAccessors.GetAsync(turnContext, () => new UserProfile());
if (string.IsNullOrEmpty(userProfile.Name))
{
// First time around this is set to false, so we will prompt user for name.
if (conversationData.PromptedUserForName)
{
// Set the name to what the user provided.
userProfile.Name = turnContext.Activity.Text?.Trim();
// Acknowledge that we got their name.
await turnContext.SendActivityAsync($"Thanks {userProfile.Name}. To see conversation data, type anything.");
// Reset the flag to allow the bot to go though the cycle again.
conversationData.PromptedUserForName = false;
}
else
{
// Prompt the user for their name.
await turnContext.SendActivityAsync($"What is your name?");
// Set the flag to true, so we don't prompt in the next turn.
conversationData.PromptedUserForName = true;
}
}
else
{
// Add message details to the conversation data.
// Convert saved Timestamp to local DateTimeOffset, then to string for display.
var messageTimeOffset = (DateTimeOffset) turnContext.Activity.Timestamp;
var localMessageTime = messageTimeOffset.ToLocalTime();
conversationData.Timestamp = localMessageTime.ToString();
conversationData.ChannelId = turnContext.Activity.ChannelId.ToString();
// Display state data.
await turnContext.SendActivityAsync($"{userProfile.Name} sent: {turnContext.Activity.Text}");
await turnContext.SendActivityAsync($"Message received at: {conversationData.Timestamp}");
await turnContext.SendActivityAsync($"Message received from: {conversationData.ChannelId}");
}
}
}
}
通过这种方式,您可以跟踪用户是否已经访问了特定对话框。如果该值为true,则跳过对话框,并重定向用户。希望有所帮助!