microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Schema/TeamsActivity.cs
212lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json.Serialization; |
| 5 | using Microsoft.Teams.Bot.Apps.Schema.Entities; |
| 6 | using Microsoft.Teams.Bot.Core.Schema; |
| 7 | |
| 8 | namespace Microsoft.Teams.Bot.Apps.Schema; |
| 9 | |
| 10 | /// <summary> |
| 11 | /// Teams Activity schema. |
| 12 | /// </summary> |
| 13 | public class TeamsActivity : CoreActivity |
| 14 | { |
| 15 | /// <summary> |
| 16 | /// Creates a new instance of the TeamsActivity class from the specified Activity object. |
| 17 | /// </summary> |
| 18 | /// <param name="activity">The Activity instance to convert. Cannot be null.</param> |
| 19 | /// <returns>A TeamsActivity object that represents the specified Activity.</returns> |
| 20 | public static TeamsActivity FromActivity(CoreActivity activity) |
| 21 | { |
| 22 | ArgumentNullException.ThrowIfNull(activity); |
| 23 | |
| 24 | return TeamsActivityType.ActivityDeserializerMap.TryGetValue(activity.Type, out Func<CoreActivity, TeamsActivity>? factory) |
| 25 | ? factory(activity) |
| 26 | : new TeamsActivity(activity); // Fallback to base type |
| 27 | } |
| 28 | |
| 29 | /// <summary> |
| 30 | /// Overrides the ToJson method to serialize the TeamsActivity object to a JSON string. |
| 31 | /// Uses the appropriate JSON type info based on the actual runtime type. |
| 32 | /// </summary> |
| 33 | /// <returns>A JSON string representation of the activity using the type-specific serializer.</returns> |
| 34 | public override string ToJson() |
| 35 | => TeamsActivityType.ActivitySerializerMap.TryGetValue(GetType(), out Func<TeamsActivity, string>? serializer) |
| 36 | ? serializer(this) |
| 37 | : ToJson(TeamsActivityJsonContext.Default.TeamsActivity); |
| 38 | |
| 39 | /// <summary> |
| 40 | /// Constructor with type parameter. |
| 41 | /// </summary> |
| 42 | /// <param name="type"></param> |
| 43 | protected TeamsActivity(string type) : this() |
| 44 | { |
| 45 | Type = type; |
| 46 | } |
| 47 | |
| 48 | /// <summary> |
| 49 | /// Default constructor. |
| 50 | /// </summary> |
| 51 | [JsonConstructor] |
| 52 | public TeamsActivity() |
| 53 | { |
| 54 | Type = TeamsActivityType.Message; |
| 55 | } |
| 56 | |
| 57 | /// <summary> |
| 58 | /// Protected constructor to create TeamsActivity from CoreActivity. |
| 59 | /// Allows derived classes to call via base(activity). |
| 60 | /// </summary> |
| 61 | /// <param name="activity">The CoreActivity to convert.</param> |
| 62 | protected TeamsActivity(CoreActivity activity) : base(activity) |
| 63 | { |
| 64 | ArgumentNullException.ThrowIfNull(activity); |
| 65 | // Convert base types to Teams-specific types |
| 66 | if (activity.ChannelData is not null) |
| 67 | { |
| 68 | ChannelData = new TeamsChannelData(activity.ChannelData); |
| 69 | } |
| 70 | |
| 71 | if (activity.From is not null) |
| 72 | { |
| 73 | From = TeamsConversationAccount.FromConversationAccount(activity.From); |
| 74 | } |
| 75 | |
| 76 | if (activity.Recipient is not null) |
| 77 | { |
| 78 | Recipient = TeamsConversationAccount.FromConversationAccount(activity.Recipient); |
| 79 | } |
| 80 | |
| 81 | if (activity.Conversation is not null) |
| 82 | { |
| 83 | Conversation = TeamsConversation.FromConversation(activity.Conversation); |
| 84 | } |
| 85 | Attachments = TeamsAttachment.FromJArray(activity.Attachments); |
| 86 | Entities = EntityList.FromJsonArray(activity.Entities); |
| 87 | |
| 88 | Rebase(); |
| 89 | } |
| 90 | |
| 91 | /// <summary> |
| 92 | /// Resets shadow properties in base class |
| 93 | /// </summary> |
| 94 | /// <returns></returns> |
| 95 | internal TeamsActivity Rebase() |
| 96 | { |
| 97 | base.Attachments = Attachments?.ToJsonArray(); |
| 98 | base.Entities = Entities?.ToJsonArray(); |
| 99 | |
| 100 | return this; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /// <summary> |
| 105 | /// Gets or sets the account information for the sender of the Teams conversation. |
| 106 | /// </summary> |
| 107 | [JsonPropertyName("from")] |
| 108 | public new TeamsConversationAccount? From |
| 109 | { |
| 110 | get => base.From as TeamsConversationAccount; |
| 111 | set => base.From = value; |
| 112 | } |
| 113 | |
| 114 | /// <summary> |
| 115 | /// Gets or sets the account information for the recipient of the Teams conversation. |
| 116 | /// </summary> |
| 117 | [JsonPropertyName("recipient")] |
| 118 | public new TeamsConversationAccount? Recipient |
| 119 | { |
| 120 | get => base.Recipient as TeamsConversationAccount; |
| 121 | set => base.Recipient = value; |
| 122 | } |
| 123 | |
| 124 | /// <summary> |
| 125 | /// Gets or sets the conversation information for the Teams conversation. |
| 126 | /// </summary> |
| 127 | [JsonPropertyName("conversation")] |
| 128 | public new TeamsConversation? Conversation |
| 129 | { |
| 130 | get => base.Conversation as TeamsConversation; |
| 131 | set => base.Conversation = value; |
| 132 | } |
| 133 | |
| 134 | /// <summary> |
| 135 | /// Gets or sets the Teams-specific channel data associated with this activity. |
| 136 | /// </summary> |
| 137 | [JsonPropertyName("channelData")] |
| 138 | public new TeamsChannelData? ChannelData |
| 139 | { |
| 140 | get => base.ChannelData as TeamsChannelData; |
| 141 | set => base.ChannelData = value; |
| 142 | } |
| 143 | |
| 144 | /// <summary> |
| 145 | /// Gets or sets the entities specific to Teams. |
| 146 | /// </summary> |
| 147 | [JsonPropertyName("entities")] public new EntityList? Entities { get; set; } |
| 148 | |
| 149 | /// <summary> |
| 150 | /// Attachments specific to Teams. |
| 151 | /// </summary> |
| 152 | [JsonPropertyName("attachments")] public new IList<TeamsAttachment>? Attachments { get; set; } |
| 153 | |
| 154 | /// <summary> |
| 155 | /// UTC timestamp of when the activity was sent. |
| 156 | /// </summary> |
| 157 | [JsonPropertyName("timestamp")] |
| 158 | public string? Timestamp { get; set; } |
| 159 | |
| 160 | /// <summary> |
| 161 | /// Local timestamp of when the activity was sent, including timezone offset. |
| 162 | /// </summary> |
| 163 | [JsonPropertyName("localTimestamp")] |
| 164 | public string? LocalTimestamp { get; set; } |
| 165 | |
| 166 | /// <summary> |
| 167 | /// Locale of the activity set by the client (e.g., "en-US"). |
| 168 | /// </summary> |
| 169 | [JsonPropertyName("locale")] |
| 170 | public string? Locale { get; set; } |
| 171 | |
| 172 | /// <summary> |
| 173 | /// Local timezone of the client (e.g., "America/Los_Angeles"). |
| 174 | /// </summary> |
| 175 | [JsonPropertyName("localTimezone")] |
| 176 | public string? LocalTimezone { get; set; } |
| 177 | |
| 178 | /// <summary> |
| 179 | /// Gets or sets the suggested actions for the message. |
| 180 | /// </summary> |
| 181 | [JsonPropertyName("suggestedActions")] |
| 182 | public SuggestedActions? SuggestedActions { get; set; } |
| 183 | |
| 184 | |
| 185 | /// <summary> |
| 186 | /// Adds an entity to the activity's Entities collection. |
| 187 | /// </summary> |
| 188 | /// <param name="entity"></param> |
| 189 | /// <returns></returns> |
| 190 | public TeamsActivity AddEntity(Entity entity) |
| 191 | { |
| 192 | // TODO: Pick up nuances about entities. |
| 193 | // For eg, there can only be 1 single MessageEntity |
| 194 | Entities ??= []; |
| 195 | Entities.Add(entity); |
| 196 | return this; |
| 197 | } |
| 198 | |
| 199 | /// <summary> |
| 200 | /// Creates a new TeamsActivityBuilder instance for building a TeamsActivity with a fluent API. |
| 201 | /// </summary> |
| 202 | /// <returns>A new TeamsActivityBuilder instance.</returns> |
| 203 | public static new TeamsActivityBuilder CreateBuilder() => new(); |
| 204 | |
| 205 | /// <summary> |
| 206 | /// Creates a new TeamsActivityBuilder instance initialized with the specified TeamsActivity. |
| 207 | /// </summary> |
| 208 | /// <param name="activity"></param> |
| 209 | /// <returns></returns> |
| 210 | public static TeamsActivityBuilder CreateBuilder(TeamsActivity activity) => new(activity); |
| 211 | |
| 212 | } |
| 213 | |