Esempio n. 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddNewtonsoftJson();

            services.AddSingleton <IConfiguration>(this.Configuration);

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

            Configuration.Bind(settings);

            // Create the credential provider to be used with the Bot Framework Adapter.
            services.AddSingleton <ICredentialProvider, ConfigurationCredentialProvider>();
            services.AddSingleton <BotAdapter>(sp => (BotFrameworkHttpAdapter)sp.GetService <IBotFrameworkHttpAdapter>());

            // Register AuthConfiguration to enable custom claim validation for skills.
            services.AddSingleton(sp => new AuthenticationConfiguration {
                ClaimsValidator = new AllowedCallersClaimsValidator(settings.SkillConfiguration)
            });

            // register components.
            ComponentRegistration.Add(new DialogsComponentRegistration());
            ComponentRegistration.Add(new DeclarativeComponentRegistration());
            ComponentRegistration.Add(new AdaptiveComponentRegistration());
            ComponentRegistration.Add(new LanguageGenerationComponentRegistration());
            ComponentRegistration.Add(new QnAMakerComponentRegistration());
            ComponentRegistration.Add(new LuisComponentRegistration());

            // This is for custom action component registration.
            ComponentRegistration.Add(new CustomActionComponentRegistration());

            // Register the skills client and skills request handler.
            services.AddSingleton <SkillConversationIdFactoryBase, SkillConversationIdFactory>();
            services.AddHttpClient <BotFrameworkClient, SkillHttpClient>();
            services.AddSingleton <ChannelServiceHandler, SkillHandler>();

            // Register telemetry client, initializers and middleware
            services.AddApplicationInsightsTelemetry(settings?.ApplicationInsights?.InstrumentationKey ?? string.Empty);

            services.AddSingleton <ITelemetryInitializer, OperationCorrelationTelemetryInitializer>();
            services.AddSingleton <ITelemetryInitializer, TelemetryBotIdInitializer>();
            services.AddSingleton <IBotTelemetryClient, BotTelemetryClient>();
            services.AddSingleton <TelemetryLoggerMiddleware>(sp =>
            {
                var telemetryClient = sp.GetService <IBotTelemetryClient>();
                return(new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: settings?.Telemetry?.LogPersonalInformation ?? false));
            });
            services.AddSingleton <TelemetryInitializerMiddleware>(sp =>
            {
                var httpContextAccessor       = sp.GetService <IHttpContextAccessor>();
                var telemetryLoggerMiddleware = sp.GetService <TelemetryLoggerMiddleware>();
                return(new TelemetryInitializerMiddleware(httpContextAccessor, telemetryLoggerMiddleware, settings?.Telemetry?.LogActivities ?? false));
            });

            var storage = ConfigureStorage(settings);

            services.AddSingleton(storage);
            var userState         = new UserState(storage);
            var conversationState = new ConversationState(storage);

            services.AddSingleton(userState);
            services.AddSingleton(conversationState);

            //Add Cross Channel User State
            var crossChannelUserState = new CrossChannelUserState(storage, userState);

            services.AddSingleton(crossChannelUserState);

            // Configure bot loading path
            var botDir           = settings.Bot;
            var resourceExplorer = new ResourceExplorer().AddFolder(botDir);
            var rootDialog       = GetRootDialog(botDir);

            var defaultLocale = Configuration.GetValue <string>("defaultLanguage") ?? "en-us";

            services.AddSingleton(resourceExplorer);

            resourceExplorer.RegisterType <OnQnAMatch>("Microsoft.OnQnAMatch");

            services.AddSingleton <IBotFrameworkHttpAdapter, BotFrameworkHttpAdapter>(s =>
                                                                                      GetBotAdapter(storage, settings, userState, conversationState, s));

            // Configure and add ACS SMS Adapter
            var acsSmsAdapterOptions = new AcsSmsAdapterOptions()
            {
                AcsPhoneNumber        = Configuration.GetSection("acsSmsAdapterSettings")["AcsPhoneNumber"],      // e.g. +15552622396
                AcsConnectionString   = Configuration.GetSection("acsSmsAdapterSettings")["AcsConnectionString"], // From the Azure portal and should start with 'endpoint=https://'
                EnableDeliveryReports = bool.Parse(Configuration.GetSection("acsSmsAdapterSettings")["EnableDeliveryReports"])
            };

            services.AddSingleton(s => GetAcsBotAdapter(acsSmsAdapterOptions, storage, settings, userState, crossChannelUserState, conversationState, s));

            var removeRecipientMention = settings?.Feature?.RemoveRecipientMention ?? false;

            services.AddSingleton <IBot>(s =>
                                         new ComposerBotWithSms(
                                             s.GetService <ConversationState>(),
                                             s.GetService <UserState>(),
                                             s.GetService <ResourceExplorer>(),
                                             s.GetService <BotFrameworkClient>(),
                                             s.GetService <SkillConversationIdFactoryBase>(),
                                             s.GetService <IBotTelemetryClient>(),
                                             rootDialog,
                                             defaultLocale,
                                             s.GetService <AcsSmsAdapter>(),
                                             Configuration,
                                             removeRecipientMention));
        }
Esempio n. 2
0
        public AcsSmsAdapter GetAcsBotAdapter(AcsSmsAdapterOptions options, IStorage storage, BotSettings settings, UserState userState, CrossChannelUserState crossChannelUserState, ConversationState conversationState, IServiceProvider s)
        {
            var adapter = new AcsSmsAdapter(options);

            adapter
            .UseStorage(storage)
            .UseBotState(userState, conversationState, crossChannelUserState)
            .Use(new RegisterClassMiddleware <IConfiguration>(Configuration))
            .Use(s.GetService <TelemetryInitializerMiddleware>());

            adapter.OnTurnError = async(turnContext, exception) =>
            {
                await turnContext.SendActivityAsync(exception.Message).ConfigureAwait(false);

                await conversationState.ClearStateAsync(turnContext).ConfigureAwait(false);

                await conversationState.SaveChangesAsync(turnContext).ConfigureAwait(false);
            };
            return(adapter);
        }