forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
177 lines (151 loc) · 7.72 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.ApplicationInsights;
using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Builder.BotFramework;
using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.Integration.AspNet.Core.Skills;
using Microsoft.Bot.Builder.Skills;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Solutions.Responses;
using Microsoft.Bot.Solutions.Skills;
using Microsoft.Bot.Solutions.Skills.Dialogs;
using Microsoft.Bot.Solutions.Skills.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Teams.Apps.VirtualAssistant.Adapters;
using Microsoft.Teams.Apps.VirtualAssistant.Authentication;
using Microsoft.Teams.Apps.VirtualAssistant.Bots;
using Microsoft.Teams.Apps.VirtualAssistant.Dialogs;
using Microsoft.Teams.Apps.VirtualAssistant.Services;
namespace Microsoft.Teams.Apps.VirtualAssistant
{
public class Startup
{
public Startup(IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile("cognitivemodels.json", optional: true)
.AddJsonFile($"cognitivemodels.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
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)
{
// Configure MVC
services.AddControllers().AddNewtonsoftJson();
services.AddSingleton(Configuration);
// Load settings
var settings = new BotSettings();
Configuration.Bind(settings);
services.AddSingleton(settings);
// Configure channel provider
services.AddSingleton<IChannelProvider, ConfigurationChannelProvider>();
// Configure configuration provider
services.AddSingleton<ICredentialProvider, ConfigurationCredentialProvider>();
// Configure telemetry
services.AddApplicationInsightsTelemetry();
services.AddSingleton<IBotTelemetryClient, BotTelemetryClient>();
services.AddSingleton<ITelemetryInitializer, OperationCorrelationTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, TelemetryBotIdInitializer>();
services.AddSingleton<TelemetryInitializerMiddleware>();
services.AddSingleton<TelemetryLoggerMiddleware>();
// Configure bot services
services.AddSingleton<BotServices>();
// Configure storage
// Uncomment the following line for local development without Cosmos Db
// services.AddSingleton<IStorage, MemoryStorage>();
services.AddSingleton<IStorage>(new CosmosDbStorage(settings.CosmosDb));
services.AddSingleton<ConversationState>();
// Configure localized responses
var localizedTemplates = new Dictionary<string, List<string>>();
var templateFiles = new List<string>() { "MainResponses" };
var supportedLocales = new List<string>() { "en-us", "de-de", "es-es", "fr-fr", "it-it", "zh-cn" };
foreach (var locale in supportedLocales)
{
var localeTemplateFiles = new List<string>();
foreach (var template in templateFiles)
{
// LG template for en-us does not include locale in file extension.
if (locale.Equals("en-us"))
{
localeTemplateFiles.Add(Path.Combine(".", "Responses", $"{template}.lg"));
}
else
{
localeTemplateFiles.Add(Path.Combine(".", "Responses", $"{template}.{locale}.lg"));
}
}
localizedTemplates.Add(locale, localeTemplateFiles);
}
services.AddSingleton(new LocaleTemplateEngineManager(localizedTemplates, settings.DefaultLocale ?? "en-us"));
// Register the skills configuration class
services.AddSingleton<SkillsConfiguration>();
// Register AuthConfiguration to enable custom claim validation.
services.AddSingleton(sp => new AuthenticationConfiguration { ClaimsValidator = new AllowedCallersClaimsValidator(sp.GetService<SkillsConfiguration>()) });
// Register dialogs
services.AddTransient<MainDialog>();
services.AddTransient<TeamsSwitchSkillDialog>();
// Register the Bot Framework Adapter with error handling enabled.
// Note: some classes use the base BotAdapter so we add an extra registration that pulls the same instance.
services.AddSingleton<BotFrameworkHttpAdapter, DefaultAdapter>();
services.AddSingleton<BotAdapter>(sp => sp.GetService<BotFrameworkHttpAdapter>());
// Configure bot
services.AddTransient<IBot>(sp => new DefaultActivityHandler<MainDialog>(sp, (MainDialog)sp.GetService(typeof(MainDialog)), this.Configuration["microsoftAppId"]));
// Register the skills conversation ID factory, the client and the request handler.
services.AddSingleton<SkillConversationIdFactoryBase, SkillConversationIdFactory>();
services.AddHttpClient<SkillHttpClient>();
services.AddSingleton<ChannelServiceHandler, SkillHandler>();
// Register the SkillDialogs (remote skills).
var section = Configuration?.GetSection("BotFrameworkSkills");
var skills = section?.Get<EnhancedBotFrameworkSkill[]>();
if (skills != null)
{
var hostEndpointSection = Configuration?.GetSection("SkillHostEndpoint");
if (hostEndpointSection == null)
{
throw new ArgumentException($"{hostEndpointSection} is not in the configuration");
}
else
{
var hostEndpoint = new Uri(hostEndpointSection.Value);
foreach (var skill in skills)
{
services.AddSingleton(sp =>
{
return new TeamsSkillDialog(sp.GetService<ConversationState>(), sp.GetService<SkillHttpClient>(), skill, Configuration, hostEndpoint);
});
}
}
}
}
// 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()
.UseWebSockets()
.UseRouting()
.UseEndpoints(endpoints => endpoints.MapControllers());
}
}
}