microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fix/msal-agentic-cache

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Apps.BotBuilder/ActivitySchemaMapper.cs

254lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Text;
5using System.Text.Json;
6using Microsoft.Bot.Builder.Integration.AspNet.Core.Handlers;
7using Microsoft.Bot.Schema;
8using Microsoft.Bot.Schema.Teams;
9using Microsoft.Teams.Core.Schema;
10using Newtonsoft.Json;
11
12namespace Microsoft.Teams.Apps.BotBuilder;
13
14/// <summary>
15/// Extension methods for converting between Bot Framework Activity and CoreActivity/TeamsActivity.
16/// </summary>
17public static class ActivitySchemaMapper
18{
19 private static string? GetStringValue(object? value) => value switch
20 {
21 null => null,
22 string s => s,
23 JsonElement { ValueKind: JsonValueKind.String } el => el.GetString(),
24 JsonElement el => el.GetRawText(),
25 _ => value.ToString()
26 };
27 /// <summary>
28 /// Converts a <see cref="CoreActivity"/> to a Bot Framework <see cref="Activity"/>.
29 /// </summary>
30 /// <param name="activity">The core activity to convert.</param>
31 /// <returns>The equivalent Bot Framework activity.</returns>
32 public static Activity ToBotFrameworkActivity(this CoreActivity activity)
33 {
34 ArgumentNullException.ThrowIfNull(activity);
35 using JsonTextReader reader = new(new StringReader(activity.ToJson()));
36 return BotMessageHandlerBase.BotMessageSerializer.Deserialize<Activity>(reader)!;
37 }
38
39 /// <summary>
40 /// Converts a Bot Framework <see cref="Activity"/> to a <see cref="CoreActivity"/>.
41 /// </summary>
42 /// <param name="activity">The Bot Framework activity to convert.</param>
43 /// <returns>The equivalent core activity.</returns>
44 public static CoreActivity FromBotFrameworkActivity(this Activity activity)
45 {
46 StringBuilder sb = new();
47 using StringWriter stringWriter = new(sb);
48 using JsonTextWriter json = new(stringWriter);
49 BotMessageHandlerBase.BotMessageSerializer.Serialize(json, activity);
50 string jsonString = sb.ToString();
51 return CoreActivity.FromJsonString(jsonString);
52 }
53
54
55 /// <summary>
56 /// Converts a <see cref="Microsoft.Teams.Core.Schema.ConversationAccount"/> to a Bot Framework <see cref="Microsoft.Bot.Schema.ChannelAccount"/>.
57 /// </summary>
58 /// <param name="account">The conversation account to convert.</param>
59 /// <returns>The equivalent channel account.</returns>
60 public static Microsoft.Bot.Schema.ChannelAccount ToCompatChannelAccount(this Microsoft.Teams.Core.Schema.ConversationAccount account)
61 {
62 ArgumentNullException.ThrowIfNull(account);
63
64 Microsoft.Bot.Schema.ChannelAccount channelAccount;
65
66 channelAccount = new()
67 {
68 Id = account.Id,
69 Name = account.Name
70 };
71
72
73 if (account.Properties.TryGetValue("aadObjectId", out object? aadObjectId))
74 {
75 channelAccount.AadObjectId = GetStringValue(aadObjectId);
76 }
77
78 if (account.Properties.TryGetValue("userRole", out object? userRole))
79 {
80 channelAccount.Role = GetStringValue(userRole);
81 }
82
83 if (account.Properties.TryGetValue("userPrincipalName", out object? userPrincipalName))
84 {
85 channelAccount.Properties.Add("userPrincipalName", GetStringValue(userPrincipalName) ?? string.Empty);
86 }
87
88 if (account.Properties.TryGetValue("givenName", out object? givenName))
89 {
90 channelAccount.Properties.Add("givenName", GetStringValue(givenName) ?? string.Empty);
91 }
92
93 if (account.Properties.TryGetValue("surname", out object? surname))
94 {
95 channelAccount.Properties.Add("surname", GetStringValue(surname) ?? string.Empty);
96 }
97
98 if (account.Properties.TryGetValue("email", out object? email))
99 {
100 channelAccount.Properties.Add("email", GetStringValue(email) ?? string.Empty);
101 }
102
103 if (account.Properties.TryGetValue("tenantId", out object? tenantId))
104 {
105 channelAccount.Properties.Add("tenantId", GetStringValue(tenantId) ?? string.Empty);
106 }
107
108 return channelAccount;
109 }
110
111 /// <summary>
112 /// Converts a <see cref="Microsoft.Teams.Core.Schema.ConversationAccount"/> to a <see cref="Microsoft.Bot.Schema.Teams.TeamsChannelAccount"/> with all known properties.
113 /// </summary>
114 /// <param name="account">The conversation account to convert.</param>
115 /// <returns>The equivalent Teams channel account.</returns>
116 public static Microsoft.Bot.Schema.Teams.TeamsChannelAccount ToCompatTeamsChannelAccount2(this Microsoft.Teams.Core.Schema.ConversationAccount account)
117 {
118 ArgumentNullException.ThrowIfNull(account);
119
120 return new Microsoft.Bot.Schema.Teams.TeamsChannelAccount
121 {
122 Id = account.Id,
123 Name = account.Name,
124 AadObjectId = GetStringValue(account.Properties["aadObjectId"]) ?? string.Empty,
125 Email = GetStringValue(account.Properties["email"]) ?? string.Empty,
126 GivenName = GetStringValue(account.Properties["givenName"]) ?? string.Empty,
127 Surname = GetStringValue(account.Properties["surname"]) ?? string.Empty,
128 UserPrincipalName = GetStringValue(account.Properties["userPrincipalName"]) ?? string.Empty,
129 UserRole = GetStringValue(account.Properties["userRole"]) ?? string.Empty,
130 TenantId = GetStringValue(account.Properties["tenantId"]) ?? string.Empty
131 };
132 }
133
134
135 /// <summary>
136 /// Converts a Core PagedMembersResult to a Bot Framework TeamsPagedMembersResult.
137 /// </summary>
138 /// <param name="pagedMembers">The paged members result to convert.</param>
139 /// <returns>The equivalent Bot Framework paged members result.</returns>
140 public static Microsoft.Bot.Schema.Teams.TeamsPagedMembersResult ToCompatTeamsPagedMembersResult(this Microsoft.Teams.Core.PagedMembersResult pagedMembers)
141 {
142 ArgumentNullException.ThrowIfNull(pagedMembers);
143
144 return new Microsoft.Bot.Schema.Teams.TeamsPagedMembersResult
145 {
146 ContinuationToken = pagedMembers.ContinuationToken,
147 Members = pagedMembers.Members?.Select(m => m.ToCompatTeamsChannelAccount()).ToList()
148 };
149 }
150
151 /// <summary>
152 /// Converts a <see cref="Microsoft.Teams.Core.Schema.ConversationAccount"/> to a <see cref="Microsoft.Bot.Schema.Teams.TeamsChannelAccount"/>.
153 /// </summary>
154 /// <param name="account">The conversation account to convert.</param>
155 /// <returns>The equivalent Teams channel account.</returns>
156 public static Microsoft.Bot.Schema.Teams.TeamsChannelAccount ToCompatTeamsChannelAccount(this Microsoft.Teams.Core.Schema.ConversationAccount account)
157 {
158 ArgumentNullException.ThrowIfNull(account);
159
160 TeamsChannelAccount teamsChannelAccount = new()
161 {
162 Id = account.Id,
163 Name = account.Name
164 };
165
166 // Extract properties from Properties dictionary
167 if (account.Properties.TryGetValue("aadObjectId", out object? aadObjectId))
168 {
169 teamsChannelAccount.AadObjectId = GetStringValue(aadObjectId);
170 }
171
172 if (account.Properties.TryGetValue("userPrincipalName", out object? userPrincipalName))
173 {
174 teamsChannelAccount.UserPrincipalName = GetStringValue(userPrincipalName);
175 }
176
177 if (account.Properties.TryGetValue("givenName", out object? givenName))
178 {
179 teamsChannelAccount.GivenName = GetStringValue(givenName);
180 }
181
182 if (account.Properties.TryGetValue("surname", out object? surname))
183 {
184 teamsChannelAccount.Surname = GetStringValue(surname);
185 }
186
187 if (account.Properties.TryGetValue("email", out object? email))
188 {
189 teamsChannelAccount.Email = GetStringValue(email);
190 }
191
192 if (account.Properties.TryGetValue("tenantId", out object? tenantId))
193 {
194 teamsChannelAccount.Properties.Add("tenantId", GetStringValue(tenantId) ?? string.Empty);
195 }
196
197 return teamsChannelAccount;
198 }
199
200 /// <summary>
201 /// Converts a Bot Framework ChannelAccount to a Core ConversationAccount.
202 /// </summary>
203 public static Microsoft.Teams.Core.Schema.ConversationAccount FromCompatChannelAccount(this Microsoft.Bot.Schema.ChannelAccount account)
204 {
205 ArgumentNullException.ThrowIfNull(account);
206
207 Microsoft.Teams.Core.Schema.ConversationAccount result = new() { Id = account.Id, Name = account.Name };
208
209 if (!string.IsNullOrEmpty(account.AadObjectId))
210 {
211 result.Properties["aadObjectId"] = account.AadObjectId;
212 }
213
214 if (!string.IsNullOrEmpty(account.Role))
215 {
216 result.Properties["userRole"] = account.Role;
217 }
218
219 return result;
220 }
221
222 /// <summary>
223 /// Converts a Bot Framework ConversationParameters to a Core ConversationParameters.
224 /// </summary>
225 public static Microsoft.Teams.Core.ConversationParameters FromCompatConversationParameters(this Microsoft.Bot.Schema.ConversationParameters parameters)
226 {
227 ArgumentNullException.ThrowIfNull(parameters);
228
229 return new Microsoft.Teams.Core.ConversationParameters
230 {
231 IsGroup = parameters.IsGroup,
232 Bot = parameters.Bot?.FromCompatChannelAccount(),
233 Members = parameters.Members?.Select(m => m.FromCompatChannelAccount()).ToList(),
234 TopicName = parameters.TopicName,
235 Activity = parameters.Activity?.FromBotFrameworkActivity(),
236 ChannelData = parameters.ChannelData,
237 TenantId = parameters.TenantId,
238 };
239 }
240
241 /// <summary>
242 /// Gets the TeamInfo object from the current activity.
243 /// </summary>
244 /// <param name="activity">The activity.</param>
245 /// <returns>The current activity's team's information, or null.</returns>
246 public static TeamInfo? TeamsGetTeamInfo(this IActivity activity)
247 {
248 ArgumentNullException.ThrowIfNull(activity);
249 Microsoft.Bot.Schema.Teams.TeamsChannelData channelData = activity.GetChannelData<Microsoft.Bot.Schema.Teams.TeamsChannelData>();
250 return channelData?.Team;
251 }
252
253
254}
255