microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
next/core-compat-sso-middleware

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Bot.Apps/TeamsBotApplication.HostingExtensions.cs

85lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using Microsoft.AspNetCore.Routing;
5using Microsoft.Extensions.DependencyInjection;
6using Microsoft.Teams.Bot.Apps.Api.Clients;
7using Microsoft.Teams.Bot.Core.Hosting;
8
9namespace Microsoft.Teams.Bot.Apps;
10
11/// <summary>
12/// Extension methods for <see cref="TeamsBotApplication"/>.
13/// </summary>
14public 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