public SkillConfiguration(BotConfiguration botConfiguration, Dictionary <string, Dictionary <string, string> > languageModels, string[] supportedProviders = null, string[] parameters = null, Dictionary <string, object> configuration = null) { if (supportedProviders != null && supportedProviders.Count() > 0) { IsAuthenticatedSkill = true; } foreach (var service in botConfiguration.Services) { switch (service.Type) { case ServiceTypes.Generic: { if (service.Name == "Authentication") { var auth = service as GenericService; foreach (var provider in supportedProviders) { var matches = auth.Configuration.Where(x => x.Value == provider); foreach (var match in matches) { AuthenticationConnections.Add(match.Key, match.Value); } } } break; } } } foreach (var language in languageModels) { var localeConfig = new LocaleConfiguration { Locale = language.Key }; var path = language.Value["botFilePath"]; var secret = language.Value["botFileSecret"]; var config = BotConfiguration.Load(path, !string.IsNullOrEmpty(secret) ? secret : null); foreach (var service in config.Services) { switch (service.Type) { case ServiceTypes.Dispatch: { var dispatch = service as DispatchService; var dispatchApp = new LuisApplication(dispatch.AppId, dispatch.SubscriptionKey, dispatch.GetEndpoint()); localeConfig.DispatchRecognizer = new TelemetryLuisRecognizer(dispatchApp); break; } case ServiceTypes.Luis: { var luis = service as LuisService; var luisApp = new LuisApplication(luis.AppId, luis.SubscriptionKey, luis.GetEndpoint()); localeConfig.LuisServices.Add(service.Id, new TelemetryLuisRecognizer(luisApp)); break; } case ServiceTypes.QnA: { var qna = service as QnAMakerService; var qnaEndpoint = new QnAMakerEndpoint() { KnowledgeBaseId = qna.KbId, EndpointKey = qna.EndpointKey, Host = qna.Hostname, }; var qnaMaker = new TelemetryQnAMaker(qnaEndpoint); localeConfig.QnAServices.Add(qna.Id, qnaMaker); break; } } } LocaleConfigurations.Add(language.Key, localeConfig); } if (parameters != null) { // add the parameters the skill needs foreach (var parameter in parameters) { // Initialize each parameter to null. Needs to be set later by the bot. Properties.Add(parameter, null); } } if (configuration != null) { // add the additional keys the skill needs foreach (var set in configuration) { Properties.Add(set.Key, set.Value); } } }
/// <summary> /// Initializes a new instance of the <see cref="BotServices"/> class. /// </summary> /// <param name="botConfiguration">The <see cref="BotConfiguration"/> instance for the bot.</param> /// <param name="skills">List of <see cref="SkillDefinition"/> for loading skill configurations.</param> /// <param name="languageModels">The locale specifc language model configs for each supported language.</param> /// <param name="skillEventsConfig">The configuration for skill events.</param> public BotServices(BotConfiguration botConfiguration, Dictionary <string, Dictionary <string, string> > languageModels, List <SkillDefinition> skills, List <SkillEvent> skillEventsConfig) { // Create service clients for each service in the .bot file. foreach (var service in botConfiguration.Services) { switch (service.Type) { case ServiceTypes.AppInsights: { var appInsights = (AppInsightsService)service; if (appInsights == null) { throw new InvalidOperationException("The Application Insights is not configured correctly in your '.bot' file."); } if (string.IsNullOrWhiteSpace(appInsights.InstrumentationKey)) { throw new InvalidOperationException("The Application Insights Instrumentation Key ('instrumentationKey') is required to run this sample. Please update your '.bot' file."); } var telemetryConfig = new TelemetryConfiguration(appInsights.InstrumentationKey); TelemetryClient = new TelemetryClient(telemetryConfig) { InstrumentationKey = appInsights.InstrumentationKey, }; break; } case ServiceTypes.CosmosDB: { var cosmos = service as CosmosDbService; CosmosDbOptions = new CosmosDbStorageOptions { AuthKey = cosmos.Key, CollectionId = cosmos.Collection, DatabaseId = cosmos.Database, CosmosDBEndpoint = new Uri(cosmos.Endpoint), }; break; } case ServiceTypes.Generic: { if (service.Name == "Authentication") { var authentication = service as GenericService; AuthenticationConnections = authentication.Configuration; } break; } } } // Create locale configuration object for each language config in appsettings.json foreach (var language in languageModels) { if (language.Value.TryGetValue("botFilePath", out var botFilePath) && File.Exists(botFilePath)) { var botFileSecret = language.Value["botFileSecret"]; var config = BotConfiguration.Load(botFilePath, !string.IsNullOrEmpty(botFileSecret) ? botFileSecret : null); var localeConfig = new LocaleConfiguration { Locale = language.Key }; foreach (var service in config.Services) { switch (service.Type) { case ServiceTypes.Dispatch: { var dispatch = service as DispatchService; if (dispatch == null) { throw new InvalidOperationException("The Dispatch service is not configured correctly in your '.bot' file."); } if (string.IsNullOrWhiteSpace(dispatch.AppId)) { throw new InvalidOperationException("The Dispatch Luis Model Application Id ('appId') is required to run this sample. Please update your '.bot' file."); } if (string.IsNullOrWhiteSpace(dispatch.SubscriptionKey)) { throw new InvalidOperationException("The Subscription Key ('subscriptionKey') is required to run this sample. Please update your '.bot' file."); } var dispatchApp = new LuisApplication(dispatch.AppId, dispatch.SubscriptionKey, dispatch.GetEndpoint()); localeConfig.DispatchRecognizer = new TelemetryLuisRecognizer(dispatchApp); break; } case ServiceTypes.Luis: { var luis = service as LuisService; if (luis == null) { throw new InvalidOperationException("The Luis service is not configured correctly in your '.bot' file."); } if (string.IsNullOrWhiteSpace(luis.AppId)) { throw new InvalidOperationException("The Luis Model Application Id ('appId') is required to run this sample. Please update your '.bot' file."); } if (string.IsNullOrWhiteSpace(luis.AuthoringKey)) { throw new InvalidOperationException("The Luis Authoring Key ('authoringKey') is required to run this sample. Please update your '.bot' file."); } if (string.IsNullOrWhiteSpace(luis.SubscriptionKey)) { throw new InvalidOperationException("The Subscription Key ('subscriptionKey') is required to run this sample. Please update your '.bot' file."); } if (string.IsNullOrWhiteSpace(luis.Region)) { throw new InvalidOperationException("The Region ('region') is required to run this sample. Please update your '.bot' file."); } var luisApp = new LuisApplication(luis.AppId, luis.SubscriptionKey, luis.GetEndpoint()); var recognizer = new TelemetryLuisRecognizer(luisApp, logPersonalInformation: true); localeConfig.LuisServices.Add(service.Id, recognizer); break; } case ServiceTypes.QnA: { var qna = service as QnAMakerService; var qnaEndpoint = new QnAMakerEndpoint() { KnowledgeBaseId = qna.KbId, EndpointKey = qna.EndpointKey, Host = qna.Hostname, }; var qnaMaker = new TelemetryQnAMaker(qnaEndpoint, logPersonalInformation: true); localeConfig.QnAServices.Add(qna.Id, qnaMaker); break; } } } LocaleConfigurations.Add(language.Key, localeConfig); } } // Create a skill configurations for each skill in appsettings.json foreach (var skill in skills) { var skillConfig = new SkillConfiguration() { CosmosDbOptions = CosmosDbOptions }; foreach (var localeConfig in LocaleConfigurations) { skillConfig.LocaleConfigurations.Add(localeConfig.Key, new LocaleConfiguration { LuisServices = localeConfig.Value.LuisServices.Where(l => skill.LuisServiceIds.Contains(l.Key) == true).ToDictionary(l => l.Key, l => l.Value) }); } if (skill.SupportedProviders != null) { foreach (var provider in skill.SupportedProviders) { var matches = AuthenticationConnections.Where(x => x.Value == provider); foreach (var match in matches) { skillConfig.AuthenticationConnections.Add(match.Key, match.Value); } } } foreach (var set in skill.Configuration) { skillConfig.Properties.Add(set.Key, set.Value); } SkillDefinitions.Add(skill); SkillConfigurations.Add(skill.Id, skillConfig); SkillEvents = skillEventsConfig != null?skillEventsConfig.ToDictionary(i => i.Event) : null; } }
/// <summary> /// Initializes a new instance of the <see cref="BotServices"/> class. /// </summary> /// <param name="botConfiguration">The <see cref="BotConfiguration"/> instance for the bot.</param> /// <param name="skills">List of <see cref="SkillDefinition"/> for loading skill configurations.</param> /// <param name="languageModels">The locale specifc language model configs for each supported language.</param> public BotServices(BotConfiguration botConfiguration, Dictionary <string, Dictionary <string, string> > languageModels, List <SkillDefinition> skills) { // Create service clients for each service in the .bot file. foreach (var service in botConfiguration.Services) { switch (service.Type) { case ServiceTypes.AppInsights: { var appInsights = service as AppInsightsService; var telemetryConfig = new TelemetryConfiguration(appInsights.InstrumentationKey); TelemetryClient = new TelemetryClient(telemetryConfig); break; } case ServiceTypes.CosmosDB: { var cosmos = service as CosmosDbService; CosmosDbOptions = new CosmosDbStorageOptions { AuthKey = cosmos.Key, CollectionId = cosmos.Collection, DatabaseId = cosmos.Database, CosmosDBEndpoint = new Uri(cosmos.Endpoint), }; break; } case ServiceTypes.Generic: { if (service.Name == "Authentication") { var authentication = service as GenericService; AuthenticationConnections = authentication.Configuration; } break; } } } // Create locale configuration object for each language config in appsettings.json foreach (var language in languageModels) { var localeConfig = new LocaleConfiguration { Locale = language.Key }; var path = language.Value["botFilePath"]; var secret = language.Value["botFileSecret"]; var config = BotConfiguration.Load(path, !string.IsNullOrEmpty(secret) ? secret : null); foreach (var service in config.Services) { switch (service.Type) { case ServiceTypes.Dispatch: { var dispatch = service as DispatchService; var dispatchApp = new LuisApplication(dispatch.AppId, dispatch.SubscriptionKey, dispatch.GetEndpoint()); localeConfig.DispatchRecognizer = new TelemetryLuisRecognizer(dispatchApp); break; } case ServiceTypes.Luis: { var luis = service as LuisService; var luisApp = new LuisApplication(luis.AppId, luis.SubscriptionKey, luis.GetEndpoint()); localeConfig.LuisServices.Add(service.Id, new TelemetryLuisRecognizer(luisApp)); break; } case ServiceTypes.QnA: { var qna = service as QnAMakerService; var qnaEndpoint = new QnAMakerEndpoint() { KnowledgeBaseId = qna.KbId, EndpointKey = qna.EndpointKey, Host = qna.Hostname, }; var qnaMaker = new TelemetryQnAMaker(qnaEndpoint); localeConfig.QnAServices.Add(qna.Id, qnaMaker); break; } } } LocaleConfigurations.Add(language.Key, localeConfig); } // Create a skill configurations for each skill in appsettings.json foreach (var skill in skills) { var skillConfig = new SkillConfiguration() { CosmosDbOptions = CosmosDbOptions, TelemetryClient = TelemetryClient, }; foreach (var localeConfig in LocaleConfigurations) { skillConfig.LocaleConfigurations.Add(localeConfig.Key, new LocaleConfiguration { LuisServices = localeConfig.Value.LuisServices.Where(l => skill.LuisServiceIds.Contains(l.Key) == true).ToDictionary(l => l.Key, l => l.Value) }); } if (skill.SupportedProviders != null) { foreach (var provider in skill.SupportedProviders) { var matches = AuthenticationConnections.Where(x => x.Value == provider); foreach (var match in matches) { skillConfig.AuthenticationConnections.Add(match.Key, match.Value); } } } foreach (var set in skill.Configuration) { skillConfig.Properties.Add(set.Key, set.Value); } SkillDefinitions.Add(skill); SkillConfigurations.Add(skill.Id, skillConfig); } }