forked from microsoft/botbuilder-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStartup.cs
139 lines (117 loc) · 5.56 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.AI.Luis;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.TestBot.Shared.Bots;
using Microsoft.Bot.Builder.TestBot.Shared.Dialogs;
using Microsoft.Bot.Builder.TestBot.Shared.Services;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Microsoft.BotBuilderSamples
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
/// <summary>
/// Gets the configuration that represents a set of key/value application configuration properties.
/// </summary>
/// <value>
/// The <see cref="IConfiguration"/> that represents a set of key/value application configuration properties.
/// </value>
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.MaxDepth = HttpHelper.BotMessageSerializerSettings.MaxDepth;
});
services.AddHttpClient();
// Create the debug middleware
services.AddSingleton(sp => new MicrosoftAppCredentials(sp.GetRequiredService<IConfiguration>()["MicrosoftAppId"], sp.GetRequiredService<IConfiguration>()["MicrosoftAppPassword"]));
services.AddSingleton<InspectionMiddleware>();
// 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 App state. (Used by the DebugMiddleware.)
services.AddSingleton<InspectionState>();
// Create the User state. (Used in this bot's Dialog implementation.)
services.AddSingleton<UserState>();
// Create the Conversation state. (Used by the Dialog system itself.)
services.AddSingleton<ConversationState>();
// Register LUIS recognizer
RegisterLuisRecognizers(services);
// Register dialogs that will be used by the bot.
RegisterDialogs(services);
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddScoped<MyBot>();
services.AddScoped<DialogBot<MainDialog>>();
services.AddScoped<DialogAndWelcomeBot<MainDialog>>();
// We can also run the inspection at a different endpoint. Just uncomment these lines.
// services.AddSingleton<DebugAdapter>();
// services.AddTransient<DebugBot>();
services.AddTransient<Func<string, IBot>>(serviceProvider => key =>
{
switch (key)
{
case "mybot":
return serviceProvider.GetService<MyBot>();
case "dialogbot":
return serviceProvider.GetService<DialogBot<MainDialog>>();
case "messages":
case "dialogandwelcomebot":
return serviceProvider.GetService<DialogAndWelcomeBot<MainDialog>>();
default:
return null;
}
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseDefaultFiles()
.UseStaticFiles()
.UseRouting()
.UseAuthorization()
.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// app.UseHttpsRedirection();
}
private static void RegisterDialogs(IServiceCollection services)
{
// Register booking dialog
services.AddSingleton(new BookingDialog(new GetBookingDetailsDialog(), new FlightBookingService()));
// The Dialog that will be run by the bot.
services.AddSingleton<MainDialog>();
}
private void RegisterLuisRecognizers(IServiceCollection services)
{
var luisIsConfigured = !string.IsNullOrEmpty(Configuration["LuisAppId"]) && !string.IsNullOrEmpty(Configuration["LuisAPIKey"]) && !string.IsNullOrEmpty(Configuration["LuisAPIHostName"]);
if (luisIsConfigured)
{
var luisApplication = new LuisApplication(
Configuration["LuisAppId"],
Configuration["LuisAPIKey"],
"https://" + Configuration["LuisAPIHostName"]);
var recognizer = new LuisRecognizer(new LuisRecognizerOptionsV2(luisApplication));
services.AddSingleton<IRecognizer>(recognizer);
}
}
}
}