microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/AspNetCorePlugin.cs
241lines · 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.Recipient = reference.User; |
| 93 | activity.ChannelId = reference.ChannelId; |
| 94 | |
| 95 | if (activity.Id is not null && !activity.IsStreaming) |
| 96 | { |
| 97 | await client |
| 98 | .Conversations |
| 99 | .Activities |
| 100 | .UpdateAsync(reference.Conversation.Id, activity.Id, activity); |
| 101 | |
| 102 | return activity; |
| 103 | } |
| 104 | |
| 105 | var res = await client |
| 106 | .Conversations |
| 107 | .Activities |
| 108 | .CreateAsync(reference.Conversation.Id, activity); |
| 109 | |
| 110 | activity.Id = res?.Id; |
| 111 | return activity; |
| 112 | } |
| 113 | |
| 114 | public IStreamer CreateStream(Api.ConversationReference reference, CancellationToken cancellationToken = default) |
| 115 | { |
| 116 | return new Stream() |
| 117 | { |
| 118 | Send = async activity => |
| 119 | { |
| 120 | var res = await Send(activity, reference, cancellationToken); |
| 121 | return res; |
| 122 | } |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | public async Task<Response> Do(ActivityEvent @event, CancellationToken cancellationToken = default) |
| 127 | { |
| 128 | try |
| 129 | { |
| 130 | var @out = await Events( |
| 131 | this, |
| 132 | "activity", |
| 133 | @event, |
| 134 | cancellationToken |
| 135 | ); |
| 136 | |
| 137 | var res = (Response?)@out ?? throw new Exception("expected activity response"); |
| 138 | Logger.Debug(res); |
| 139 | return res; |
| 140 | } |
| 141 | catch (Exception ex) |
| 142 | { |
| 143 | Logger.Error(ex); |
| 144 | await Events( |
| 145 | this, |
| 146 | "error", |
| 147 | new ErrorEvent() { Exception = ex }, |
| 148 | cancellationToken |
| 149 | ); |
| 150 | |
| 151 | return new Response(System.Net.HttpStatusCode.InternalServerError, ex.ToString()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | public async Task<IResult> Do(HttpContext httpContext, CancellationToken cancellationToken = default) |
| 156 | { |
| 157 | try |
| 158 | { |
| 159 | var request = httpContext.Request; |
| 160 | var token = ExtractToken(request); |
| 161 | var activity = await ParseActivity(request); |
| 162 | |
| 163 | if (activity is null) |
| 164 | { |
| 165 | return Results.BadRequest("Missing activity"); |
| 166 | } |
| 167 | |
| 168 | var data = new Dictionary<string, object?> |
| 169 | { |
| 170 | ["Request.TraceId"] = httpContext.TraceIdentifier |
| 171 | }; |
| 172 | |
| 173 | foreach (var pair in httpContext.Items) |
| 174 | { |
| 175 | var key = pair.Key.ToString(); |
| 176 | |
| 177 | if (key is null) continue; |
| 178 | |
| 179 | data[key] = pair.Value; |
| 180 | } |
| 181 | |
| 182 | var res = await Do(new ActivityEvent() |
| 183 | { |
| 184 | Token = token, |
| 185 | Activity = activity, |
| 186 | Extra = data, |
| 187 | Services = httpContext.RequestServices |
| 188 | }, cancellationToken); |
| 189 | |
| 190 | // convert response metadata to headers |
| 191 | foreach (var (key, value) in res.Meta) |
| 192 | { |
| 193 | var str = value?.ToString(); |
| 194 | if (string.IsNullOrEmpty(str)) continue; |
| 195 | httpContext.Response.Headers.Append($"X-Teams-{char.ToUpper(key[0]) + key[1..]}", str); |
| 196 | } |
| 197 | |
| 198 | return Results.Json( |
| 199 | res.Body, |
| 200 | _jsonSerializerOptions, |
| 201 | contentType: null, |
| 202 | statusCode: (int)res.Status |
| 203 | ); |
| 204 | } |
| 205 | catch (Exception ex) |
| 206 | { |
| 207 | Logger.Error(ex); |
| 208 | await Events( |
| 209 | this, |
| 210 | "error", |
| 211 | new ErrorEvent() { Exception = ex }, |
| 212 | cancellationToken |
| 213 | ); |
| 214 | |
| 215 | return Results.Problem(detail: ex.Message, statusCode: 500); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | public JsonWebToken ExtractToken(HttpRequest httpRequest) |
| 220 | { |
| 221 | var authHeader = httpRequest.Headers.Authorization.FirstOrDefault() ?? throw new UnauthorizedAccessException(); |
| 222 | return new JsonWebToken(authHeader.Replace("Bearer ", "")); |
| 223 | } |
| 224 | |
| 225 | public async Task<Activity?> ParseActivity(HttpRequest httpRequest) |
| 226 | { |
| 227 | httpRequest.EnableBuffering(); |
| 228 | |
| 229 | if (httpRequest.Body.CanSeek) |
| 230 | { |
| 231 | // reset the stream position to the beginning in case it was read before |
| 232 | httpRequest.Body.Position = 0; |
| 233 | } |
| 234 | |
| 235 | using StreamReader sr = new(httpRequest.Body); |
| 236 | var body = await sr.ReadToEndAsync(); |
| 237 | Activity? activity = JsonSerializer.Deserialize<Activity>(body); |
| 238 | |
| 239 | return activity; |
| 240 | } |
| 241 | } |