Пример #1
0
        /// <summary>
        /// Map the Bot Framework into the request execution pipeline.
        /// </summary>
        /// <param name="httpConfiguration">The <see cref="HttpConfiguration" /> to map the bot into.</param>
        /// <param name="configurer">A callback to configure the bot.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static HttpConfiguration MapBotFramework(this HttpConfiguration httpConfiguration, Action <BotFrameworkConfigurationBuilder> configurer = null)
        {
            if (httpConfiguration == null)
            {
                throw new ArgumentNullException(nameof(httpConfiguration));
            }

            var options        = new BotFrameworkOptions();
            var optionsBuilder = new BotFrameworkConfigurationBuilder(options);

            configurer?.Invoke(optionsBuilder);

            var botFrameworkAdapter = httpConfiguration.DependencyResolver.GetService(typeof(BotFrameworkAdapter)) as BotFrameworkAdapter;

            if (botFrameworkAdapter == null)
            {
                var credentialProvider = ResolveCredentialProvider(options);

                // TODO: fix up constructor to take options
                botFrameworkAdapter = new BotFrameworkAdapter(credentialProvider, options.ConnectorClientRetryPolicy, options.HttpClient);
            }

            // error handler
            botFrameworkAdapter.ErrorHandler = options.ErrorHandler;

            // add middleware
            foreach (var middleware in options.Middleware)
            {
                botFrameworkAdapter.Use(middleware);
            }

            ConfigureBotRoutes(httpConfiguration, options, botFrameworkAdapter);

            return(httpConfiguration);
        }
Пример #2
0
        private static ICredentialProvider ResolveCredentialProvider(BotFrameworkOptions options)
        {
            var credentialProvider = options.CredentialProvider;

            // If a credential provider was explicitly configured, just return that straight away
            if (credentialProvider != null)
            {
                return(credentialProvider);
            }

            return(new SimpleCredentialProvider(ConfigurationManager.AppSettings[MicrosoftAppCredentials.MicrosoftAppIdKey], ConfigurationManager.AppSettings[MicrosoftAppCredentials.MicrosoftAppPasswordKey]));
        }
Пример #3
0
        private static void ConfigureBotRoutes(HttpConfiguration httpConfiguration, BotFrameworkOptions options, IAdapterIntegration adapter)
        {
            var routes  = httpConfiguration.Routes;
            var baseUrl = options.Paths.BasePath;

            routes.MapHttpRoute(
                BotMessageHandler.RouteName,
                baseUrl.Trim('/') + "/" + options.Paths.MessagesPath.Trim('/'),
                defaults: null,
                constraints: null,
                handler: new BotMessageHandler(adapter));
        }
Пример #4
0
        public static HttpConfiguration MapBotFramework(this HttpConfiguration httpConfiguration, Action <BotFrameworkConfigurationBuilder> configurer)
        {
            var options        = new BotFrameworkOptions();
            var optionsBuilder = new BotFrameworkConfigurationBuilder(options);

            configurer(optionsBuilder);

            ConfigureBotRoutes(BuildAdapter());

            return(httpConfiguration);

            BotFrameworkAdapter BuildAdapter()
            {
                var adapter = new BotFrameworkAdapter(options.CredentialProvider);

                foreach (var middleware in options.Middleware)
                {
                    adapter.Use(middleware);
                }

                return(adapter);
            }

            void ConfigureBotRoutes(BotFrameworkAdapter adapter)
            {
                var routes  = httpConfiguration.Routes;
                var baseUrl = options.Paths.BasePath;

                if (!baseUrl.EndsWith("/"))
                {
                    baseUrl += "/";
                }

                if (options.EnableProactiveMessages)
                {
                    routes.MapHttpRoute(
                        "BotFramework - Proactive Message Handler",
                        baseUrl + options.Paths.ProactiveMessagesPath,
                        defaults: null,
                        constraints: null,
                        handler: new BotProactiveMessageHandler(adapter));
                }

                routes.MapHttpRoute(
                    "BotFramework - Message Handler",
                    baseUrl + options.Paths.MessagesPath,
                    defaults: null,
                    constraints: null,
                    handler: new BotMessageHandler(adapter));
            }
        }
Пример #5
0
        private static void ConfigureBotRoutes(HttpConfiguration httpConfiguration, BotFrameworkOptions options, IAdapterIntegration adapter)
        {
            var routes  = httpConfiguration.Routes;
            var baseUrl = options.Paths.BasePath;

            routes.MapHttpRoute(
                BotMessageHandler.RouteName,
                baseUrl.Trim('/') + "/" + options.Paths.MessagesPath.Trim('/'),
                defaults: null,
                constraints: null,
#pragma warning disable CA2000 // Dispose objects before losing scope (we will let ASP.Net core deal with disposing this handler)
                handler: new BotMessageHandler(adapter));
#pragma warning restore CA2000 // Dispose objects before losing scope
        }
