microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
next/core

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

115lines · 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 the default TeamsBotApplication with configuration options.
41 /// </summary>
42 /// <param name="services">The service collection.</param>
43 /// <param name="configure">A delegate to configure <see cref="TeamsBotApplicationOptions"/>.</param>
44 /// <param name="sectionName">The configuration section name for AzureAd settings. Default is "AzureAd".</param>
45 /// <returns>The service collection for chaining.</returns>
46 public static IServiceCollection AddTeamsBotApplication(this IServiceCollection services, Action<TeamsBotApplicationOptions> configure, string sectionName = "AzureAd")
47 {
48 return AddTeamsBotApplication<TeamsBotApplication>(services, configure, sectionName);
49 }
50
51 /// <summary>
52 /// Adds a custom TeamsBotApplication
53 /// </summary>
54 /// <param name="services">The WebApplicationBuilder instance.</param>
55 /// <param name="sectionName">The configuration section name for AzureAd settings. Default is "AzureAd".</param>
56 /// <returns>The updated WebApplicationBuilder instance.</returns>
57 public static IServiceCollection AddTeamsBotApplication<TApp>(this IServiceCollection services, string sectionName = "AzureAd") where TApp : TeamsBotApplication
58 {
59 return AddTeamsBotApplication<TApp>(services, configure: null, sectionName);
60 }
61
62 /// <summary>
63 /// Adds a custom TeamsBotApplication with configuration options.
64 /// </summary>
65 /// <typeparam name="TApp">The custom TeamsBotApplication type.</typeparam>
66 /// <param name="services">The service collection.</param>
67 /// <param name="configure">A delegate to configure <see cref="TeamsBotApplicationOptions"/>. Can be null.</param>
68 /// <param name="sectionName">The configuration section name for AzureAd settings. Default is "AzureAd".</param>
69 /// <returns>The service collection for chaining.</returns>
70 public static IServiceCollection AddTeamsBotApplication<TApp>(this IServiceCollection services, Action<TeamsBotApplicationOptions>? configure, string sectionName = "AzureAd") where TApp : TeamsBotApplication
71 {
72 BotConfig botConfig = BotConfig.Resolve(services, sectionName);
73
74 services.AddBotClient<ApiClient>(nameof(ApiClient), botConfig);
75
76 // Register TeamsBotApplicationOptions
77 TeamsBotApplicationOptions teamsOptions = new();
78 configure?.Invoke(teamsOptions);
79 services.AddSingleton(teamsOptions);
80
81 services.AddBotApplication<TApp>(botConfig);
82 return services;
83 }
84
85 /// <summary>
86 /// Configures the TeamsBotApp
87 /// </summary>
88 /// <typeparam name="TApp"></typeparam>
89 /// <param name="endpoints"></param>
90 /// <param name="routePath"></param>
91 /// <returns></returns>
92 public static TApp UseTeamsBotApplication<TApp>(this IEndpointRouteBuilder endpoints,
93 string routePath = "api/messages")
94 where TApp : TeamsBotApplication
95 => endpoints.UseBotApplication<TApp>(routePath);
96
97 /// <summary>
98 /// Configures the default TeamsBotApplication
99 /// </summary>
100 /// <param name="endpoints"></param>
101 /// <param name="routePath"></param>
102 /// <returns></returns>
103 public static TeamsBotApplication UseTeamsBotApplication(this IEndpointRouteBuilder endpoints,
104 string routePath = "api/messages")
105 => endpoints.UseBotApplication<TeamsBotApplication>(routePath);
106
107 /// <summary>
108 /// Alias for backward compat
109 /// </summary>
110 /// <param name="endpoints"></param>
111 /// <param name="routePath"></param>
112 /// <returns></returns>
113 public static TeamsBotApplication UseTeams(this IEndpointRouteBuilder endpoints, string routePath = "api/messages")
114 => endpoints.UseBotApplication<TeamsBotApplication>(routePath);
115}
116