microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/TeamsBotApplication.HostingExtensions.cs
85lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.AspNetCore.Routing; |
| 5 | using Microsoft.Extensions.DependencyInjection; |
| 6 | using Microsoft.Teams.Bot.Apps.Api.Clients; |
| 7 | using Microsoft.Teams.Bot.Core.Hosting; |
| 8 | |
| 9 | namespace Microsoft.Teams.Bot.Apps; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// Extension methods for <see cref="TeamsBotApplication"/>. |
| 13 | /// </summary> |
| 14 | public static class TeamsBotApplicationHostingExtensions |
| 15 | { |
| 16 | /// <summary> |
| 17 | /// Registers Teams bot application services with the specified service collection. |
| 18 | /// </summary> |
| 19 | /// <remarks>This method provides a simplified way to configure Teams bot support by encapsulating the |
| 20 | /// necessary service registrations and configuration binding.</remarks> |
| 21 | /// <param name="services">The service collection to which Teams bot application services will be added. Cannot be null.</param> |
| 22 | /// <param name="sectionName">The name of the configuration section containing Azure Active Directory settings. Defaults to "AzureAd" if not |
| 23 | /// specified.</param> |
| 24 | /// <returns>The service collection with Teams bot application services registered.</returns> |
| 25 | public static IServiceCollection AddTeams(this IServiceCollection services, string sectionName = "AzureAd") |
| 26 | => AddTeamsBotApplication(services, sectionName); |
| 27 | |
| 28 | /// <summary> |
| 29 | /// Adds the Default TeamsBotApplication |
| 30 | /// </summary> |
| 31 | /// <param name="services"></param> |
| 32 | /// <param name="sectionName"></param> |
| 33 | /// <returns></returns> |
| 34 | public static IServiceCollection AddTeamsBotApplication(this IServiceCollection services, string sectionName = "AzureAd") |
| 35 | { |
| 36 | return AddTeamsBotApplication<TeamsBotApplication>(services, sectionName); |
| 37 | } |
| 38 | |
| 39 | /// <summary> |
| 40 | /// Adds a custom TeamsBotApplication |
| 41 | /// </summary> |
| 42 | /// <param name="services">The WebApplicationBuilder instance.</param> |
| 43 | /// <param name="sectionName">The configuration section name for AzureAd settings. Default is "AzureAd".</param> |
| 44 | /// <returns>The updated WebApplicationBuilder instance.</returns> |
| 45 | public static IServiceCollection AddTeamsBotApplication<TApp>(this IServiceCollection services, string sectionName = "AzureAd") where TApp : TeamsBotApplication |
| 46 | { |
| 47 | BotConfig botConfig = BotConfig.Resolve(services, sectionName); |
| 48 | |
| 49 | services.AddBotClient<ApiClient>(nameof(ApiClient), botConfig); |
| 50 | |
| 51 | services.AddBotApplication<TApp>(botConfig); |
| 52 | return services; |
| 53 | } |
| 54 | |
| 55 | /// <summary> |
| 56 | /// Configures the TeamsBotApp |
| 57 | /// </summary> |
| 58 | /// <typeparam name="TApp"></typeparam> |
| 59 | /// <param name="endpoints"></param> |
| 60 | /// <param name="routePath"></param> |
| 61 | /// <returns></returns> |
| 62 | public static TApp UseTeamsBotApplication<TApp>(this IEndpointRouteBuilder endpoints, |
| 63 | string routePath = "api/messages") |
| 64 | where TApp : TeamsBotApplication |
| 65 | => endpoints.UseBotApplication<TApp>(routePath); |
| 66 | |
| 67 | /// <summary> |
| 68 | /// Configures the default TeamsBotApplication |
| 69 | /// </summary> |
| 70 | /// <param name="endpoints"></param> |
| 71 | /// <param name="routePath"></param> |
| 72 | /// <returns></returns> |
| 73 | public static TeamsBotApplication UseTeamsBotApplication(this IEndpointRouteBuilder endpoints, |
| 74 | string routePath = "api/messages") |
| 75 | => endpoints.UseBotApplication<TeamsBotApplication>(routePath); |
| 76 | |
| 77 | /// <summary> |
| 78 | /// Alias for backward compat |
| 79 | /// </summary> |
| 80 | /// <param name="endpoints"></param> |
| 81 | /// <param name="routePath"></param> |
| 82 | /// <returns></returns> |
| 83 | public static TeamsBotApplication UseTeams(this IEndpointRouteBuilder endpoints, string routePath = "api/messages") |
| 84 | => endpoints.UseBotApplication<TeamsBotApplication>(routePath); |
| 85 | } |
| 86 | |