microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/TeamsBotApplication.cs
89lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.AspNetCore.Http; |
| 5 | using Microsoft.Extensions.Logging; |
| 6 | using Microsoft.Teams.Bot.Apps.Api; |
| 7 | using Microsoft.Teams.Bot.Apps.Handlers; |
| 8 | using Microsoft.Teams.Bot.Apps.Routing; |
| 9 | using Microsoft.Teams.Bot.Apps.Schema; |
| 10 | using Microsoft.Teams.Bot.Core; |
| 11 | using Microsoft.Teams.Bot.Core.Hosting; |
| 12 | |
| 13 | namespace Microsoft.Teams.Bot.Apps; |
| 14 | |
| 15 | /// <summary> |
| 16 | /// Teams specific Bot Application |
| 17 | /// </summary> |
| 18 | public class TeamsBotApplication : BotApplication |
| 19 | { |
| 20 | private readonly TeamsApiClient _teamsApiClient; |
| 21 | |
| 22 | /// <summary> |
| 23 | /// Gets the router for dispatching Teams activities to registered routes. |
| 24 | /// </summary> |
| 25 | internal Router Router { get; } |
| 26 | |
| 27 | /// <summary> |
| 28 | /// Gets the client used to interact with the Teams API service. |
| 29 | /// </summary> |
| 30 | public TeamsApiClient TeamsApiClient => _teamsApiClient; |
| 31 | /// <summary> |
| 32 | /// Gets the hierarchical API facade for Teams operations. |
| 33 | /// </summary> |
| 34 | /// <remarks> |
| 35 | /// This property provides a structured API for accessing Teams operations through a hierarchy: |
| 36 | /// <list type="bullet"> |
| 37 | /// <item><c>Api.Conversations.Activities</c> - Activity operations (send, update, delete)</item> |
| 38 | /// <item><c>Api.Conversations.Members</c> - Member operations (get, delete)</item> |
| 39 | /// <item><c>Api.Users.Token</c> - User token operations (OAuth SSO, sign-in resources)</item> |
| 40 | /// <item><c>Api.Teams</c> - Team operations (get details, channels)</item> |
| 41 | /// <item><c>Api.Meetings</c> - Meeting operations (get info, participant, notifications)</item> |
| 42 | /// <item><c>Api.Batch</c> - Batch messaging operations</item> |
| 43 | /// </list> |
| 44 | /// </remarks> |
| 45 | public TeamsApi Api { get; } |
| 46 | |
| 47 | /// <param name="conversationClient"></param> |
| 48 | /// <param name="userTokenClient"></param> |
| 49 | /// <param name="teamsApiClient"></param> |
| 50 | /// <param name="httpContextAccessor"></param> |
| 51 | /// <param name="logger"></param> |
| 52 | /// <param name="options">Options containing the application (client) ID, used for logging and diagnostics. Defaults to an empty instance if not provided.</param> |
| 53 | public TeamsBotApplication( |
| 54 | ConversationClient conversationClient, |
| 55 | UserTokenClient userTokenClient, |
| 56 | TeamsApiClient teamsApiClient, |
| 57 | IHttpContextAccessor httpContextAccessor, |
| 58 | ILogger<TeamsBotApplication> logger, |
| 59 | BotApplicationOptions? options = null) |
| 60 | : base(conversationClient, userTokenClient, logger, options) |
| 61 | { |
| 62 | _teamsApiClient = teamsApiClient; |
| 63 | Api = new TeamsApi(conversationClient, userTokenClient, teamsApiClient); |
| 64 | Router = new Router(logger); |
| 65 | OnActivity = async (activity, cancellationToken) => |
| 66 | { |
| 67 | logger.LogInformation("OnActivity invoked for activity: Id={Id}", activity.Id); |
| 68 | TeamsActivity teamsActivity = TeamsActivity.FromActivity(activity); |
| 69 | Context<TeamsActivity> defaultContext = new(this, teamsActivity); |
| 70 | |
| 71 | if (teamsActivity.Type != TeamsActivityType.Invoke) |
| 72 | { |
| 73 | await Router.DispatchAsync(defaultContext, cancellationToken).ConfigureAwait(false); |
| 74 | } |
| 75 | else // invokes |
| 76 | { |
| 77 | InvokeResponse invokeResponse = await Router.DispatchWithReturnAsync(defaultContext, cancellationToken).ConfigureAwait(false); |
| 78 | HttpContext? httpContext = httpContextAccessor.HttpContext; |
| 79 | if (httpContext is not null && invokeResponse is not null) |
| 80 | { |
| 81 | httpContext.Response.StatusCode = invokeResponse.Status; |
| 82 | logger.LogTrace("Sending invoke response with status {Status} and Body {Body}", invokeResponse.Status, invokeResponse.Body); |
| 83 | if (invokeResponse.Body is not null) |
| 84 | await httpContext.Response.WriteAsJsonAsync(invokeResponse.Body, cancellationToken).ConfigureAwait(false); |
| 85 | } |
| 86 | } |
| 87 | }; |
| 88 | } |
| 89 | } |
| 90 | |