Пример #6
0
        /// <summary>
        /// Map the Bot Framework into the request execution pipeline.
        /// </summary>
        /// <param name="httpConfiguration">The <see cref="HttpConfiguration" /> to map the bot into.</param>
        /// <param name="configurer">A callback to configure the bot.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static HttpConfiguration MapBotFramework(this HttpConfiguration httpConfiguration, Action <BotFrameworkConfigurationBuilder> configurer = null)
        {
            if (httpConfiguration == null)
            {
                throw new ArgumentNullException(nameof(httpConfiguration));
            }

            var options        = new BotFrameworkOptions();
            var optionsBuilder = new BotFrameworkConfigurationBuilder(options);

            configurer?.Invoke(optionsBuilder);

            var adapter = httpConfiguration.DependencyResolver.GetService(typeof(IAdapterIntegration)) as IAdapterIntegration;

            if (adapter == null)
            {
                BotFrameworkAdapter botFrameworkAdapter;

                if (options.AppCredentials != null)
                {
                    botFrameworkAdapter = new BotFrameworkAdapter(options.AppCredentials, options.AuthenticationConfiguration, options.ChannelProvider, options.ConnectorClientRetryPolicy, options.HttpClient);
                }
                else
                {
                    var credentialProvider = ResolveCredentialProvider(options);
                    botFrameworkAdapter = new BotFrameworkAdapter(credentialProvider, options.AuthenticationConfiguration, options.ChannelProvider, options.ConnectorClientRetryPolicy, options.HttpClient);
                }

                // error handler
                botFrameworkAdapter.OnTurnError = options.OnTurnError;

                // add middleware
                foreach (var middleware in options.Middleware)
                {
                    botFrameworkAdapter.Use(middleware);
                }

                adapter = botFrameworkAdapter;
            }

            ConfigureBotRoutes(httpConfiguration, options, adapter);

            ConfigureCustomEndpoints();

            return(httpConfiguration);
        }
 public BotFrameworkConfigurationBuilder(BotFrameworkOptions botFrameworkOptions)
 {
     _options = botFrameworkOptions;
 }
        /// <summary>
        /// Map the Bot Framework into the request execution pipeline.
        /// </summary>
        /// <param name="httpConfiguration">The <see cref="HttpConfiguration" /> to map the bot into.</param>
        /// <param name="configurer">A callback to configure the bot.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static HttpConfiguration MapBotFramework(this HttpConfiguration httpConfiguration, Action <BotFrameworkConfigurationBuilder> configurer = null)
        {
            if (httpConfiguration == null)
            {
                throw new ArgumentNullException(nameof(httpConfiguration));
            }

            var options        = new BotFrameworkOptions();
            var optionsBuilder = new BotFrameworkConfigurationBuilder(options);

            configurer?.Invoke(optionsBuilder);

            var botFrameworkAdapter = GetOrCreateBotFrameworkAdapter();

            ConfigureMiddleware(botFrameworkAdapter);
            ConfigureBotRoutes(botFrameworkAdapter);

            return(httpConfiguration);

            BotFrameworkAdapter GetOrCreateBotFrameworkAdapter()
            {
                if (!(httpConfiguration.DependencyResolver.GetService(typeof(BotFrameworkAdapter)) is BotFrameworkAdapter adapter))
                {
                    var credentialProvider = ResolveCredentialProvider();

                    adapter = new BotFrameworkAdapter(credentialProvider, options.ConnectorClientRetryPolicy, options.HttpClient);
                }

                return(adapter);
            }

            void ConfigureMiddleware(BotFrameworkAdapter adapter)
            {
                foreach (var middleware in options.Middleware)
                {
                    adapter.Use(middleware);
                }
            }

            void ConfigureBotRoutes(BotFrameworkAdapter adapter)
            {
                var routes  = httpConfiguration.Routes;
                var baseUrl = options.Paths.BasePath;

                if (!baseUrl.EndsWith("/"))
                {
                    baseUrl += "/";
                }

                if (options.EnableProactiveMessages)
                {
                    routes.MapHttpRoute(
                        BotProactiveMessageHandler.RouteName,
                        baseUrl + options.Paths.ProactiveMessagesPath,
                        defaults: null,
                        constraints: null,
                        handler: new BotProactiveMessageHandler(adapter));
                }

                routes.MapHttpRoute(
                    BotMessageHandler.RouteName,
                    baseUrl + options.Paths.MessagesPath,
                    defaults: null,
                    constraints: null,
                    handler: new BotMessageHandler(adapter));
            }

            ICredentialProvider ResolveCredentialProvider()
            {
                var credentialProvider = options.CredentialProvider;

                // If a credential provider was explicitly configured, just return that straight away
                if (credentialProvider != null)
                {
                    return(credentialProvider);
                }

                return(new SimpleCredentialProvider(ConfigurationManager.AppSettings[MicrosoftAppCredentials.MicrosoftAppIdKey], ConfigurationManager.AppSettings[MicrosoftAppCredentials.MicrosoftAppPasswordKey]));
            }
        }
Пример #9
0
 public BotFrameworkConfigurationBuilder()
 {
     _options = new BotFrameworkOptions();
 }