Пример #1
0
        public static HttpConfiguration MapAlexaBotFramework(this HttpConfiguration httpConfiguration, Action <AlexaBotConfigurationBuilder> configurer)
        {
            var options        = new AlexaBotOptions();
            var optionsBuilder = new AlexaBotConfigurationBuilder(options);

            configurer(optionsBuilder);

            ConfigureAlexaBotRoutes(BuildAdapter());

            return(httpConfiguration);

            AlexaAdapter BuildAdapter()
            {
                var adapter = new AlexaAdapter();

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

                return(adapter);
            }

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

                if (!baseUrl.StartsWith("/"))
                {
                    baseUrl = baseUrl.Substring(1, baseUrl.Length - 1);
                }

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

                routes.MapHttpRoute(
                    "Alexa Skill Requests Handler",
                    baseUrl + options.Paths.SkillRequestsPath,
                    defaults: null,
                    constraints: null,
                    handler: new AlexaRequestHandler(adapter, options.AlexaOptions));
            }
        }
        /// <summary>
        /// Initializes and adds a bot adapter to the HTTP request pipeline, using custom endpoint paths for the bot.
        /// </summary>
        /// <param name="applicationBuilder">The application builder for the ASP.NET application.</param>
        /// <param name="configurePaths">Allows you to modify the endpoints for the bot.</param>
        /// <returns>The updated application builder.</returns>
        /// <remarks>This method adds any middleware from the <see cref="AlexaBotOptions"/> provided in the
        /// <see cref="ServiceCollectionExtensions.AddBot{TBot}(IServiceCollection, Action{AlexaBotOptions})"/>
        /// method to the adapter.</remarks>
        public static IApplicationBuilder UseAlexa(this IApplicationBuilder applicationBuilder, Action <AlexaBotPaths> configurePaths)
        {
            if (applicationBuilder == null)
            {
                throw new ArgumentNullException(nameof(applicationBuilder));
            }

            if (configurePaths == null)
            {
                throw new ArgumentNullException(nameof(configurePaths));
            }

            var options = applicationBuilder.ApplicationServices.GetRequiredService <IOptions <AlexaBotOptions> >().Value;

            var alexaAdapter = new AlexaAdapter()
            {
                ShouldEndSessionByDefault          = options.AlexaOptions.ShouldEndSessionByDefault,
                ConvertBotBuilderCardsToAlexaCards = options.AlexaOptions.TryConvertFirstActivityAttachmentToAlexaCard
            };

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

            var paths = options.Paths;

            configurePaths(paths);

            if (!options.Paths.BasePath.EndsWith("/"))
            {
                options.Paths.BasePath += "/";
            }

            applicationBuilder.Map(
                $"{paths.BasePath}{paths.SkillRequestsPath}",
                botActivitiesAppBuilder => botActivitiesAppBuilder.Run(new AlexaRequestHandler(alexaAdapter, options.AlexaOptions).HandleAsync));

            return(applicationBuilder);
        }