microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/TeamsBotApplication.cs
71lines · 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.Routing; |
| 7 | using Microsoft.Teams.Bot.Apps.Schema; |
| 8 | using Microsoft.Teams.Bot.Core; |
| 9 | using Microsoft.Teams.Bot.Core.Hosting; |
| 10 | |
| 11 | namespace Microsoft.Teams.Bot.Apps; |
| 12 | |
| 13 | /// <summary> |
| 14 | /// Teams specific Bot Application |
| 15 | /// </summary> |
| 16 | public class TeamsBotApplication : BotApplication |
| 17 | { |
| 18 | private readonly TeamsApiClient _teamsApiClient; |
| 19 | |
| 20 | /// <summary> |
| 21 | /// Gets the router for dispatching Teams activities to registered routes. |
| 22 | /// </summary> |
| 23 | internal Router Router { get; } |
| 24 | |
| 25 | /// <summary> |
| 26 | /// Gets the client used to interact with the Teams API service. |
| 27 | /// </summary> |
| 28 | public TeamsApiClient TeamsApiClient => _teamsApiClient; |
| 29 | |
| 30 | /// <param name="conversationClient"></param> |
| 31 | /// <param name="userTokenClient"></param> |
| 32 | /// <param name="teamsApiClient"></param> |
| 33 | /// <param name="httpContextAccessor"></param> |
| 34 | /// <param name="logger"></param> |
| 35 | /// <param name="options">Options containing the application (client) ID, used for logging and diagnostics. Defaults to an empty instance if not provided.</param> |
| 36 | public TeamsBotApplication( |
| 37 | ConversationClient conversationClient, |
| 38 | UserTokenClient userTokenClient, |
| 39 | TeamsApiClient teamsApiClient, |
| 40 | IHttpContextAccessor httpContextAccessor, |
| 41 | ILogger<TeamsBotApplication> logger, |
| 42 | BotApplicationOptions? options = null) |
| 43 | : base(conversationClient, userTokenClient, logger, options) |
| 44 | { |
| 45 | _teamsApiClient = teamsApiClient; |
| 46 | Router = new Router(logger); |
| 47 | OnActivity = async (activity, cancellationToken) => |
| 48 | { |
| 49 | logger.LogInformation("OnActivity invoked for activity: Id={Id}", activity.Id); |
| 50 | TeamsActivity teamsActivity = TeamsActivity.FromActivity(activity); |
| 51 | Context<TeamsActivity> defaultContext = new(this, teamsActivity); |
| 52 | |
| 53 | if (teamsActivity.Type != TeamsActivityType.Invoke) |
| 54 | { |
| 55 | await Router.DispatchAsync(defaultContext, cancellationToken).ConfigureAwait(false); |
| 56 | } |
| 57 | else // invokes |
| 58 | { |
| 59 | InvokeResponse invokeResponse = await Router.DispatchWithReturnAsync(defaultContext, cancellationToken).ConfigureAwait(false); |
| 60 | HttpContext? httpContext = httpContextAccessor.HttpContext; |
| 61 | if (httpContext is not null && invokeResponse is not null) |
| 62 | { |
| 63 | httpContext.Response.StatusCode = invokeResponse.Status; |
| 64 | logger.LogTrace("Sending invoke response with status {Status} and Body {Body}", invokeResponse.Status, invokeResponse.Body); |
| 65 | await httpContext.Response.WriteAsJsonAsync(invokeResponse.Body, cancellationToken).ConfigureAwait(false); |
| 66 | |
| 67 | } |
| 68 | } |
| 69 | }; |
| 70 | } |
| 71 | } |
| 72 | |