예제 #1
0
        // 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> >();
        }
예제 #2
0
        // 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 credentials
            services.AddSingleton <ICredentialProvider, ConfigurationCredentialProvider>();

            var appCredentials = new MicrosoftAppCredentials(settings.MicrosoftAppId, settings.MicrosoftAppPassword);

            services.AddSingleton(appCredentials);

            // Configure telemetry
            services.AddApplicationInsightsTelemetry();
            var telemetryClient = new BotTelemetryClient(new TelemetryClient());

            services.AddSingleton <IBotTelemetryClient>(telemetryClient);
            services.AddBotApplicationInsights(telemetryClient);

            // Configure bot services
            services.AddSingleton <BotServices>();

            // 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, appCredentials);
                    var credentials = new MicrosoftAppCredentialsEx(settings.MicrosoftAppId, settings.MicrosoftAppPassword, skill.MSAappId);
                    skillDialogs.Add(new SkillDialog(skill, credentials, telemetryClient, userState, authDialog));
                }

                return(skillDialogs);
            });

            // Configure adapters
            // DefaultAdapter is for all regular channels that use Http transport
            services.AddSingleton <IBotFrameworkHttpAdapter, DefaultAdapter>();

            // DefaultWebSocketAdapter is for directline speech channel
            // This adapter implementation is currently a workaround as
            // later on we'll have a WebSocketEnabledHttpAdapter implementation that handles
            // both Http for regular channels and websocket for directline speech channel
            services.AddSingleton <WebSocketEnabledHttpAdapter, DefaultWebSocketAdapter>();

            // Configure bot
            services.AddTransient <IBot, DialogBot <MainDialog> >();
        }
        // 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);

            // Load settings
            var settings = new BotSettings();

            Configuration.Bind(settings);
            services.AddSingleton(settings);

            // Configure credentials
            services.AddSingleton <ICredentialProvider, ConfigurationCredentialProvider>();
            var appCredentials = new MicrosoftAppCredentials(settings.MicrosoftAppId, settings.MicrosoftAppPassword);

            services.AddSingleton(appCredentials);

            // 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
            var botservices = new BotServices(settings, new BotTelemetryClient(new Microsoft.ApplicationInsights.TelemetryClient()));

            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 <UserState>();
            services.AddSingleton <ConversationState>();

            // Configure localized responses
            var localizedTemplates = new Dictionary <string, List <string> >();
            var templateFiles      = new List <string>()
            {
                "MainResponses", "OnboardingResponses"
            };
            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 default locale should not include locale in file extension.
                    if (locale.Equals(settings.DefaultLocale ?? "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 dialogs
            services.AddTransient <MainDialog>();
            services.AddTransient <OnboardingDialog>();

            // SAMPLE: Multi-turn QnA dialog
            var currentLocale = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;

            services.AddTransient(s => new QnADialog(botservices.CognitiveModelSets[currentLocale].QnAServices["HRBenefits"]));

            // SAMPLE: Proactive notifications
            services.AddSingleton <ProactiveState>();

            // Register skill dialogs
            foreach (var skill in settings.Skills)
            {
                var authDialog  = BuildAuthDialog(skill, settings, appCredentials);
                var credentials = new MicrosoftAppCredentialsEx(settings.MicrosoftAppId, settings.MicrosoftAppPassword, skill.MSAappId);
                services.AddTransient(sp =>
                {
                    var userState       = sp.GetService <UserState>();
                    var telemetryClient = sp.GetService <IBotTelemetryClient>();
                    return(new SkillDialog(skill, credentials, telemetryClient, userState, authDialog));
                });
            }

            // IBotFrameworkHttpAdapter now supports both http and websocket transport
            services.AddSingleton <IBotFrameworkHttpAdapter, DefaultAdapter>();

            // Configure bot
            services.AddTransient <IBot, DefaultActivityHandler <MainDialog> >();
        }