microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Api/BatchApi.cs
358lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Bot.Apps.Schema; |
| 5 | using Microsoft.Teams.Bot.Core.Schema; |
| 6 | |
| 7 | namespace Microsoft.Teams.Bot.Apps.Api; |
| 8 | |
| 9 | using CustomHeaders = Dictionary<string, string>; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// Provides batch messaging operations for sending messages to multiple recipients. |
| 13 | /// </summary> |
| 14 | public class BatchApi |
| 15 | { |
| 16 | private readonly TeamsApiClient _client; |
| 17 | |
| 18 | /// <summary> |
| 19 | /// Initializes a new instance of the <see cref="BatchApi"/> class. |
| 20 | /// </summary> |
| 21 | /// <param name="teamsApiClient">The Teams API client for batch operations.</param> |
| 22 | internal BatchApi(TeamsApiClient teamsApiClient) |
| 23 | { |
| 24 | _client = teamsApiClient; |
| 25 | } |
| 26 | |
| 27 | /// <summary> |
| 28 | /// Sends a message to a list of Teams users. |
| 29 | /// </summary> |
| 30 | /// <param name="activity">The activity to send.</param> |
| 31 | /// <param name="teamsMembers">The list of team members to send the message to.</param> |
| 32 | /// <param name="tenantId">The ID of the tenant.</param> |
| 33 | /// <param name="serviceUrl">The service URL for the Teams service.</param> |
| 34 | /// <param name="agenticIdentity">Optional agentic identity for authentication.</param> |
| 35 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 36 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 37 | /// <returns>A task that represents the asynchronous operation. The task result contains the operation ID.</returns> |
| 38 | public Task<string> SendToUsersAsync( |
| 39 | CoreActivity activity, |
| 40 | IList<TeamMember> teamsMembers, |
| 41 | string tenantId, |
| 42 | Uri serviceUrl, |
| 43 | AgenticIdentity? agenticIdentity = null, |
| 44 | CustomHeaders? customHeaders = null, |
| 45 | CancellationToken cancellationToken = default) |
| 46 | => _client.SendMessageToListOfUsersAsync(activity, teamsMembers, tenantId, serviceUrl, agenticIdentity, customHeaders, cancellationToken); |
| 47 | |
| 48 | /// <summary> |
| 49 | /// Sends a message to a list of Teams users using activity context. |
| 50 | /// </summary> |
| 51 | /// <param name="activity">The activity to send.</param> |
| 52 | /// <param name="teamsMembers">The list of team members to send the message to.</param> |
| 53 | /// <param name="contextActivity">The activity providing service URL, tenant ID, and identity context.</param> |
| 54 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 55 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 56 | /// <returns>A task that represents the asynchronous operation. The task result contains the operation ID.</returns> |
| 57 | public Task<string> SendToUsersAsync( |
| 58 | CoreActivity activity, |
| 59 | IList<TeamMember> teamsMembers, |
| 60 | TeamsActivity contextActivity, |
| 61 | CustomHeaders? customHeaders = null, |
| 62 | CancellationToken cancellationToken = default) |
| 63 | { |
| 64 | ArgumentNullException.ThrowIfNull(contextActivity); |
| 65 | ArgumentNullException.ThrowIfNull(contextActivity.ServiceUrl); |
| 66 | |
| 67 | string? tenantId = contextActivity.ChannelData?.Tenant?.Id; |
| 68 | ArgumentException.ThrowIfNullOrWhiteSpace(tenantId, "contextActivity.ChannelData.Tenant.Id"); |
| 69 | return _client.SendMessageToListOfUsersAsync( |
| 70 | activity, |
| 71 | teamsMembers, |
| 72 | tenantId, |
| 73 | contextActivity.ServiceUrl, |
| 74 | contextActivity.From?.GetAgenticIdentity(), |
| 75 | customHeaders, |
| 76 | cancellationToken); |
| 77 | } |
| 78 | |
| 79 | /// <summary> |
| 80 | /// Sends a message to all users in a tenant. |
| 81 | /// </summary> |
| 82 | /// <param name="activity">The activity to send.</param> |
| 83 | /// <param name="tenantId">The ID of the tenant.</param> |
| 84 | /// <param name="serviceUrl">The service URL for the Teams service.</param> |
| 85 | /// <param name="agenticIdentity">Optional agentic identity for authentication.</param> |
| 86 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 87 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 88 | /// <returns>A task that represents the asynchronous operation. The task result contains the operation ID.</returns> |
| 89 | public Task<string> SendToTenantAsync( |
| 90 | CoreActivity activity, |
| 91 | string tenantId, |
| 92 | Uri serviceUrl, |
| 93 | AgenticIdentity? agenticIdentity = null, |
| 94 | CustomHeaders? customHeaders = null, |
| 95 | CancellationToken cancellationToken = default) |
| 96 | => _client.SendMessageToAllUsersInTenantAsync(activity, tenantId, serviceUrl, agenticIdentity, customHeaders, cancellationToken); |
| 97 | |
| 98 | /// <summary> |
| 99 | /// Sends a message to all users in a tenant using activity context. |
| 100 | /// </summary> |
| 101 | /// <param name="activity">The activity to send.</param> |
| 102 | /// <param name="contextActivity">The activity providing service URL, tenant ID, and identity context.</param> |
| 103 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 104 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 105 | /// <returns>A task that represents the asynchronous operation. The task result contains the operation ID.</returns> |
| 106 | public Task<string> SendToTenantAsync( |
| 107 | CoreActivity activity, |
| 108 | TeamsActivity contextActivity, |
| 109 | CustomHeaders? customHeaders = null, |
| 110 | CancellationToken cancellationToken = default) |
| 111 | { |
| 112 | ArgumentNullException.ThrowIfNull(contextActivity); |
| 113 | ArgumentNullException.ThrowIfNull(contextActivity.ServiceUrl); |
| 114 | |
| 115 | string? tenantId = contextActivity.ChannelData?.Tenant?.Id; |
| 116 | ArgumentException.ThrowIfNullOrWhiteSpace(tenantId, "contextActivity.ChannelData.Tenant.Id"); |
| 117 | return _client.SendMessageToAllUsersInTenantAsync( |
| 118 | activity, |
| 119 | tenantId, |
| 120 | contextActivity.ServiceUrl, |
| 121 | contextActivity.From?.GetAgenticIdentity(), |
| 122 | customHeaders, |
| 123 | cancellationToken); |
| 124 | } |
| 125 | |
| 126 | /// <summary> |
| 127 | /// Sends a message to all users in a team. |
| 128 | /// </summary> |
| 129 | /// <param name="activity">The activity to send.</param> |
| 130 | /// <param name="teamId">The ID of the team.</param> |
| 131 | /// <param name="tenantId">The ID of the tenant.</param> |
| 132 | /// <param name="serviceUrl">The service URL for the Teams service.</param> |
| 133 | /// <param name="agenticIdentity">Optional agentic identity for authentication.</param> |
| 134 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 135 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 136 | /// <returns>A task that represents the asynchronous operation. The task result contains the operation ID.</returns> |
| 137 | public Task<string> SendToTeamAsync( |
| 138 | CoreActivity activity, |
| 139 | string teamId, |
| 140 | string tenantId, |
| 141 | Uri serviceUrl, |
| 142 | AgenticIdentity? agenticIdentity = null, |
| 143 | CustomHeaders? customHeaders = null, |
| 144 | CancellationToken cancellationToken = default) |
| 145 | => _client.SendMessageToAllUsersInTeamAsync(activity, teamId, tenantId, serviceUrl, agenticIdentity, customHeaders, cancellationToken); |
| 146 | |
| 147 | /// <summary> |
| 148 | /// Sends a message to all users in a team using activity context. |
| 149 | /// </summary> |
| 150 | /// <param name="activity">The activity to send.</param> |
| 151 | /// <param name="teamId">The ID of the team.</param> |
| 152 | /// <param name="contextActivity">The activity providing service URL, tenant ID, and identity context.</param> |
| 153 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 154 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 155 | /// <returns>A task that represents the asynchronous operation. The task result contains the operation ID.</returns> |
| 156 | public Task<string> SendToTeamAsync( |
| 157 | CoreActivity activity, |
| 158 | string teamId, |
| 159 | TeamsActivity contextActivity, |
| 160 | CustomHeaders? customHeaders = null, |
| 161 | CancellationToken cancellationToken = default) |
| 162 | { |
| 163 | ArgumentNullException.ThrowIfNull(contextActivity); |
| 164 | ArgumentNullException.ThrowIfNull(contextActivity.ServiceUrl); |
| 165 | |
| 166 | string? tenantId = contextActivity.ChannelData?.Tenant?.Id; |
| 167 | ArgumentException.ThrowIfNullOrWhiteSpace(tenantId, "contextActivity.ChannelData.Tenant.Id"); |
| 168 | return _client.SendMessageToAllUsersInTeamAsync( |
| 169 | activity, |
| 170 | teamId, |
| 171 | tenantId, |
| 172 | contextActivity.ServiceUrl, |
| 173 | contextActivity.From?.GetAgenticIdentity(), |
| 174 | customHeaders, |
| 175 | cancellationToken); |
| 176 | } |
| 177 | |
| 178 | /// <summary> |
| 179 | /// Sends a message to a list of Teams channels. |
| 180 | /// </summary> |
| 181 | /// <param name="activity">The activity to send.</param> |
| 182 | /// <param name="channelMembers">The list of channels to send the message to.</param> |
| 183 | /// <param name="tenantId">The ID of the tenant.</param> |
| 184 | /// <param name="serviceUrl">The service URL for the Teams service.</param> |
| 185 | /// <param name="agenticIdentity">Optional agentic identity for authentication.</param> |
| 186 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 187 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 188 | /// <returns>A task that represents the asynchronous operation. The task result contains the operation ID.</returns> |
| 189 | public Task<string> SendToChannelsAsync( |
| 190 | CoreActivity activity, |
| 191 | IList<TeamMember> channelMembers, |
| 192 | string tenantId, |
| 193 | Uri serviceUrl, |
| 194 | AgenticIdentity? agenticIdentity = null, |
| 195 | CustomHeaders? customHeaders = null, |
| 196 | CancellationToken cancellationToken = default) |
| 197 | => _client.SendMessageToListOfChannelsAsync(activity, channelMembers, tenantId, serviceUrl, agenticIdentity, customHeaders, cancellationToken); |
| 198 | |
| 199 | /// <summary> |
| 200 | /// Sends a message to a list of Teams channels using activity context. |
| 201 | /// </summary> |
| 202 | /// <param name="activity">The activity to send.</param> |
| 203 | /// <param name="channelMembers">The list of channels to send the message to.</param> |
| 204 | /// <param name="contextActivity">The activity providing service URL, tenant ID, and identity context.</param> |
| 205 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 206 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 207 | /// <returns>A task that represents the asynchronous operation. The task result contains the operation ID.</returns> |
| 208 | public Task<string> SendToChannelsAsync( |
| 209 | CoreActivity activity, |
| 210 | IList<TeamMember> channelMembers, |
| 211 | TeamsActivity contextActivity, |
| 212 | CustomHeaders? customHeaders = null, |
| 213 | CancellationToken cancellationToken = default) |
| 214 | { |
| 215 | ArgumentNullException.ThrowIfNull(contextActivity); |
| 216 | ArgumentNullException.ThrowIfNull(contextActivity.ServiceUrl); |
| 217 | |
| 218 | string? tenantId = contextActivity.ChannelData?.Tenant?.Id; |
| 219 | ArgumentException.ThrowIfNullOrWhiteSpace(tenantId, "contextActivity.ChannelData.Tenant.Id"); |
| 220 | return _client.SendMessageToListOfChannelsAsync( |
| 221 | activity, |
| 222 | channelMembers, |
| 223 | tenantId, |
| 224 | contextActivity.ServiceUrl, |
| 225 | contextActivity.From?.GetAgenticIdentity(), |
| 226 | customHeaders, |
| 227 | cancellationToken); |
| 228 | } |
| 229 | |
| 230 | /// <summary> |
| 231 | /// Gets the state of a batch operation. |
| 232 | /// </summary> |
| 233 | /// <param name="operationId">The ID of the operation.</param> |
| 234 | /// <param name="serviceUrl">The service URL for the Teams service.</param> |
| 235 | /// <param name="agenticIdentity">Optional agentic identity for authentication.</param> |
| 236 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 237 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 238 | /// <returns>A task that represents the asynchronous operation. The task result contains the operation state.</returns> |
| 239 | public Task<BatchOperationState> GetStateAsync( |
| 240 | string operationId, |
| 241 | Uri serviceUrl, |
| 242 | AgenticIdentity? agenticIdentity = null, |
| 243 | CustomHeaders? customHeaders = null, |
| 244 | CancellationToken cancellationToken = default) |
| 245 | => _client.GetOperationStateAsync(operationId, serviceUrl, agenticIdentity, customHeaders, cancellationToken); |
| 246 | |
| 247 | /// <summary> |
| 248 | /// Gets the state of a batch operation using activity context. |
| 249 | /// </summary> |
| 250 | /// <param name="operationId">The ID of the operation.</param> |
| 251 | /// <param name="activity">The activity providing service URL and identity context.</param> |
| 252 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 253 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 254 | /// <returns>A task that represents the asynchronous operation. The task result contains the operation state.</returns> |
| 255 | public Task<BatchOperationState> GetStateAsync( |
| 256 | string operationId, |
| 257 | TeamsActivity activity, |
| 258 | CustomHeaders? customHeaders = null, |
| 259 | CancellationToken cancellationToken = default) |
| 260 | { |
| 261 | ArgumentNullException.ThrowIfNull(activity); |
| 262 | ArgumentNullException.ThrowIfNull(activity.ServiceUrl); |
| 263 | return _client.GetOperationStateAsync( |
| 264 | operationId, |
| 265 | activity.ServiceUrl, |
| 266 | activity.From?.GetAgenticIdentity(), |
| 267 | customHeaders, |
| 268 | cancellationToken); |
| 269 | } |
| 270 | |
| 271 | /// <summary> |
| 272 | /// Gets the failed entries of a batch operation. |
| 273 | /// </summary> |
| 274 | /// <param name="operationId">The ID of the operation.</param> |
| 275 | /// <param name="serviceUrl">The service URL for the Teams service.</param> |
| 276 | /// <param name="continuationToken">Optional continuation token for pagination.</param> |
| 277 | /// <param name="agenticIdentity">Optional agentic identity for authentication.</param> |
| 278 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 279 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 280 | /// <returns>A task that represents the asynchronous operation. The task result contains the failed entries.</returns> |
| 281 | public Task<BatchFailedEntriesResponse> GetFailedEntriesAsync( |
| 282 | string operationId, |
| 283 | Uri serviceUrl, |
| 284 | string? continuationToken = null, |
| 285 | AgenticIdentity? agenticIdentity = null, |
| 286 | CustomHeaders? customHeaders = null, |
| 287 | CancellationToken cancellationToken = default) |
| 288 | => _client.GetPagedFailedEntriesAsync(operationId, serviceUrl, continuationToken, agenticIdentity, customHeaders, cancellationToken); |
| 289 | |
| 290 | /// <summary> |
| 291 | /// Gets the failed entries of a batch operation using activity context. |
| 292 | /// </summary> |
| 293 | /// <param name="operationId">The ID of the operation.</param> |
| 294 | /// <param name="activity">The activity providing service URL and identity context.</param> |
| 295 | /// <param name="continuationToken">Optional continuation token for pagination.</param> |
| 296 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 297 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 298 | /// <returns>A task that represents the asynchronous operation. The task result contains the failed entries.</returns> |
| 299 | public Task<BatchFailedEntriesResponse> GetFailedEntriesAsync( |
| 300 | string operationId, |
| 301 | TeamsActivity activity, |
| 302 | string? continuationToken = null, |
| 303 | CustomHeaders? customHeaders = null, |
| 304 | CancellationToken cancellationToken = default) |
| 305 | { |
| 306 | ArgumentNullException.ThrowIfNull(activity); |
| 307 | ArgumentNullException.ThrowIfNull(activity.ServiceUrl); |
| 308 | return _client.GetPagedFailedEntriesAsync( |
| 309 | operationId, |
| 310 | activity.ServiceUrl, |
| 311 | continuationToken, |
| 312 | activity.From?.GetAgenticIdentity(), |
| 313 | customHeaders, |
| 314 | cancellationToken); |
| 315 | } |
| 316 | |
| 317 | /// <summary> |
| 318 | /// Cancels a batch operation. |
| 319 | /// </summary> |
| 320 | /// <param name="operationId">The ID of the operation to cancel.</param> |
| 321 | /// <param name="serviceUrl">The service URL for the Teams service.</param> |
| 322 | /// <param name="agenticIdentity">Optional agentic identity for authentication.</param> |
| 323 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 324 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 325 | /// <returns>A task that represents the asynchronous operation.</returns> |
| 326 | public Task CancelAsync( |
| 327 | string operationId, |
| 328 | Uri serviceUrl, |
| 329 | AgenticIdentity? agenticIdentity = null, |
| 330 | CustomHeaders? customHeaders = null, |
| 331 | CancellationToken cancellationToken = default) |
| 332 | => _client.CancelOperationAsync(operationId, serviceUrl, agenticIdentity, customHeaders, cancellationToken); |
| 333 | |
| 334 | /// <summary> |
| 335 | /// Cancels a batch operation using activity context. |
| 336 | /// </summary> |
| 337 | /// <param name="operationId">The ID of the operation to cancel.</param> |
| 338 | /// <param name="activity">The activity providing service URL and identity context.</param> |
| 339 | /// <param name="customHeaders">Optional custom headers to include in the request.</param> |
| 340 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 341 | /// <returns>A task that represents the asynchronous operation.</returns> |
| 342 | public Task CancelAsync( |
| 343 | string operationId, |
| 344 | TeamsActivity activity, |
| 345 | CustomHeaders? customHeaders = null, |
| 346 | CancellationToken cancellationToken = default) |
| 347 | { |
| 348 | ArgumentNullException.ThrowIfNull(activity); |
| 349 | ArgumentNullException.ThrowIfNull(activity.ServiceUrl); |
| 350 | |
| 351 | return _client.CancelOperationAsync( |
| 352 | operationId, |
| 353 | activity.ServiceUrl, |
| 354 | activity.From?.GetAgenticIdentity(), |
| 355 | customHeaders, |
| 356 | cancellationToken); |
| 357 | } |
| 358 | } |
| 359 | |