Skip to content
Permalink
Fetching contributors…
Cannot retrieve contributors at this time
147 lines (126 sloc) 5.96 KB
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
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.Skills;
using Microsoft.Bot.Builder.Skills.Auth;
using Microsoft.Bot.Builder.Skills.Models.Manifest;
using Microsoft.Bot.Builder.Solutions.Authentication;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using VirtualAssistantSample.Bots;
using VirtualAssistantSample.Dialogs;
using VirtualAssistantSample.Services;
namespace VirtualAssistantSample
{
public class Startup
{
public Startup(IHostingEnvironment 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)
.AddJsonFile("skills.json", optional: true)
.AddJsonFile($"skills.{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)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
var provider = services.BuildServiceProvider();
// Load settings
var settings = new BotSettings();
Configuration.Bind(settings);
services.AddSingleton(settings);
// Configure bot services
services.AddSingleton<BotServices>();
// Configure credentials
services.AddSingleton<ICredentialProvider, ConfigurationCredentialProvider>();
services.AddSingleton(new MicrosoftAppCredentials(settings.MicrosoftAppId, settings.MicrosoftAppPassword));
// Configure telemetry
var telemetryClient = new BotTelemetryClient(new TelemetryClient(settings.AppInsights));
services.AddSingleton<IBotTelemetryClient>(telemetryClient);
services.AddBotApplicationInsights(telemetryClient);
// Configure storage
services.AddSingleton<IStorage>(new CosmosDbStorage(settings.CosmosDb));
services.AddSingleton<UserState>();
services.AddSingleton<ConversationState>();
services.AddSingleton(sp =>
{
var userState = sp.GetService<UserState>();
var conversationState = sp.GetService<ConversationState>();
return new BotStateSet(userState, conversationState);
});
// Register dialogs
services.AddTransient<CancelDialog>();
services.AddTransient<EscalateDialog>();
services.AddTransient<MainDialog>();
services.AddTransient<OnboardingDialog>();
// Register skill dialogs
services.AddTransient(sp =>
{
var userState = sp.GetService<UserState>();
var skillDialogs = new List<SkillDialog>();
foreach (var skill in settings.Skills)
{
var authDialog = BuildAuthDialog(skill, settings);
var credentials = new MicrosoftAppCredentialsEx(settings.MicrosoftAppId, settings.MicrosoftAppPassword, skill.MSAappId);
skillDialogs.Add(new SkillDialog(skill, credentials, telemetryClient, userState, authDialog));
}
return skillDialogs;
});
// Configure adapters
services.AddSingleton<IBotFrameworkHttpAdapter, DefaultAdapter>();
// Configure bot
services.AddTransient<IBot, DialogBot<MainDialog>>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseBotApplicationInsights()
.UseDefaultFiles()
.UseStaticFiles()
.UseMvc();
}
// This method creates a MultiProviderAuthDialog based on a skill manifest.
private MultiProviderAuthDialog BuildAuthDialog(SkillManifest skill, BotSettings settings)
{
if (skill.AuthenticationConnections?.Count() > 0)
{
if (settings.OAuthConnections.Any() && settings.OAuthConnections.Any(o => skill.AuthenticationConnections.Any(s => s.ServiceProviderId == o.Provider)))
{
var oauthConnections = settings.OAuthConnections.Where(o => skill.AuthenticationConnections.Any(s => s.ServiceProviderId == o.Provider)).ToList();
return new MultiProviderAuthDialog(oauthConnections);
}
else
{
throw new Exception($"You must configure at least one supported OAuth connection to use this skill: {skill.Name}.");
}
}
return null;
}
}
}
You can’t perform that action at this time.