Esempio n. 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);
        }
Esempio n. 2
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));
            }
        }
Esempio n. 3
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 static HttpConfiguration MapBotFramework(this HttpConfiguration httpConfiguration, Action <BotFrameworkConfigurationBuilder> configurer)
        {
            var optionsBuilder = new BotFrameworkConfigurationBuilder();

            configurer(optionsBuilder);

            var options = optionsBuilder.BotFrameworkOptions;

            ConfigureBotRoute(BuildAdapter());

            return(httpConfiguration);

            BotFrameworkAdapter BuildAdapter()
            {
                var adapter = new BotFrameworkAdapter(options.AppId, options.AppPassword);

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

                return(adapter);
            }

            void ConfigureBotRoute(BotFrameworkAdapter adapter)
            {
                var botActivitiesRouteUrl = options.RouteBaseUrl;

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

                botActivitiesRouteUrl += "activities";

                httpConfiguration.Routes.MapHttpRoute(
                    "BotFrameworkV4 Activities Controller",
                    botActivitiesRouteUrl,
                    defaults: null,
                    constraints: null,
                    handler: new BotActivitiesHandler(adapter));
            }
        }
 /// <summary>
 /// Configures the bot with the a single identity that will be used to authenticate requests made to the Bot Framework.
 /// </summary>
 /// <param name="builder">The <see cref="BotFrameworkConfigurationBuilder"/>.</param>
 /// <param name="applicationId">The application id that should be used to authenticate requests made to the Bot Framework.</param>
 /// <param name="applicationPassword">The application password that should be used to authenticate requests made to the Bot Framework.</param>
 /// <returns>A reference to this instance after the operation has completed.</returns>
 /// <seealso cref="ICredentialProvider"/>
 /// <seealso cref="SimpleCredentialProvider"/>
 public static BotFrameworkConfigurationBuilder UseMicrosoftApplicationIdentity(this BotFrameworkConfigurationBuilder builder, string applicationId, string applicationPassword) =>
 builder.UseCredentialProvider(new SimpleCredentialProvider(applicationId, applicationPassword));
        /// <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]));
            }
        }