microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
next/oauth-card-null-ref-bug

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Bot.Compat/CompatActivity.cs

325lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Text;
5using Microsoft.Bot.Builder.Integration.AspNet.Core.Handlers;
6using Microsoft.Bot.Schema;
7using Microsoft.Bot.Schema.Teams;
8using Microsoft.Teams.Bot.Apps.Schema;
9using Microsoft.Teams.Bot.Core.Schema;
10using Newtonsoft.Json;
11
12namespace Microsoft.Teams.Bot.Compat;
13
14/// <summary>
15/// Extension methods for converting between Bot Framework Activity and CoreActivity/TeamsActivity.
16/// </summary>
17public static class CompatActivity
18{
19 /// <summary>
20 /// Converts a CoreActivity to a Bot Framework Activity.
21 /// </summary>
22 /// <param name="activity"></param>
23 /// <returns></returns>
24 public static Activity ToCompatActivity(this CoreActivity activity)
25 {
26 ArgumentNullException.ThrowIfNull(activity);
27 using JsonTextReader reader = new(new StringReader(activity.ToJson()));
28 return BotMessageHandlerBase.BotMessageSerializer.Deserialize<Activity>(reader)!;
29 }
30
31 /// <summary>
32 /// Converts a Bot Framework Activity to a TeamsActivity.
33 /// </summary>
34 /// <param name="activity"></param>
35 /// <returns></returns>
36 public static CoreActivity FromCompatActivity(this Activity activity)
37 {
38 StringBuilder sb = new();
39 using StringWriter stringWriter = new(sb);
40 using JsonTextWriter json = new(stringWriter);
41 BotMessageHandlerBase.BotMessageSerializer.Serialize(json, activity);
42 string jsonString = sb.ToString();
43 return CoreActivity.FromJsonString(jsonString);
44 }
45
46
47 /// <summary>
48 /// Converts a ConversationAccount to a ChannelAccount.
49 /// </summary>
50 /// <param name="account"></param>
51 /// <returns></returns>
52 public static Microsoft.Bot.Schema.ChannelAccount ToCompatChannelAccount(this Microsoft.Teams.Bot.Core.Schema.ConversationAccount account)
53 {
54 ArgumentNullException.ThrowIfNull(account);
55
56 Microsoft.Bot.Schema.ChannelAccount channelAccount;
57 if (account is TeamsConversationAccount tae)
58 {
59 channelAccount = new()
60 {
61 Id = account.Id,
62 Name = account.Name,
63 AadObjectId = tae.AadObjectId
64 };
65 }
66 else
67 {
68 channelAccount = new()
69 {
70 Id = account.Id,
71 Name = account.Name
72 };
73 }
74
75 if (account.Properties.TryGetValue("aadObjectId", out object? aadObjectId))
76 {
77 channelAccount.AadObjectId = aadObjectId?.ToString();
78 }
79
80 if (account.Properties.TryGetValue("userRole", out object? userRole))
81 {
82 channelAccount.Role = userRole?.ToString();
83 }
84
85 if (account.Properties.TryGetValue("userPrincipalName", out object? userPrincipalName))
86 {
87 channelAccount.Properties.Add("userPrincipalName", userPrincipalName?.ToString() ?? string.Empty);
88 }
89
90 if (account.Properties.TryGetValue("givenName", out object? givenName))
91 {
92 channelAccount.Properties.Add("givenName", givenName?.ToString() ?? string.Empty);
93 }
94
95 if (account.Properties.TryGetValue("surname", out object? surname))
96 {
97 channelAccount.Properties.Add("surname", surname?.ToString() ?? string.Empty);
98 }
99
100 if (account.Properties.TryGetValue("email", out object? email))
101 {
102 channelAccount.Properties.Add("email", email?.ToString() ?? string.Empty);
103 }
104
105 if (account.Properties.TryGetValue("tenantId", out object? tenantId))
106 {
107 channelAccount.Properties.Add("tenantId", tenantId?.ToString() ?? string.Empty);
108 }
109
110 return channelAccount;
111 }
112
113 /// <summary>
114 /// Converts a TeamsConversationAccount to a TeamsChannelAccount.
115 /// </summary>
116 /// <param name="account"></param>
117 /// <returns></returns>
118 public static Microsoft.Bot.Schema.Teams.TeamsChannelAccount ToCompatTeamsChannelAccount(this Microsoft.Teams.Bot.Apps.Schema.TeamsConversationAccount account)
119 {
120 ArgumentNullException.ThrowIfNull(account);
121
122 return new Microsoft.Bot.Schema.Teams.TeamsChannelAccount
123 {
124 Id = account.Id,
125 Name = account.Name,
126 AadObjectId = account.AadObjectId,
127 Email = account.Email,
128 GivenName = account.GivenName,
129 Surname = account.Surname,
130 UserPrincipalName = account.UserPrincipalName,
131 UserRole = account.UserRole,
132 TenantId = account.TenantId
133 };
134 }
135
136 /// <summary>
137 /// Converts a Core MeetingInfo to a Bot Framework MeetingInfo.
138 /// </summary>
139 /// <param name="meetingInfo"></param>
140 /// <returns></returns>
141 public static Microsoft.Bot.Schema.Teams.MeetingInfo ToCompatMeetingInfo(this Microsoft.Teams.Bot.Apps.MeetingInfo meetingInfo)
142 {
143 ArgumentNullException.ThrowIfNull(meetingInfo);
144
145 return new Microsoft.Bot.Schema.Teams.MeetingInfo
146 {
147 Details = meetingInfo.Details != null ? new Microsoft.Bot.Schema.Teams.MeetingDetails
148 {
149 Id = meetingInfo.Details.Id,
150 MsGraphResourceId = meetingInfo.Details.MsGraphResourceId,
151 ScheduledStartTime = meetingInfo.Details.ScheduledStartTime?.DateTime,
152 ScheduledEndTime = meetingInfo.Details.ScheduledEndTime?.DateTime,
153 JoinUrl = meetingInfo.Details.JoinUrl,
154 Title = meetingInfo.Details.Title,
155 Type = meetingInfo.Details.Type
156 } : null,
157 Conversation = meetingInfo.Conversation != null ? new Microsoft.Bot.Schema.ConversationAccount
158 {
159 Id = meetingInfo.Conversation.Id,
160 Name = meetingInfo.Conversation.Name
161 } : null,
162 Organizer = meetingInfo.Organizer != null ? meetingInfo.Organizer.ToCompatTeamsChannelAccount() : null
163 };
164 }
165
166 /// <summary>
167 /// Converts a Core MeetingParticipant to a Bot Framework TeamsMeetingParticipant.
168 /// </summary>
169 /// <param name="participant"></param>
170 /// <returns></returns>
171 public static Microsoft.Bot.Schema.Teams.TeamsMeetingParticipant ToCompatTeamsMeetingParticipant(this Microsoft.Teams.Bot.Apps.MeetingParticipant participant)
172 {
173 ArgumentNullException.ThrowIfNull(participant);
174
175 return new Microsoft.Bot.Schema.Teams.TeamsMeetingParticipant
176 {
177 User = participant.User != null ? participant.User.ToCompatTeamsChannelAccount() : null,
178 Meeting = participant.Meeting != null ? new Microsoft.Bot.Schema.Teams.MeetingParticipantInfo
179 {
180 Role = participant.Meeting.Role,
181 InMeeting = participant.Meeting.InMeeting
182 } : null,
183 Conversation = participant.Conversation != null ? new Microsoft.Bot.Schema.ConversationAccount
184 {
185 Id = participant.Conversation.Id
186 } : null
187 };
188 }
189
190 /// <summary>
191 /// Converts a Core TeamsChannel to a Bot Framework ChannelInfo.
192 /// </summary>
193 /// <param name="channel"></param>
194 /// <returns></returns>
195 public static Microsoft.Bot.Schema.Teams.ChannelInfo ToCompatChannelInfo(this Microsoft.Teams.Bot.Apps.Schema.TeamsChannel channel)
196 {
197 ArgumentNullException.ThrowIfNull(channel);
198
199 return new Microsoft.Bot.Schema.Teams.ChannelInfo
200 {
201 Id = channel.Id,
202 Name = channel.Name
203 };
204 }
205
206 /// <summary>
207 /// Converts a Core PagedMembersResult to a Bot Framework TeamsPagedMembersResult.
208 /// </summary>
209 /// <param name="pagedMembers"></param>
210 /// <returns></returns>
211 public static Microsoft.Bot.Schema.Teams.TeamsPagedMembersResult ToCompatTeamsPagedMembersResult(this Microsoft.Teams.Bot.Core.PagedMembersResult pagedMembers)
212 {
213 ArgumentNullException.ThrowIfNull(pagedMembers);
214
215 return new Microsoft.Bot.Schema.Teams.TeamsPagedMembersResult
216 {
217 ContinuationToken = pagedMembers.ContinuationToken,
218 Members = pagedMembers.Members?.Select(m => m.ToCompatTeamsChannelAccount()).ToList()
219 };
220 }
221
222 /// <summary>
223 /// Converts a ConversationAccount to a TeamsChannelAccount.
224 /// </summary>
225 /// <param name="account"></param>
226 /// <returns></returns>
227 public static Microsoft.Bot.Schema.Teams.TeamsChannelAccount ToCompatTeamsChannelAccount(this Microsoft.Teams.Bot.Core.Schema.ConversationAccount account)
228 {
229 ArgumentNullException.ThrowIfNull(account);
230
231 TeamsChannelAccount teamsChannelAccount = new()
232 {
233 Id = account.Id,
234 Name = account.Name
235 };
236
237 // Extract properties from Properties dictionary
238 if (account.Properties.TryGetValue("aadObjectId", out object? aadObjectId))
239 {
240 teamsChannelAccount.AadObjectId = aadObjectId?.ToString();
241 }
242
243 if (account.Properties.TryGetValue("userPrincipalName", out object? userPrincipalName))
244 {
245 teamsChannelAccount.UserPrincipalName = userPrincipalName?.ToString();
246 }
247
248 if (account.Properties.TryGetValue("givenName", out object? givenName))
249 {
250 teamsChannelAccount.GivenName = givenName?.ToString();
251 }
252
253 if (account.Properties.TryGetValue("surname", out object? surname))
254 {
255 teamsChannelAccount.Surname = surname?.ToString();
256 }
257
258 if (account.Properties.TryGetValue("email", out object? email))
259 {
260 teamsChannelAccount.Email = email?.ToString();
261 }
262
263 if (account.Properties.TryGetValue("tenantId", out object? tenantId))
264 {
265 teamsChannelAccount.Properties.Add("tenantId", tenantId?.ToString() ?? string.Empty);
266 }
267
268 return teamsChannelAccount;
269 }
270
271 /// <summary>
272 /// Converts a Bot Framework ChannelAccount to a Core ConversationAccount.
273 /// </summary>
274 public static Microsoft.Teams.Bot.Core.Schema.ConversationAccount FromCompatChannelAccount(this Microsoft.Bot.Schema.ChannelAccount account)
275 {
276 ArgumentNullException.ThrowIfNull(account);
277
278 Microsoft.Teams.Bot.Core.Schema.ConversationAccount result = new() { Id = account.Id, Name = account.Name };
279
280 if (!string.IsNullOrEmpty(account.AadObjectId))
281 {
282 result.Properties["aadObjectId"] = account.AadObjectId;
283 }
284
285 if (!string.IsNullOrEmpty(account.Role))
286 {
287 result.Properties["userRole"] = account.Role;
288 }
289
290 return result;
291 }
292
293 /// <summary>
294 /// Converts a Bot Framework ConversationParameters to a Core ConversationParameters.
295 /// </summary>
296 public static Microsoft.Teams.Bot.Core.ConversationParameters FromCompatConversationParameters(this Microsoft.Bot.Schema.ConversationParameters parameters)
297 {
298 ArgumentNullException.ThrowIfNull(parameters);
299
300 return new Microsoft.Teams.Bot.Core.ConversationParameters
301 {
302 IsGroup = parameters.IsGroup,
303 Bot = parameters.Bot?.FromCompatChannelAccount(),
304 Members = parameters.Members?.Select(m => m.FromCompatChannelAccount()).ToList(),
305 TopicName = parameters.TopicName,
306 Activity = parameters.Activity?.FromCompatActivity(),
307 ChannelData = parameters.ChannelData,
308 TenantId = parameters.TenantId,
309 };
310 }
311
312 /// <summary>
313 /// Gets the TeamInfo object from the current activity.
314 /// </summary>
315 /// <param name="activity">The activity.</param>
316 /// <returns>The current activity's team's information, or null.</returns>
317 public static TeamInfo? TeamsGetTeamInfo(this IActivity activity)
318 {
319 ArgumentNullException.ThrowIfNull(activity);
320 Microsoft.Bot.Schema.Teams.TeamsChannelData channelData = activity.GetChannelData<Microsoft.Bot.Schema.Teams.TeamsChannelData>();
321 return channelData?.Team;
322 }
323
324
325}
326