-
Notifications
You must be signed in to change notification settings - Fork 487
/
Copy pathTeamsInfo.cs
534 lines (481 loc) · 29.6 KB
/
TeamsInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Connector.Teams;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Schema.Teams;
using Newtonsoft.Json.Linq;
namespace Microsoft.Bot.Builder.Teams
{
/// <summary>
/// The TeamsInfo Test If Build Remote Successful
/// provides utility methods for the events and interactions that occur within Microsoft Teams.
/// </summary>
public static class TeamsInfo
{
/// <summary>
/// Gets the details for the given meeting participant. This only works in teams meeting scoped conversations.
/// </summary>
/// <param name="turnContext">Turn context.</param>
/// <param name="meetingId">The id of the Teams meeting. TeamsChannelData.Meeting.Id will be used if none provided.</param>
/// <param name="participantId">The id of the Teams meeting participant. From.AadObjectId will be used if none provided.</param>
/// <param name="tenantId">The id of the Teams meeting Tenant. TeamsChannelData.Tenant.Id will be used if none provided.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <remarks> <see cref="InvalidOperationException"/> will be thrown if meetingId, participantId or tenantId have not been
/// provided, and also cannot be retrieved from turnContext.Activity.</remarks>
/// <returns>Team participant channel account.</returns>
public static async Task<TeamsMeetingParticipant> GetMeetingParticipantAsync(ITurnContext turnContext, string meetingId = null, string participantId = null, string tenantId = null, CancellationToken cancellationToken = default)
{
meetingId ??= turnContext.Activity.TeamsGetMeetingInfo()?.Id ?? throw new InvalidOperationException("This method is only valid within the scope of a MS Teams Meeting.");
participantId ??= turnContext.Activity.From.AadObjectId ?? throw new InvalidOperationException($"{nameof(participantId)} is required.");
tenantId ??= turnContext.Activity.GetChannelData<TeamsChannelData>()?.Tenant?.Id ?? throw new InvalidOperationException($"{nameof(tenantId)} is required.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
return await teamsClient.Teams.FetchParticipantAsync(meetingId, participantId, tenantId, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Gets the information for the given meeting id.
/// </summary>
/// <param name="turnContext"> Turn context.</param>
/// <param name="meetingId"> The BASE64-encoded id of the Teams meeting.</param>
/// <param name="cancellationToken"> Cancellation token.</param>
/// <returns>Team Details.</returns>
public static async Task<MeetingInfo> GetMeetingInfoAsync(ITurnContext turnContext, string meetingId = null, CancellationToken cancellationToken = default)
{
meetingId ??= turnContext.Activity.TeamsGetMeetingInfo()?.Id ?? throw new InvalidOperationException("The meetingId can only be null if turnContext is within the scope of a MS Teams Meeting.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
return await teamsClient.Teams.FetchMeetingInfoAsync(meetingId, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Gets the details for the given team id. This only works in teams scoped conversations.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="teamId"> The id of the Teams team. </param>
/// <param name="cancellationToken"> Cancellation token. </param>
/// <returns>Team Details.</returns>
public static async Task<TeamDetails> GetTeamDetailsAsync(ITurnContext turnContext, string teamId = null, CancellationToken cancellationToken = default)
{
var t = teamId ?? turnContext.Activity.TeamsGetTeamInfo()?.Id ?? throw new InvalidOperationException("This method is only valid within the scope of MS Teams Team.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
return await teamsClient.Teams.FetchTeamDetailsAsync(t, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Returns a list of channels in a Team.
/// This only works in teams scoped conversations.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="teamId"> ID of the Teams team. </param>
/// <param name="cancellationToken"> cancellation token. </param>
/// <returns>Team Details.</returns>
public static async Task<IList<ChannelInfo>> GetTeamChannelsAsync(ITurnContext turnContext, string teamId = null, CancellationToken cancellationToken = default)
{
var t = teamId ?? turnContext.Activity.TeamsGetTeamInfo()?.Id ?? throw new InvalidOperationException("This method is only valid within the scope of MS Teams Team.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
var channelList = await teamsClient.Teams.FetchChannelListAsync(t, cancellationToken).ConfigureAwait(false);
return channelList.Conversations;
}
}
/// <summary>
/// Gets the list of TeamsChannelAccounts within a team.
/// This only works in teams scoped conversations.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="teamId"> ID of the Teams team. </param>
/// <param name="cancellationToken"> cancellation token. </param>
/// <returns>TeamsChannelAccount.</returns>
[Obsolete("Microsoft Teams is deprecating the non-paged version of the getMembers API which this method uses. Please use GetPagedTeamMembersAsync instead of this API.")]
public static Task<IEnumerable<TeamsChannelAccount>> GetTeamMembersAsync(ITurnContext turnContext, string teamId = null, CancellationToken cancellationToken = default)
{
var t = teamId ?? turnContext.Activity.TeamsGetTeamInfo()?.Id ?? throw new InvalidOperationException("This method is only valid within the scope of MS Teams Team.");
return GetMembersAsync(GetConnectorClient(turnContext), t, cancellationToken);
}
/// <summary>
/// Gets the conversation members of a one-on-one or group chat.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="cancellationToken"> Cancellation token. </param>
/// <returns>TeamsChannelAccount.</returns>
[Obsolete("Microsoft Teams is deprecating the non-paged version of the getMembers API which this method uses. Please use GetPagedTeamMembersAsync instead of this API.")]
public static Task<IEnumerable<TeamsChannelAccount>> GetMembersAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
{
var teamInfo = turnContext.Activity.TeamsGetTeamInfo();
if (teamInfo?.Id != null)
{
return GetTeamMembersAsync(turnContext, teamInfo.Id, cancellationToken);
}
else
{
var conversationId = turnContext.Activity?.Conversation?.Id;
return GetMembersAsync(GetConnectorClient(turnContext), conversationId, cancellationToken);
}
}
/// <summary>
/// Gets a paginated list of members of a team.
/// This only works in teams scoped conversations.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="teamId"> ID of the Teams team. </param>
/// <param name="continuationToken"> continuationToken token. </param>
/// <param name="pageSize"> number of entries on the page. </param>
/// /// <param name="cancellationToken"> cancellation token. </param>
/// <returns>TeamsPagedMembersResult.</returns>
public static Task<TeamsPagedMembersResult> GetPagedTeamMembersAsync(ITurnContext turnContext, string teamId = null, string continuationToken = default(string), int? pageSize = default(int?), CancellationToken cancellationToken = default)
{
var t = teamId ?? turnContext.Activity.TeamsGetTeamInfo()?.Id ?? throw new InvalidOperationException("This method is only valid within the scope of MS Teams Team.");
return GetPagedMembersAsync(GetConnectorClient(turnContext), t, continuationToken, cancellationToken, pageSize);
}
/// <summary>
/// Gets a paginated list of members of one-on-one, group, or team conversation.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="pageSize"> Suggested number of entries on a page. </param>
/// <param name="continuationToken"> ContinuationToken token. </param>
/// /// <param name="cancellationToken"> Cancellation token. </param>
/// <returns>TeamsPagedMembersResult.</returns>
public static Task<TeamsPagedMembersResult> GetPagedMembersAsync(ITurnContext turnContext, int? pageSize = default(int?), string continuationToken = default(string), CancellationToken cancellationToken = default)
{
var teamInfo = turnContext.Activity.TeamsGetTeamInfo();
if (teamInfo?.Id != null)
{
return GetPagedTeamMembersAsync(turnContext, teamInfo.Id, continuationToken, pageSize, cancellationToken);
}
else
{
var conversationId = turnContext.Activity?.Conversation?.Id;
return GetPagedMembersAsync(GetConnectorClient(turnContext), conversationId, continuationToken, cancellationToken, pageSize);
}
}
/// <summary>
/// Gets the member of a teams scoped conversation.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="userId"> user id. </param>
/// <param name="teamId"> ID of the Teams team. </param>
/// <param name="cancellationToken"> cancellation token. </param>
/// <returns>Team Details.</returns>
public static Task<TeamsChannelAccount> GetTeamMemberAsync(ITurnContext turnContext, string userId, string teamId = null, CancellationToken cancellationToken = default)
{
var t = teamId ?? turnContext.Activity.TeamsGetTeamInfo()?.Id ?? throw new InvalidOperationException("This method is only valid within the scope of MS Teams Team.");
return GetMemberAsync(GetConnectorClient(turnContext), userId, t, cancellationToken);
}
/// <summary>
/// Gets the account of a single conversation member.
/// This works in one-on-one, group, and teams scoped conversations.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="userId"> ID of the user in question. </param>
/// <param name="cancellationToken"> cancellation token. </param>
/// <returns>Team Details.</returns>
public static Task<TeamsChannelAccount> GetMemberAsync(ITurnContext turnContext, string userId, CancellationToken cancellationToken = default)
{
var teamInfo = turnContext.Activity.TeamsGetTeamInfo();
if (teamInfo?.Id != null)
{
return GetTeamMemberAsync(turnContext, userId, teamInfo.Id, cancellationToken);
}
else
{
var conversationId = turnContext.Activity?.Conversation?.Id;
return GetMemberAsync(GetConnectorClient(turnContext), userId, conversationId, cancellationToken);
}
}
/// <summary>
/// Creates a new thread in a team chat and sends an activity to that new thread. Use this method if you are using BotFrameworkAdapter and are handling credentials in your code.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="activity"> The activity to send on starting the new thread. </param>
/// <param name="teamsChannelId"> The Team's Channel ID, note this is distinct from the Bot Framework activity property with same name. </param>
/// <param name="credentials"> Microsoft app credentials. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns>Team Details.</returns>
public static async Task<Tuple<ConversationReference, string>> SendMessageToTeamsChannelAsync(ITurnContext turnContext, IActivity activity, string teamsChannelId, MicrosoftAppCredentials credentials, CancellationToken cancellationToken = default)
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
if (turnContext.Activity == null)
{
throw new InvalidOperationException(nameof(turnContext.Activity));
}
if (string.IsNullOrEmpty(teamsChannelId))
{
throw new ArgumentNullException(nameof(teamsChannelId));
}
if (credentials == null)
{
throw new ArgumentNullException(nameof(credentials));
}
ConversationReference conversationReference = null;
var newActivityId = string.Empty;
var serviceUrl = turnContext.Activity.ServiceUrl;
var conversationParameters = new ConversationParameters
{
IsGroup = true,
ChannelData = new TeamsChannelData { Channel = new ChannelInfo() { Id = teamsChannelId } },
Activity = (Activity)activity,
};
await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
teamsChannelId,
serviceUrl,
credentials,
conversationParameters,
(t, ct) =>
{
conversationReference = t.Activity.GetConversationReference();
newActivityId = t.Activity.Id;
return Task.CompletedTask;
},
cancellationToken).ConfigureAwait(false);
return new Tuple<ConversationReference, string>(conversationReference, newActivityId);
}
/// <summary>
/// Creates a new thread in a team chat and sends an activity to that new thread. Use this method if you are using CloudAdapter where credentials are handled by the adapter.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="activity"> The activity to send on starting the new thread. </param>
/// <param name="teamsChannelId"> The Team's Channel ID, note this is distinct from the Bot Framework activity property with same name. </param>
/// <param name="botAppId"> The bot's appId. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns>Team Details.</returns>
public static async Task<Tuple<ConversationReference, string>> SendMessageToTeamsChannelAsync(ITurnContext turnContext, IActivity activity, string teamsChannelId, string botAppId, CancellationToken cancellationToken = default)
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
if (turnContext.Activity == null)
{
throw new InvalidOperationException(nameof(turnContext.Activity));
}
if (string.IsNullOrEmpty(teamsChannelId))
{
throw new ArgumentNullException(nameof(teamsChannelId));
}
ConversationReference conversationReference = null;
var newActivityId = string.Empty;
var serviceUrl = turnContext.Activity.ServiceUrl;
var conversationParameters = new ConversationParameters
{
IsGroup = true,
ChannelData = new TeamsChannelData { Channel = new ChannelInfo() { Id = teamsChannelId } },
Activity = (Activity)activity,
};
await turnContext.Adapter.CreateConversationAsync(
botAppId,
Channels.Msteams,
serviceUrl,
null,
conversationParameters,
(t, ct) =>
{
conversationReference = t.Activity.GetConversationReference();
newActivityId = t.Activity.Id;
return Task.CompletedTask;
},
cancellationToken).ConfigureAwait(false);
return new Tuple<ConversationReference, string>(conversationReference, newActivityId);
}
/// <summary>
/// Sends a notification to meeting participants. This functionality is available only in teams meeting scoped conversations.
/// </summary>
/// <param name="turnContext">Turn context.</param>
/// <param name="notification">The notification to send to Teams.</param>
/// <param name="meetingId">The id of the Teams meeting. TeamsChannelData.Meeting.Id will be used if none provided.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <remarks>InvalidOperationException will be thrown if meetingId or notification have not been
/// provided, and also cannot be retrieved from turnContext.Activity.</remarks>
/// <returns> <see cref="MeetingNotificationResponse"/>.</returns>
public static async Task<MeetingNotificationResponse> SendMeetingNotificationAsync(ITurnContext turnContext, MeetingNotificationBase notification, string meetingId = null, CancellationToken cancellationToken = default)
{
meetingId ??= turnContext.Activity.TeamsGetMeetingInfo()?.Id ?? throw new InvalidOperationException("This method is only valid within the scope of a MS Teams Meeting.");
notification = notification ?? throw new InvalidOperationException($"{nameof(notification)} is required.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
return await teamsClient.Teams.SendMeetingNotificationAsync(meetingId, notification, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Sends a message to the provided list of Teams members.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="activity"> The activity to send. </param>
/// <param name="teamsMembers"> The list of members. </param>
/// <param name="tenantId"> The tenant ID. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns> The operation Id. </returns>
public static async Task<string> SendMessageToListOfUsersAsync(ITurnContext turnContext, IActivity activity, List<TeamMember> teamsMembers, string tenantId, CancellationToken cancellationToken = default)
{
activity = activity ?? throw new InvalidOperationException($"{nameof(activity)} is required.");
teamsMembers = teamsMembers ?? throw new InvalidOperationException($"{nameof(teamsMembers)} is required.");
tenantId = tenantId ?? throw new InvalidOperationException($"{nameof(tenantId)} is required.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
return await teamsClient.Teams.SendMessageToListOfUsersAsync(activity, teamsMembers, tenantId, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Sends a message to all the users in a tenant.
/// </summary>
/// <param name="turnContext"> The turn context. </param>
/// <param name="activity"> The activity to send to the tenant. </param>
/// <param name="tenantId"> The tenant ID. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns> The operation Id. </returns>
public static async Task<string> SendMessageToAllUsersInTenantAsync(ITurnContext turnContext, IActivity activity, string tenantId, CancellationToken cancellationToken = default)
{
activity = activity ?? throw new InvalidOperationException($"{nameof(activity)} is required.");
tenantId = tenantId ?? throw new InvalidOperationException($"{nameof(tenantId)} is required.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
return await teamsClient.Teams.SendMessageToAllUsersInTenantAsync(activity, tenantId, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Sends a message to all the users in a team.
/// </summary>
/// <param name="turnContext"> The turn context. </param>
/// <param name="activity"> The activity to send to the users in the team. </param>
/// <param name="teamId"> The team ID. </param>
/// <param name="tenantId"> The tenant ID. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns>The operation Id.</returns>
public static async Task<string> SendMessageToAllUsersInTeamAsync(ITurnContext turnContext, IActivity activity, string teamId, string tenantId, CancellationToken cancellationToken = default)
{
activity = activity ?? throw new InvalidOperationException($"{nameof(activity)} is required.");
teamId = teamId ?? throw new InvalidOperationException($"{nameof(teamId)} is required.");
tenantId = tenantId ?? throw new InvalidOperationException($"{nameof(tenantId)} is required.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
return await teamsClient.Teams.SendMessageToAllUsersInTeamAsync(activity, teamId, tenantId, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Sends a message to the provided list of Teams channels.
/// </summary>
/// <param name="turnContext"> The turn context. </param>
/// <param name="activity"> The activity to send. </param>
/// <param name="channelsMembers"> The list of channels. </param>
/// <param name="tenantId"> The tenant ID. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns> The operation Id. </returns>
public static async Task<string> SendMessageToListOfChannelsAsync(ITurnContext turnContext, IActivity activity, List<TeamMember> channelsMembers, string tenantId, CancellationToken cancellationToken = default)
{
activity = activity ?? throw new InvalidOperationException($"{nameof(activity)} is required.");
channelsMembers = channelsMembers ?? throw new InvalidOperationException($"{nameof(channelsMembers)} is required.");
tenantId = tenantId ?? throw new InvalidOperationException($"{nameof(tenantId)} is required.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
return await teamsClient.Teams.SendMessageToListOfChannelsAsync(activity, channelsMembers, tenantId, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Gets the state of an operation.
/// </summary>
/// <param name="turnContext"> Turn context. </param>
/// <param name="operationId"> The operationId to get the state of. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns> The state and responses of the operation. </returns>
public static async Task<BatchOperationState> GetOperationStateAsync(ITurnContext turnContext, string operationId, CancellationToken cancellationToken = default)
{
operationId = operationId ?? throw new InvalidOperationException($"{nameof(operationId)} is required.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
return await teamsClient.Teams.GetOperationStateAsync(operationId, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Gets the failed entries of a batch operation.
/// </summary>
/// <param name="turnContext"> The turn context. </param>
/// <param name="operationId"> The operationId to get the failed entries of. </param>
/// <param name="continuationToken"> The continuation token. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns> The list of failed entries of the operation. </returns>
public static async Task<BatchFailedEntriesResponse> GetPagedFailedEntriesAsync(ITurnContext turnContext, string operationId, string continuationToken = null, CancellationToken cancellationToken = default)
{
operationId = operationId ?? throw new InvalidOperationException($"{nameof(operationId)} is required.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
return await teamsClient.Teams.GetPagedFailedEntriesAsync(operationId, continuationToken, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Cancels a batch operation by its id.
/// </summary>
/// <param name="turnContext"> The turn context. </param>
/// <param name="operationId"> The id of the operation to cancel. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns> A <see cref="Task"/> representing the asynchronous operation. </returns>
public static async Task CancelOperationAsync(ITurnContext turnContext, string operationId, CancellationToken cancellationToken = default)
{
operationId = operationId ?? throw new InvalidOperationException($"{nameof(operationId)} is required.");
using (var teamsClient = GetTeamsConnectorClient(turnContext))
{
await teamsClient.Teams.CancelOperationAsync(operationId, cancellationToken).ConfigureAwait(false);
}
}
private static async Task<IEnumerable<TeamsChannelAccount>> GetMembersAsync(IConnectorClient connectorClient, string conversationId, CancellationToken cancellationToken)
{
if (conversationId == null)
{
throw new InvalidOperationException("The GetMembers operation needs a valid conversation Id.");
}
var teamMembers = await connectorClient.Conversations.GetConversationMembersAsync(conversationId, cancellationToken).ConfigureAwait(false);
var teamsChannelAccounts = teamMembers.Select(channelAccount => JObject.FromObject(channelAccount).ToObject<TeamsChannelAccount>());
return teamsChannelAccounts;
}
private static IConnectorClient GetConnectorClient(ITurnContext turnContext)
{
return turnContext.TurnState.Get<IConnectorClient>() ?? throw new InvalidOperationException("This method requires a connector client.");
}
private static async Task<TeamsChannelAccount> GetMemberAsync(IConnectorClient connectorClient, string userId, string conversationId, CancellationToken cancellationToken)
{
if (conversationId == null)
{
throw new InvalidOperationException("The GetMembers operation needs a valid conversation Id.");
}
if (userId == null)
{
throw new InvalidOperationException("The GetMembers operation needs a valid user Id.");
}
var teamMember = await ((Conversations)connectorClient.Conversations).GetConversationMemberAsync(userId, conversationId, cancellationToken).ConfigureAwait(false);
var teamsChannelAccount = JObject.FromObject(teamMember).ToObject<TeamsChannelAccount>();
return teamsChannelAccount;
}
private static async Task<TeamsPagedMembersResult> GetPagedMembersAsync(IConnectorClient connectorClient, string conversationId, string continuationToken, CancellationToken cancellationToken, int? pageSize = default(int?))
{
if (conversationId == null)
{
throw new InvalidOperationException("The GetMembers operation needs a valid conversation Id.");
}
var pagedMemberResults = await connectorClient.Conversations.GetConversationPagedMembersAsync(conversationId, pageSize, continuationToken, cancellationToken).ConfigureAwait(false);
var teamsPagedMemberResults = new TeamsPagedMembersResult(pagedMemberResults.ContinuationToken, pagedMemberResults.Members);
return teamsPagedMemberResults;
}
private static ITeamsConnectorClient GetTeamsConnectorClient(ITurnContext turnContext)
{
var connectorClient = GetConnectorClient(turnContext);
if (connectorClient is ConnectorClient connectorClientImpl)
{
return new TeamsConnectorClient(connectorClientImpl.BaseUri, connectorClientImpl.Credentials, connectorClientImpl.HttpClient, connectorClientImpl.HttpClient == null);
}
else
{
return new TeamsConnectorClient(connectorClient.BaseUri, connectorClient.Credentials);
}
}
}
}