microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Apps/Schema/TeamsActivity.cs
163lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json.Serialization; |
| 5 | using Microsoft.Teams.Apps.Schema.Entities; |
| 6 | using Microsoft.Teams.Core.Schema; |
| 7 | |
| 8 | namespace Microsoft.Teams.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 core extension properties to Teams-specific typed properties. |
| 66 | // CoreActivity stores these as untyped entries in its Properties dictionary |
| 67 | // (via [JsonExtensionData]), so we extract and promote them here. |
| 68 | base.From = TeamsConversationAccount.FromConversationAccount(activity.From) ?? new TeamsConversationAccount(); |
| 69 | base.Recipient = TeamsConversationAccount.FromConversationAccount(activity.Recipient) ?? new TeamsConversationAccount(); |
| 70 | base.Conversation = TeamsConversation.FromConversation(activity.Conversation) ?? new TeamsConversation(); |
| 71 | ChannelData = activity.Properties.Extract<TeamsChannelData>("channelData"); |
| 72 | Entities = activity.Properties.Extract<EntityList>("entities"); |
| 73 | } |
| 74 | |
| 75 | /// <summary> |
| 76 | /// Gets or sets the account information for the sender of the Teams conversation. |
| 77 | /// Delegates to the base CoreActivity.From slot, casting to TeamsConversationAccount. |
| 78 | /// </summary> |
| 79 | [JsonPropertyName("from")] |
| 80 | public new TeamsConversationAccount? From |
| 81 | { |
| 82 | get => base.From as TeamsConversationAccount ?? TeamsConversationAccount.FromConversationAccount(base.From); |
| 83 | set => base.From = value; |
| 84 | } |
| 85 | |
| 86 | /// <summary> |
| 87 | /// Gets or sets the account information for the recipient of the Teams conversation. |
| 88 | /// Delegates to the base CoreActivity.Recipient slot, casting to TeamsConversationAccount. |
| 89 | /// </summary> |
| 90 | [JsonPropertyName("recipient")] |
| 91 | public new TeamsConversationAccount? Recipient |
| 92 | { |
| 93 | get => base.Recipient as TeamsConversationAccount ?? TeamsConversationAccount.FromConversationAccount(base.Recipient); |
| 94 | set => base.Recipient = value; |
| 95 | } |
| 96 | |
| 97 | /// <summary> |
| 98 | /// Gets or sets the conversation information for the Teams conversation. |
| 99 | /// Delegates to the base CoreActivity.Conversation slot, casting to TeamsConversation. |
| 100 | /// </summary> |
| 101 | [JsonPropertyName("conversation")] |
| 102 | public new TeamsConversation? Conversation |
| 103 | { |
| 104 | get => base.Conversation as TeamsConversation ?? TeamsConversation.FromConversation(base.Conversation); |
| 105 | set => base.Conversation = value!; |
| 106 | } |
| 107 | |
| 108 | /// <summary> |
| 109 | /// Gets or sets the Teams-specific channel data associated with this activity. |
| 110 | /// </summary> |
| 111 | [JsonPropertyName("channelData")] |
| 112 | public TeamsChannelData? ChannelData { get; set; } |
| 113 | |
| 114 | /// <summary> |
| 115 | /// Gets or sets the entities specific to Teams. |
| 116 | /// </summary> |
| 117 | [JsonPropertyName("entities")] |
| 118 | public EntityList? Entities { get; set; } |
| 119 | |
| 120 | /// <summary> |
| 121 | /// UTC timestamp of when the activity was sent. |
| 122 | /// </summary> |
| 123 | [JsonPropertyName("timestamp")] |
| 124 | public string? Timestamp { get; set; } |
| 125 | |
| 126 | /// <summary> |
| 127 | /// Local timestamp of when the activity was sent, including timezone offset. |
| 128 | /// </summary> |
| 129 | [JsonPropertyName("localTimestamp")] |
| 130 | public string? LocalTimestamp { get; set; } |
| 131 | |
| 132 | /// <summary> |
| 133 | /// Locale of the activity set by the client (e.g., "en-US"). |
| 134 | /// </summary> |
| 135 | [JsonPropertyName("locale")] |
| 136 | public string? Locale { get; set; } |
| 137 | |
| 138 | /// <summary> |
| 139 | /// Local timezone of the client (e.g., "America/Los_Angeles"). |
| 140 | /// </summary> |
| 141 | [JsonPropertyName("localTimezone")] |
| 142 | public string? LocalTimezone { get; set; } |
| 143 | |
| 144 | /// <summary> |
| 145 | /// Gets or sets the suggested actions for the message. |
| 146 | /// </summary> |
| 147 | [JsonPropertyName("suggestedActions")] |
| 148 | public SuggestedActions? SuggestedActions { get; set; } |
| 149 | |
| 150 | /// <summary> |
| 151 | /// Creates a new TeamsActivityBuilder instance for building a TeamsActivity with a fluent API. |
| 152 | /// </summary> |
| 153 | /// <returns>A new TeamsActivityBuilder instance.</returns> |
| 154 | public static new TeamsActivityBuilder CreateBuilder() => new(); |
| 155 | |
| 156 | /// <summary> |
| 157 | /// Creates a new TeamsActivityBuilder instance initialized with the specified TeamsActivity. |
| 158 | /// </summary> |
| 159 | /// <param name="activity"></param> |
| 160 | /// <returns></returns> |
| 161 | public static TeamsActivityBuilder CreateBuilder(TeamsActivity activity) => new(activity); |
| 162 | |
| 163 | } |
| 164 | |