microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.8

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Apps/Context.cs

333lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Diagnostics.CodeAnalysis;
5using Microsoft.Extensions.Logging;
6using Microsoft.Teams.Apps.Api.Clients;
7using Microsoft.Teams.Apps.OAuth;
8using Microsoft.Teams.Apps.Schema;
9using Microsoft.Teams.Apps.Schema.Entities;
10using Microsoft.Teams.Core;
11
12namespace 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>
20public 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#pragma warning disable ExperimentalTeamsQuotedReplies
96 if (!string.IsNullOrWhiteSpace(Activity.Id))
97 {
98 return Quote(Activity.Id, activity, cancellationToken);
99 }
100#pragma warning restore ExperimentalTeamsQuotedReplies
101 return SendActivityAsync(activity, cancellationToken);
102 }
103
104 /// <summary>
105 /// Sends a typing indicator to the conversation.
106 /// </summary>
107 /// <param name="text">Reserved for future use; currently ignored.</param>
108 /// <param name="cancellationToken">A cancellation token.</param>
109 /// <returns>The response from the send operation.</returns>
110 public Task<SendActivityResponse?> TypingAsync(string? text = null, CancellationToken cancellationToken = default)
111 => SendTypingActivityAsync(cancellationToken);
112
113 /// <summary>
114 /// Send a message to the conversation with a quoted message reference prepended to the text.
115 /// Teams renders the quoted message as a preview bubble above the response text.
116 /// </summary>
117 /// <param name="messageId">The ID of the message to quote.</param>
118 /// <param name="text">The response text, appended to the quoted message placeholder.</param>
119 /// <param name="cancellationToken">Optional cancellation token.</param>
120 /// <returns>The response from sending the activity.</returns>
121 [Experimental("ExperimentalTeamsQuotedReplies")]
122 public Task<SendActivityResponse?> Quote(string messageId, string text, CancellationToken cancellationToken = default)
123 => Quote(messageId, new MessageActivity(text), cancellationToken);
124
125 /// <summary>
126 /// Send a message to the conversation with a quoted message reference prepended to the text.
127 /// Teams renders the quoted message as a preview bubble above the response text.
128 /// </summary>
129 /// <param name="messageId">The ID of the message to quote.</param>
130 /// <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>
131 /// <param name="cancellationToken">Optional cancellation token.</param>
132 /// <returns>The response from sending the activity.</returns>
133 [Experimental("ExperimentalTeamsQuotedReplies")]
134 public Task<SendActivityResponse?> Quote(string messageId, TeamsActivity activity, CancellationToken cancellationToken = default)
135 {
136 ArgumentNullException.ThrowIfNull(activity);
137 ArgumentException.ThrowIfNullOrWhiteSpace(messageId);
138 if (activity is MessageActivity message)
139 {
140 message.PrependQuote(messageId);
141 }
142 return SendActivityAsync(activity, cancellationToken);
143 }
144
145 /// <inheritdoc cref="SendAsync(string, CancellationToken)"/>
146 public Task<SendActivityResponse?> Send(string text, CancellationToken cancellationToken = default)
147 => SendAsync(text, cancellationToken);
148
149 /// <inheritdoc cref="SendAsync(TeamsActivity, CancellationToken)"/>
150 public Task<SendActivityResponse?> Send(TeamsActivity activity, CancellationToken cancellationToken = default)
151 => SendAsync(activity, cancellationToken);
152
153 /// <inheritdoc cref="ReplyAsync(string, CancellationToken)"/>
154 public Task<SendActivityResponse?> Reply(string text, CancellationToken cancellationToken = default)
155 => ReplyAsync(text, cancellationToken);
156
157 /// <inheritdoc cref="ReplyAsync(TeamsActivity, CancellationToken)"/>
158 public Task<SendActivityResponse?> Reply(TeamsActivity activity, CancellationToken cancellationToken = default)
159 => ReplyAsync(activity, cancellationToken);
160
161 /// <inheritdoc cref="TypingAsync(string?, CancellationToken)"/>
162 public Task<SendActivityResponse?> Typing(string? text = null, CancellationToken cancellationToken = default)
163 => TypingAsync(text, cancellationToken);
164
165 // ==================== Core Send Methods ====================
166
167 /// <summary>
168 /// Sends a message activity as a reply.
169 /// </summary>
170 /// <param name="text">The text to send.</param>
171 /// <param name="cancellationToken">A cancellation token.</param>
172 /// <returns>The response from the send operation.</returns>
173 public Task<SendActivityResponse?> SendActivityAsync(string text, CancellationToken cancellationToken = default)
174 => SendActivityAsync(new MessageActivity(text) { TextFormat = TextFormats.Plain }, cancellationToken);
175
176 /// <summary>
177 /// Sends an activity to the conversation.
178 /// </summary>
179 /// <param name="activity">The activity to send.</param>
180 /// <param name="cancellationToken">A cancellation token.</param>
181 /// <returns>The response from the send operation.</returns>
182 public Task<SendActivityResponse?> SendActivityAsync(TeamsActivity activity, CancellationToken cancellationToken = default)
183 {
184 ArgumentNullException.ThrowIfNull(activity);
185
186 bool isTargeted = activity.Recipient?.IsTargeted == true;
187
188 if (isTargeted && Activity.Conversation?.ConversationType == ConversationType.Personal)
189 {
190 throw new InvalidOperationException(
191 "Targeted messages are not supported in personal (1:1) chats.");
192 }
193
194 if (activity.Type == TeamsActivityType.Message
195 && Activity.Recipient?.IsTargeted == true
196 && Activity.Id is not null)
197 {
198 TargetedMessageInfoEntityExtensions.AddToActivity(activity, Activity.Id);
199 }
200
201 TeamsActivity reply = new TeamsActivityBuilder(activity)
202 .WithConversationReference(Activity)
203 .Build();
204 return TeamsBotApplication.SendActivityAsync(reply, cancellationToken: cancellationToken);
205 }
206
207 /// <summary>
208 /// Sends a typing activity to the conversation asynchronously.
209 /// </summary>
210 /// <param name="cancellationToken">A cancellation token.</param>
211 /// <returns>The response from the send operation.</returns>
212 public Task<SendActivityResponse?> SendTypingActivityAsync(CancellationToken cancellationToken = default)
213 {
214 TeamsActivity reply = new TeamsActivityBuilder()
215 .WithType(TeamsActivityType.Typing)
216 .WithConversationReference(Activity)
217 .Build();
218 return TeamsBotApplication.SendActivityAsync(reply, cancellationToken: cancellationToken);
219 }
220
221 // ==================== OAuth Sign-In ====================
222
223 /// <summary>
224 /// Trigger user OAuth sign-in flow for the activity sender.
225 /// Attempts silent token acquisition first; if no token is cached, sends an OAuthCard.
226 /// </summary>
227 /// <param name="options">OAuth options including connection name and card text.</param>
228 /// <param name="cancellationToken">A cancellation token.</param>
229 /// <returns>The existing user token if found, or null if the sign-in flow was initiated.</returns>
230 public Task<string?> SignInAsync(OAuthOptions? options = null, CancellationToken cancellationToken = default)
231 {
232 OAuthFlow flow = ResolveOAuthFlow(options?.ConnectionName);
233 return flow.SignInAsync(this, options, cancellationToken);
234 }
235
236 /// <summary>
237 /// Sign the user out, revoking their token from the Bot Framework Token Store.
238 /// </summary>
239 /// <param name="connectionName">The connection name to sign out from. If null, uses the default registered connection.</param>
240 /// <param name="cancellationToken">A cancellation token.</param>
241 public Task SignOutAsync(string? connectionName = null, CancellationToken cancellationToken = default)
242 {
243 OAuthFlow flow = ResolveOAuthFlow(connectionName);
244 return flow.SignOutAsync(this, cancellationToken);
245 }
246
247 /// <inheritdoc cref="SignInAsync(OAuthOptions?, CancellationToken)"/>
248 public Task<string?> SignIn(OAuthOptions? options = null, CancellationToken cancellationToken = default)
249 => SignInAsync(options, cancellationToken);
250
251 /// <inheritdoc cref="SignOutAsync(string?, CancellationToken)"/>
252 public Task SignOut(string? connectionName = null, CancellationToken cancellationToken = default)
253 => SignOutAsync(connectionName, cancellationToken);
254
255 /// <summary>
256 /// Whether the activity sender has a valid cached token.
257 /// When a single OAuthFlow is registered, checks that connection.
258 /// When multiple are registered, checks the first one and logs a warning;
259 /// prefer <see cref="IsSignedInAsync"/> with an explicit connection name instead.
260 /// Returns false if no OAuthFlow is registered.
261 /// </summary>
262 /// <remarks>
263 /// This property blocks the calling thread (sync-over-async) while querying
264 /// the Bot Framework Token Service. Under high concurrency this can cause
265 /// thread-pool starvation. Prefer <see cref="IsSignedInAsync"/> in new code.
266 /// </remarks>
267 [Obsolete("Use IsSignedInAsync() instead. This property blocks the calling thread and can cause thread-pool starvation under load.")]
268 public bool IsSignedIn
269 {
270 get
271 {
272 OAuthFlowRegistry? registry = TeamsBotApplication.OAuthRegistry;
273 if (registry is null) return false;
274
275 OAuthFlow? flow = registry.ResolveSingleWithWarning();
276 if (flow is null) return false;
277
278 return flow.GetTokenAsync(this).GetAwaiter().GetResult() is not null;
279 }
280 }
281
282 /// <summary>
283 /// Check whether the user has a valid cached token for a given OAuth connection.
284 /// </summary>
285 /// <param name="connectionName">The connection name to check. If null, uses the single registered connection.</param>
286 /// <param name="cancellationToken">A cancellation token.</param>
287 /// <returns>True if the user has a valid token; false otherwise.</returns>
288 public Task<bool> IsSignedInAsync(string? connectionName = null, CancellationToken cancellationToken = default)
289 {
290 OAuthFlow flow = ResolveOAuthFlow(connectionName);
291 return flow.IsSignedInAsync(this, cancellationToken);
292 }
293
294 /// <summary>
295 /// Get the token status for all configured OAuth connections.
296 /// Returns every connection registered on the bot, so the developer
297 /// never needs to enumerate connection names manually.
298 /// </summary>
299 /// <param name="cancellationToken">A cancellation token.</param>
300 /// <returns>A list of token status results for all configured connections.</returns>
301 public Task<IList<GetTokenStatusResult>> GetConnectionStatusAsync(CancellationToken cancellationToken = default)
302 {
303 OAuthFlowRegistry registry = TeamsBotApplication.OAuthRegistry
304 ?? throw new InvalidOperationException("No OAuthFlow registered. Call AddOAuthFlow(connectionName) on the TeamsBotApplication first.");
305
306 // Use any flow -- GetConnectionStatusAsync returns all connections regardless
307 OAuthFlow flow = registry.ResolveSingle()
308 ?? registry.GetAllFlows().First();
309
310 return flow.GetConnectionStatusAsync(this, cancellationToken);
311 }
312
313 private OAuthFlow ResolveOAuthFlow(string? connectionName)
314 {
315 OAuthFlowRegistry registry = TeamsBotApplication.OAuthRegistry
316 ?? throw new InvalidOperationException("No OAuthFlow registered. Call AddOAuthFlow(connectionName) on the TeamsBotApplication first.");
317
318 if (connectionName is not null)
319 {
320 OAuthFlow? flow = registry.Resolve(connectionName);
321 if (flow is not null) return flow;
322
323 string registered = string.Join(", ", registry.GetRegisteredConnectionNames().Select(n => $"'{n}'"));
324 throw new InvalidOperationException(
325 $"No OAuthFlow registered for connection '{connectionName}'. " +
326 $"Registered connections: {(registered.Length > 0 ? registered : "(none)")}.");
327 }
328
329 return registry.ResolveSingle()
330 ?? throw new InvalidOperationException(
331 "Multiple OAuthFlow instances registered. Specify a connection name in OAuthOptions or SignOut(connectionName).");
332 }
333}
334