microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/AspNetCorePlugin.cs
260lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | using System.Text.Json.Serialization; |
| 6 | |
| 7 | using Microsoft.AspNetCore.Builder; |
| 8 | using Microsoft.AspNetCore.Http; |
| 9 | using Microsoft.Teams.Api.Activities; |
| 10 | using Microsoft.Teams.Api.Auth; |
| 11 | using Microsoft.Teams.Api.Clients; |
| 12 | using Microsoft.Teams.Apps; |
| 13 | using Microsoft.Teams.Apps.Events; |
| 14 | using Microsoft.Teams.Apps.Plugins; |
| 15 | using Microsoft.Teams.Common.Http; |
| 16 | using Microsoft.Teams.Common.Logging; |
| 17 | |
| 18 | using HttpRequest = Microsoft.AspNetCore.Http.HttpRequest; |
| 19 | |
| 20 | namespace Microsoft.Teams.Plugins.AspNetCore; |
| 21 | |
| 22 | [Plugin] |
| 23 | public partial class AspNetCorePlugin : ISenderPlugin, IAspNetCorePlugin |
| 24 | { |
| 25 | [Dependency] |
| 26 | public ILogger Logger { get; set; } |
| 27 | |
| 28 | [Dependency("Token", optional: true)] |
| 29 | public IToken? Token { get; set; } |
| 30 | |
| 31 | [Dependency] |
| 32 | public IHttpClient Client { get; set; } |
| 33 | |
| 34 | public event EventFunction Events; |
| 35 | |
| 36 | private static readonly JsonSerializerOptions _jsonSerializerOptions = new() |
| 37 | { |
| 38 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull |
| 39 | }; |
| 40 | |
| 41 | public IApplicationBuilder Configure(IApplicationBuilder builder) |
| 42 | { |
| 43 | return builder; |
| 44 | } |
| 45 | |
| 46 | public Task OnInit(App app, CancellationToken cancellationToken = default) |
| 47 | { |
| 48 | return Task.CompletedTask; |
| 49 | } |
| 50 | |
| 51 | public Task OnStart(App app, CancellationToken cancellationToken = default) |
| 52 | { |
| 53 | Logger.Debug("OnStart"); |
| 54 | return Task.CompletedTask; |
| 55 | } |
| 56 | |
| 57 | public Task OnError(App app, IPlugin plugin, ErrorEvent @event, CancellationToken cancellationToken = default) |
| 58 | { |
| 59 | Logger.Debug("OnError"); |
| 60 | return Task.CompletedTask; |
| 61 | } |
| 62 | |
| 63 | public Task OnActivity(App app, ISenderPlugin sender, ActivityEvent @event, CancellationToken cancellationToken = default) |
| 64 | { |
| 65 | Logger.Debug("OnActivity"); |
| 66 | return Task.CompletedTask; |
| 67 | } |
| 68 | |
| 69 | public Task OnActivitySent(App app, ISenderPlugin sender, ActivitySentEvent @event, CancellationToken cancellationToken = default) |
| 70 | { |
| 71 | Logger.Debug("OnActivitySent"); |
| 72 | return Task.CompletedTask; |
| 73 | } |
| 74 | |
| 75 | public Task OnActivityResponse(App app, ISenderPlugin sender, ActivityResponseEvent @event, CancellationToken cancellationToken = default) |
| 76 | { |
| 77 | Logger.Debug("OnActivityResponse"); |
| 78 | return Task.CompletedTask; |
| 79 | } |
| 80 | |
| 81 | public Task<IActivity> Send(IActivity activity, Api.ConversationReference reference, CancellationToken cancellationToken = default) |
| 82 | { |
| 83 | return Send<IActivity>(activity, reference, cancellationToken); |
| 84 | } |
| 85 | |
| 86 | public async Task<TActivity> Send<TActivity>(TActivity activity, Api.ConversationReference reference, CancellationToken cancellationToken = default) where TActivity : IActivity |
| 87 | { |
| 88 | var client = new ApiClient(reference.ServiceUrl, Client, cancellationToken); |
| 89 | |
| 90 | activity.Conversation = reference.Conversation; |
| 91 | activity.From = reference.Bot; |
| 92 | activity.ChannelId = reference.ChannelId; |
| 93 | |
| 94 | // For targeted messages with an explicit Recipient (proactive sends), preserve it. |
| 95 | // Otherwise, use the reference User from the conversation context. |
| 96 | #pragma warning disable ExperimentalTeamsTargeted |
| 97 | var isTargeted = activity.Recipient?.IsTargeted == true; |
| 98 | |
| 99 | if (!isTargeted) |
| 100 | { |
| 101 | activity.Recipient = reference.User; |
| 102 | } |
| 103 | |
| 104 | if (activity.Id is not null && !activity.IsStreaming) |
| 105 | { |
| 106 | if (isTargeted) |
| 107 | { |
| 108 | await client |
| 109 | .Conversations |
| 110 | .Activities |
| 111 | .UpdateTargetedAsync(reference.Conversation.Id, activity.Id, activity); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | await client |
| 116 | .Conversations |
| 117 | .Activities |
| 118 | .UpdateAsync(reference.Conversation.Id, activity.Id, activity); |
| 119 | } |
| 120 | |
| 121 | return activity; |
| 122 | } |
| 123 | |
| 124 | var res = isTargeted |
| 125 | ? await client.Conversations.Activities.CreateTargetedAsync(reference.Conversation.Id, activity) |
| 126 | : await client.Conversations.Activities.CreateAsync(reference.Conversation.Id, activity); |
| 127 | #pragma warning restore ExperimentalTeamsTargeted |
| 128 | |
| 129 | activity.Id = res?.Id; |
| 130 | return activity; |
| 131 | } |
| 132 | |
| 133 | public IStreamer CreateStream(Api.ConversationReference reference, CancellationToken cancellationToken = default) |
| 134 | { |
| 135 | return new Stream() |
| 136 | { |
| 137 | Send = async activity => |
| 138 | { |
| 139 | var res = await Send(activity, reference, cancellationToken); |
| 140 | return res; |
| 141 | } |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | public async Task<Response> Do(ActivityEvent @event, CancellationToken cancellationToken = default) |
| 146 | { |
| 147 | try |
| 148 | { |
| 149 | var @out = await Events( |
| 150 | this, |
| 151 | "activity", |
| 152 | @event, |
| 153 | cancellationToken |
| 154 | ); |
| 155 | |
| 156 | var res = (Response?)@out ?? throw new Exception("expected activity response"); |
| 157 | Logger.Debug(res); |
| 158 | return res; |
| 159 | } |
| 160 | catch (Exception ex) |
| 161 | { |
| 162 | Logger.Error(ex); |
| 163 | await Events( |
| 164 | this, |
| 165 | "error", |
| 166 | new ErrorEvent() { Exception = ex }, |
| 167 | cancellationToken |
| 168 | ); |
| 169 | |
| 170 | return new Response(System.Net.HttpStatusCode.InternalServerError, ex.ToString()); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | public async Task<IResult> Do(HttpContext httpContext, CancellationToken cancellationToken = default) |
| 175 | { |
| 176 | try |
| 177 | { |
| 178 | var request = httpContext.Request; |
| 179 | var token = ExtractToken(request); |
| 180 | var activity = await ParseActivity(request); |
| 181 | |
| 182 | if (activity is null) |
| 183 | { |
| 184 | return Results.BadRequest("Missing activity"); |
| 185 | } |
| 186 | |
| 187 | var data = new Dictionary<string, object?> |
| 188 | { |
| 189 | ["Request.TraceId"] = httpContext.TraceIdentifier |
| 190 | }; |
| 191 | |
| 192 | foreach (var pair in httpContext.Items) |
| 193 | { |
| 194 | var key = pair.Key.ToString(); |
| 195 | |
| 196 | if (key is null) continue; |
| 197 | |
| 198 | data[key] = pair.Value; |
| 199 | } |
| 200 | |
| 201 | var res = await Do(new ActivityEvent() |
| 202 | { |
| 203 | Token = token, |
| 204 | Activity = activity, |
| 205 | Extra = data, |
| 206 | Services = httpContext.RequestServices |
| 207 | }, cancellationToken); |
| 208 | |
| 209 | // convert response metadata to headers |
| 210 | foreach (var (key, value) in res.Meta) |
| 211 | { |
| 212 | var str = value?.ToString(); |
| 213 | if (string.IsNullOrEmpty(str)) continue; |
| 214 | httpContext.Response.Headers.Append($"X-Teams-{char.ToUpper(key[0]) + key[1..]}", str); |
| 215 | } |
| 216 | |
| 217 | return Results.Json( |
| 218 | res.Body, |
| 219 | _jsonSerializerOptions, |
| 220 | contentType: null, |
| 221 | statusCode: (int)res.Status |
| 222 | ); |
| 223 | } |
| 224 | catch (Exception ex) |
| 225 | { |
| 226 | Logger.Error(ex); |
| 227 | await Events( |
| 228 | this, |
| 229 | "error", |
| 230 | new ErrorEvent() { Exception = ex }, |
| 231 | cancellationToken |
| 232 | ); |
| 233 | |
| 234 | return Results.Problem(detail: ex.Message, statusCode: 500); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | public JsonWebToken ExtractToken(HttpRequest httpRequest) |
| 239 | { |
| 240 | var authHeader = httpRequest.Headers.Authorization.FirstOrDefault() ?? throw new UnauthorizedAccessException(); |
| 241 | return new JsonWebToken(authHeader.Replace("Bearer ", "")); |
| 242 | } |
| 243 | |
| 244 | public async Task<Activity?> ParseActivity(HttpRequest httpRequest) |
| 245 | { |
| 246 | httpRequest.EnableBuffering(); |
| 247 | |
| 248 | if (httpRequest.Body.CanSeek) |
| 249 | { |
| 250 | // reset the stream position to the beginning in case it was read before |
| 251 | httpRequest.Body.Position = 0; |
| 252 | } |
| 253 | |
| 254 | using StreamReader sr = new(httpRequest.Body); |
| 255 | var body = await sr.ReadToEndAsync(); |
| 256 | Activity? activity = JsonSerializer.Deserialize<Activity>(body); |
| 257 | |
| 258 | return activity; |
| 259 | } |
| 260 | } |