Bot Builder V3版
组合流程
Bot.Builder.Community.Dialogs.FormFlow
图书馆。
你的
帮助表单
可以用与其他V4组件对话框相同的方式添加到V4对话框集中:
_dialogs.Add(FormDialog.FromForm(HelpForm.BuildForm));
下面是一个更完整的示例:
public class TestEchoBotAccessors
{
public TestEchoBotAccessors(ConversationState conversationState)
{
ConversationState = conversationState ?? throw new ArgumentNullException(nameof(conversationState));
}
public ConversationState ConversationState { get; }
public IStatePropertyAccessor<DialogState> ConversationDialogState { get; set; }
}
Startup.cs配置服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddBot<TestEchoBotBot>(options =>
{
IStorage dataStore = new MemoryStorage();
options.State.Add(new ConversationState(dataStore));
options.Middleware.Add(new AutoSaveStateMiddleware(options.State.ToArray()));
var secretKey = Configuration.GetSection("botFileSecret")?.Value;
var botFilePath = Configuration.GetSection("botFilePath")?.Value;
// Loads .bot configuration file and adds a singleton that your Bot can access through dependency injection.
var botConfig = BotConfiguration.Load(botFilePath ?? @".\TestEchoBot.bot", secretKey);
services.AddSingleton(sp => botConfig ?? throw new InvalidOperationException($"The .bot config file could not be loaded. ({botConfig})"));
// Retrieve current endpoint.
var environment = _isProduction ? "production" : "development";
var service = botConfig.Services.Where(s => s.Type == "endpoint" && s.Name == environment).FirstOrDefault();
if (!(service is EndpointService endpointService))
{
throw new InvalidOperationException($"The .bot file does not contain an endpoint with name '{environment}'.");
}
options.CredentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);
});
services.AddSingleton(sp =>
{
var options = sp.GetRequiredService<IOptions<BotFrameworkOptions>>().Value;
var conversationState = options.State.OfType<ConversationState>().FirstOrDefault();
var accessors = new TestEchoBotAccessors(conversationState)
{
ConversationDialogState = conversationState.CreateProperty<DialogState>("DialogState")
};
return accessors;
});
}
机器人代码:
public class TestEchoBotBot : IBot
{
private readonly TestEchoBotAccessors _accessors;
private DialogSet _dialogs;
public TestEchoBotBot(TestEchoBotAccessors accessors, ILoggerFactory loggerFactory)
{
if (loggerFactory == null)
{
throw new System.ArgumentNullException(nameof(loggerFactory));
}
_dialogs = new DialogSet(accessors.ConversationDialogState);
_dialogs.Add(FormDialog.FromForm(HelpForm.BuildForm));
_accessors = accessors ?? throw new System.ArgumentNullException(nameof(accessors));
}
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
var dialogContext = await _dialogs.CreateContextAsync(turnContext, cancellationToken);
if (turnContext.Activity.Text?.ToUpper() == "HELP")
{
await dialogContext.BeginDialogAsync(typeof(HelpForm).Name, null, cancellationToken);
}
else
{
var dialogResult = await dialogContext.ContinueDialogAsync(cancellationToken);
if ((dialogResult.Status == DialogTurnStatus.Cancelled || dialogResult.Status == DialogTurnStatus.Empty))
{
var responseMessage = $"You sent '{turnContext.Activity.Text}'\n";
await turnContext.SendActivityAsync(responseMessage);
}
}
}
}
}