microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Apps/Context.cs
331lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Diagnostics.CodeAnalysis; |
| 5 | using Microsoft.Extensions.Logging; |
| 6 | using Microsoft.Teams.Apps.Api.Clients; |
| 7 | using Microsoft.Teams.Apps.OAuth; |
| 8 | using Microsoft.Teams.Apps.Schema; |
| 9 | using Microsoft.Teams.Apps.Schema.Entities; |
| 10 | using Microsoft.Teams.Core; |
| 11 | |
| 12 | namespace Microsoft.Teams.Apps; |
| 13 | |
| 14 | |
| 15 | /// <summary> |
| 16 | /// Context for a bot turn. |
| 17 | /// </summary> |
| 18 | /// <param name="botApplication">The bot application instance that owns this context.</param> |
| 19 | /// <param name="activity">The incoming activity for this turn.</param> |
| 20 | public class Context<TActivity>(TeamsBotApplication botApplication, TActivity activity) where TActivity : TeamsActivity |
| 21 | { |
| 22 | /// <summary> |
| 23 | /// Base bot application. |
| 24 | /// </summary> |
| 25 | public TeamsBotApplication TeamsBotApplication { get; } = botApplication; |
| 26 | |
| 27 | /// <summary> |
| 28 | /// Current activity. |
| 29 | /// </summary> |
| 30 | public TActivity Activity { get; } = activity; |
| 31 | |
| 32 | /// <summary> |
| 33 | /// Gets the application (client) ID configured for this bot. |
| 34 | /// </summary> |
| 35 | public string AppId => TeamsBotApplication.AppId; |
| 36 | |
| 37 | private ContextLogger? _log; |
| 38 | |
| 39 | /// <summary> |
| 40 | /// Gets the logger for this context, providing <c>.Info()</c>, <c>.Error()</c>, <c>.Debug()</c>, |
| 41 | /// and <c>.Warn()</c> convenience methods that delegate to the underlying <see cref="ILogger"/>. |
| 42 | /// </summary> |
| 43 | public ContextLogger Log => _log ??= new ContextLogger(TeamsBotApplication.Logger); |
| 44 | |
| 45 | private ApiClient? _api; |
| 46 | |
| 47 | /// <summary> |
| 48 | /// Gets the <see cref="ApiClient"/> scoped to the current activity's service URL. |
| 49 | /// </summary> |
| 50 | public ApiClient Api => _api ??= TeamsBotApplication.Api.ForServiceUrl( |
| 51 | Activity.ServiceUrl ?? throw new InvalidOperationException("Activity.ServiceUrl is required to use the Api client.")); |
| 52 | |
| 53 | // ==================== Convenience Send/Reply/Typing ==================== |
| 54 | |
| 55 | /// <summary> |
| 56 | /// Sends a text message to the conversation. |
| 57 | /// </summary> |
| 58 | /// <param name="text">The text to send.</param> |
| 59 | /// <param name="cancellationToken">A cancellation token.</param> |
| 60 | /// <returns>The response from the send operation.</returns> |
| 61 | public Task<SendActivityResponse?> SendAsync(string text, CancellationToken cancellationToken = default) |
| 62 | => SendActivityAsync(text, cancellationToken); |
| 63 | |
| 64 | /// <summary> |
| 65 | /// Sends an activity to the conversation. |
| 66 | /// </summary> |
| 67 | /// <param name="activity">The activity to send.</param> |
| 68 | /// <param name="cancellationToken">A cancellation token.</param> |
| 69 | /// <returns>The response from the send operation.</returns> |
| 70 | public Task<SendActivityResponse?> SendAsync(TeamsActivity activity, CancellationToken cancellationToken = default) |
| 71 | => SendActivityAsync(activity, cancellationToken); |
| 72 | |
| 73 | /// <summary> |
| 74 | /// Sends a text message as a threaded reply to the current activity. When the inbound activity |
| 75 | /// has an id, the response auto-quotes it (rendered as a quote bubble above the response in Teams); |
| 76 | /// otherwise sends without quoting. |
| 77 | /// </summary> |
| 78 | /// <param name="text">The text to send.</param> |
| 79 | /// <param name="cancellationToken">A cancellation token.</param> |
| 80 | /// <returns>The response from the send operation.</returns> |
| 81 | public Task<SendActivityResponse?> ReplyAsync(string text, CancellationToken cancellationToken = default) |
| 82 | => ReplyAsync(new MessageActivity(text), cancellationToken); |
| 83 | |
| 84 | /// <summary> |
| 85 | /// Sends an activity to the conversation. When the inbound activity has an id, the response |
| 86 | /// auto-quotes it (rendered as a quote bubble above the response in Teams). Otherwise sends |
| 87 | /// without quoting. To send without quoting unconditionally, use <see cref="Send(TeamsActivity, CancellationToken)"/>. |
| 88 | /// </summary> |
| 89 | /// <param name="activity">The activity to send.</param> |
| 90 | /// <param name="cancellationToken">A cancellation token.</param> |
| 91 | /// <returns>The response from the send operation.</returns> |
| 92 | public Task<SendActivityResponse?> ReplyAsync(TeamsActivity activity, CancellationToken cancellationToken = default) |
| 93 | { |
| 94 | ArgumentNullException.ThrowIfNull(activity); |
| 95 | if (!string.IsNullOrWhiteSpace(Activity.Id)) |
| 96 | { |
| 97 | return Quote(Activity.Id, activity, cancellationToken); |
| 98 | } |
| 99 | |
| 100 | return SendActivityAsync(activity, cancellationToken); |
| 101 | } |
| 102 | |
| 103 | /// <summary> |
| 104 | /// Sends a typing indicator to the conversation. |
| 105 | /// </summary> |
| 106 | /// <param name="cancellationToken">A cancellation token.</param> |
| 107 | /// <returns>The response from the send operation.</returns> |
| 108 | public Task<SendActivityResponse?> TypingAsync(CancellationToken cancellationToken = default) |
| 109 | => SendTypingActivityAsync(cancellationToken); |
| 110 | |
| 111 | /// <summary> |
| 112 | /// Send a message to the conversation with a quoted message reference prepended to the text. |
| 113 | /// Teams renders the quoted message as a preview bubble above the response text. |
| 114 | /// </summary> |
| 115 | /// <param name="messageId">The ID of the message to quote.</param> |
| 116 | /// <param name="text">The response text, appended to the quoted message placeholder.</param> |
| 117 | /// <param name="cancellationToken">Optional cancellation token.</param> |
| 118 | /// <returns>The response from sending the activity.</returns> |
| 119 | [Experimental("ExperimentalTeamsQuotedReplies")] |
| 120 | public Task<SendActivityResponse?> Quote(string messageId, string text, CancellationToken cancellationToken = default) |
| 121 | => Quote(messageId, new MessageActivity(text), cancellationToken); |
| 122 | |
| 123 | /// <summary> |
| 124 | /// Send a message to the conversation with a quoted message reference prepended to the text. |
| 125 | /// Teams renders the quoted message as a preview bubble above the response text. |
| 126 | /// </summary> |
| 127 | /// <param name="messageId">The ID of the message to quote.</param> |
| 128 | /// <param name="activity">The activity to send. For <see cref="MessageActivity"/>, a quote placeholder for messageId is prepended to its text. Other activity types are sent as-is without quoting.</param> |
| 129 | /// <param name="cancellationToken">Optional cancellation token.</param> |
| 130 | /// <returns>The response from sending the activity.</returns> |
| 131 | [Experimental("ExperimentalTeamsQuotedReplies")] |
| 132 | public Task<SendActivityResponse?> Quote(string messageId, TeamsActivity activity, CancellationToken cancellationToken = default) |
| 133 | { |
| 134 | ArgumentNullException.ThrowIfNull(activity); |
| 135 | ArgumentException.ThrowIfNullOrWhiteSpace(messageId); |
| 136 | if (activity is MessageActivity message) |
| 137 | { |
| 138 | message.PrependQuote(messageId); |
| 139 | } |
| 140 | return SendActivityAsync(activity, cancellationToken); |
| 141 | } |
| 142 | |
| 143 | /// <inheritdoc cref="SendAsync(string, CancellationToken)"/> |
| 144 | public Task<SendActivityResponse?> Send(string text, CancellationToken cancellationToken = default) |
| 145 | => SendAsync(text, cancellationToken); |
| 146 | |
| 147 | /// <inheritdoc cref="SendAsync(TeamsActivity, CancellationToken)"/> |
| 148 | public Task<SendActivityResponse?> Send(TeamsActivity activity, CancellationToken cancellationToken = default) |
| 149 | => SendAsync(activity, cancellationToken); |
| 150 | |
| 151 | /// <inheritdoc cref="ReplyAsync(string, CancellationToken)"/> |
| 152 | public Task<SendActivityResponse?> Reply(string text, CancellationToken cancellationToken = default) |
| 153 | => ReplyAsync(text, cancellationToken); |
| 154 | |
| 155 | /// <inheritdoc cref="ReplyAsync(TeamsActivity, CancellationToken)"/> |
| 156 | public Task<SendActivityResponse?> Reply(TeamsActivity activity, CancellationToken cancellationToken = default) |
| 157 | => ReplyAsync(activity, cancellationToken); |
| 158 | |
| 159 | /// <inheritdoc cref="TypingAsync(CancellationToken)"/> |
| 160 | public Task<SendActivityResponse?> Typing(CancellationToken cancellationToken = default) |
| 161 | => TypingAsync(cancellationToken); |
| 162 | |
| 163 | // ==================== Core Send Methods ==================== |
| 164 | |
| 165 | /// <summary> |
| 166 | /// Sends a message activity as a reply. |
| 167 | /// </summary> |
| 168 | /// <param name="text">The text to send.</param> |
| 169 | /// <param name="cancellationToken">A cancellation token.</param> |
| 170 | /// <returns>The response from the send operation.</returns> |
| 171 | public Task<SendActivityResponse?> SendActivityAsync(string text, CancellationToken cancellationToken = default) |
| 172 | => SendActivityAsync(new MessageActivity(text) { TextFormat = TextFormats.Plain }, cancellationToken); |
| 173 | |
| 174 | /// <summary> |
| 175 | /// Sends an activity to the conversation. |
| 176 | /// </summary> |
| 177 | /// <param name="activity">The activity to send.</param> |
| 178 | /// <param name="cancellationToken">A cancellation token.</param> |
| 179 | /// <returns>The response from the send operation.</returns> |
| 180 | public Task<SendActivityResponse?> SendActivityAsync(TeamsActivity activity, CancellationToken cancellationToken = default) |
| 181 | { |
| 182 | ArgumentNullException.ThrowIfNull(activity); |
| 183 | |
| 184 | bool isTargeted = activity.Recipient?.IsTargeted == true; |
| 185 | |
| 186 | if (isTargeted && Activity.Conversation?.ConversationType == ConversationType.Personal) |
| 187 | { |
| 188 | throw new InvalidOperationException( |
| 189 | "Targeted messages are not supported in personal (1:1) chats."); |
| 190 | } |
| 191 | |
| 192 | if (activity.Type == TeamsActivityType.Message |
| 193 | && Activity.Recipient?.IsTargeted == true |
| 194 | && Activity.Id is not null) |
| 195 | { |
| 196 | TargetedMessageInfoEntityExtensions.AddToActivity(activity, Activity.Id); |
| 197 | } |
| 198 | |
| 199 | TeamsActivity reply = new TeamsActivityBuilder(activity) |
| 200 | .WithConversationReference(Activity) |
| 201 | .Build(); |
| 202 | return TeamsBotApplication.SendActivityAsync(reply, cancellationToken: cancellationToken); |
| 203 | } |
| 204 | |
| 205 | /// <summary> |
| 206 | /// Sends a typing activity to the conversation asynchronously. |
| 207 | /// </summary> |
| 208 | /// <param name="cancellationToken">A cancellation token.</param> |
| 209 | /// <returns>The response from the send operation.</returns> |
| 210 | public Task<SendActivityResponse?> SendTypingActivityAsync(CancellationToken cancellationToken = default) |
| 211 | { |
| 212 | TeamsActivity reply = new TeamsActivityBuilder() |
| 213 | .WithType(TeamsActivityType.Typing) |
| 214 | .WithConversationReference(Activity) |
| 215 | .Build(); |
| 216 | return TeamsBotApplication.SendActivityAsync(reply, cancellationToken: cancellationToken); |
| 217 | } |
| 218 | |
| 219 | // ==================== OAuth Sign-In ==================== |
| 220 | |
| 221 | /// <summary> |
| 222 | /// Trigger user OAuth sign-in flow for the activity sender. |
| 223 | /// Attempts silent token acquisition first; if no token is cached, sends an OAuthCard. |
| 224 | /// </summary> |
| 225 | /// <param name="options">OAuth options including connection name and card text.</param> |
| 226 | /// <param name="cancellationToken">A cancellation token.</param> |
| 227 | /// <returns>The existing user token if found, or null if the sign-in flow was initiated.</returns> |
| 228 | public Task<string?> SignInAsync(OAuthOptions? options = null, CancellationToken cancellationToken = default) |
| 229 | { |
| 230 | OAuthFlow flow = ResolveOAuthFlow(options?.ConnectionName); |
| 231 | return flow.SignInAsync(this, options, cancellationToken); |
| 232 | } |
| 233 | |
| 234 | /// <summary> |
| 235 | /// Sign the user out, revoking their token from the Bot Framework Token Store. |
| 236 | /// </summary> |
| 237 | /// <param name="connectionName">The connection name to sign out from. If null, uses the default registered connection.</param> |
| 238 | /// <param name="cancellationToken">A cancellation token.</param> |
| 239 | public Task SignOutAsync(string? connectionName = null, CancellationToken cancellationToken = default) |
| 240 | { |
| 241 | OAuthFlow flow = ResolveOAuthFlow(connectionName); |
| 242 | return flow.SignOutAsync(this, cancellationToken); |
| 243 | } |
| 244 | |
| 245 | /// <inheritdoc cref="SignInAsync(OAuthOptions?, CancellationToken)"/> |
| 246 | public Task<string?> SignIn(OAuthOptions? options = null, CancellationToken cancellationToken = default) |
| 247 | => SignInAsync(options, cancellationToken); |
| 248 | |
| 249 | /// <inheritdoc cref="SignOutAsync(string?, CancellationToken)"/> |
| 250 | public Task SignOut(string? connectionName = null, CancellationToken cancellationToken = default) |
| 251 | => SignOutAsync(connectionName, cancellationToken); |
| 252 | |
| 253 | /// <summary> |
| 254 | /// Whether the activity sender has a valid cached token. |
| 255 | /// When a single OAuthFlow is registered, checks that connection. |
| 256 | /// When multiple are registered, checks the first one and logs a warning; |
| 257 | /// prefer <see cref="IsSignedInAsync"/> with an explicit connection name instead. |
| 258 | /// Returns false if no OAuthFlow is registered. |
| 259 | /// </summary> |
| 260 | /// <remarks> |
| 261 | /// This property blocks the calling thread (sync-over-async) while querying |
| 262 | /// the Bot Framework Token Service. Under high concurrency this can cause |
| 263 | /// thread-pool starvation. Prefer <see cref="IsSignedInAsync"/> in new code. |
| 264 | /// </remarks> |
| 265 | [Obsolete("Use IsSignedInAsync() instead. This property blocks the calling thread and can cause thread-pool starvation under load.")] |
| 266 | public bool IsSignedIn |
| 267 | { |
| 268 | get |
| 269 | { |
| 270 | OAuthFlowRegistry? registry = TeamsBotApplication.OAuthRegistry; |
| 271 | if (registry is null) return false; |
| 272 | |
| 273 | OAuthFlow? flow = registry.ResolveSingleWithWarning(); |
| 274 | if (flow is null) return false; |
| 275 | |
| 276 | return flow.GetTokenAsync(this).GetAwaiter().GetResult() is not null; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /// <summary> |
| 281 | /// Check whether the user has a valid cached token for a given OAuth connection. |
| 282 | /// </summary> |
| 283 | /// <param name="connectionName">The connection name to check. If null, uses the single registered connection.</param> |
| 284 | /// <param name="cancellationToken">A cancellation token.</param> |
| 285 | /// <returns>True if the user has a valid token; false otherwise.</returns> |
| 286 | public Task<bool> IsSignedInAsync(string? connectionName = null, CancellationToken cancellationToken = default) |
| 287 | { |
| 288 | OAuthFlow flow = ResolveOAuthFlow(connectionName); |
| 289 | return flow.IsSignedInAsync(this, cancellationToken); |
| 290 | } |
| 291 | |
| 292 | /// <summary> |
| 293 | /// Get the token status for all configured OAuth connections. |
| 294 | /// Returns every connection registered on the bot, so the developer |
| 295 | /// never needs to enumerate connection names manually. |
| 296 | /// </summary> |
| 297 | /// <param name="cancellationToken">A cancellation token.</param> |
| 298 | /// <returns>A list of token status results for all configured connections.</returns> |
| 299 | public Task<IList<GetTokenStatusResult>> GetConnectionStatusAsync(CancellationToken cancellationToken = default) |
| 300 | { |
| 301 | OAuthFlowRegistry registry = TeamsBotApplication.OAuthRegistry |
| 302 | ?? throw new InvalidOperationException("No OAuthFlow registered. Call AddOAuthFlow(connectionName) on the TeamsBotApplication first."); |
| 303 | |
| 304 | // Use any flow -- GetConnectionStatusAsync returns all connections regardless |
| 305 | OAuthFlow flow = registry.ResolveSingle() |
| 306 | ?? registry.GetAllFlows().First(); |
| 307 | |
| 308 | return flow.GetConnectionStatusAsync(this, cancellationToken); |
| 309 | } |
| 310 | |
| 311 | private OAuthFlow ResolveOAuthFlow(string? connectionName) |
| 312 | { |
| 313 | OAuthFlowRegistry registry = TeamsBotApplication.OAuthRegistry |
| 314 | ?? throw new InvalidOperationException("No OAuthFlow registered. Call AddOAuthFlow(connectionName) on the TeamsBotApplication first."); |
| 315 | |
| 316 | if (connectionName is not null) |
| 317 | { |
| 318 | OAuthFlow? flow = registry.Resolve(connectionName); |
| 319 | if (flow is not null) return flow; |
| 320 | |
| 321 | string registered = string.Join(", ", registry.GetRegisteredConnectionNames().Select(n => $"'{n}'")); |
| 322 | throw new InvalidOperationException( |
| 323 | $"No OAuthFlow registered for connection '{connectionName}'. " + |
| 324 | $"Registered connections: {(registered.Length > 0 ? registered : "(none)")}."); |
| 325 | } |
| 326 | |
| 327 | return registry.ResolveSingle() |
| 328 | ?? throw new InvalidOperationException( |
| 329 | "Multiple OAuthFlow instances registered. Specify a connection name in OAuthOptions or SignOut(connectionName)."); |
| 330 | } |
| 331 | } |
| 332 | |