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