microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
docs/update-release-process

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Core/Schema/CoreActivity.cs

257lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Text.Json;
5using System.Text.Json.Serialization.Metadata;
6
7namespace Microsoft.Teams.Core.Schema;
8
9/// <summary>
10/// Represents a dictionary for storing extended properties as key-value pairs.
11/// </summary>
12public class ExtendedPropertiesDictionary : Dictionary<string, object?>
13{
14 /// <summary>
15 /// Initializes a new empty instance of the <see cref="ExtendedPropertiesDictionary"/> class.
16 /// </summary>
17 public ExtendedPropertiesDictionary() { }
18
19 /// <summary>
20 /// Initializes a new instance of the <see cref="ExtendedPropertiesDictionary"/> class by shallow-copying entries from another dictionary.
21 /// </summary>
22 public ExtendedPropertiesDictionary(IDictionary<string, object?> source) : base(source) { }
23
24 /// <summary>
25 /// Extracts and deserializes a value from the dictionary, removing the entry if found.
26 /// Returns the deserialized value, or default if the key is not present.
27 /// </summary>
28 public T? Extract<T>(string key)
29 {
30 if (!TryGetValue(key, out object? raw))
31 return default;
32
33 Remove(key);
34
35 if (raw is T typed)
36 return typed;
37
38 if (raw is System.Text.Json.JsonElement element)
39 return System.Text.Json.JsonSerializer.Deserialize<T>(element.GetRawText());
40
41 return default;
42 }
43
44 /// <summary>
45 /// Gets and deserializes a value from the dictionary without removing it.
46 /// Handles <see cref="System.Text.Json.JsonElement"/> values that result from deserialization.
47 /// </summary>
48 public T? Get<T>(string key)
49 {
50 if (!TryGetValue(key, out object? raw))
51 return default;
52
53 if (raw is T typed)
54 return typed;
55
56 if (raw is System.Text.Json.JsonElement element)
57 {
58 T? deserialized = System.Text.Json.JsonSerializer.Deserialize<T>(element.GetRawText());
59 this[key] = deserialized;
60 return deserialized;
61 }
62
63 return default;
64 }
65}
66
67/// <summary>
68/// Represents a core activity object that encapsulates the data and metadata for a bot interaction.
69/// </summary>
70/// <remarks>
71/// This class provides the foundational structure for bot activities including message exchanges,
72/// conversation updates, and other bot-related events. It supports serialization to and from JSON
73/// and includes extension properties for channel-specific data.
74/// Follows the Activity Protocol Specification: https://github.com/microsoft/Agents/blob/main/specs/activity/protocol-activity.md
75/// </remarks>
76public class CoreActivity
77{
78 /// <summary>
79 /// Gets or sets the type of the activity. See <see cref="ActivityType"/> for common values.
80 /// </summary>
81 /// <remarks>
82 /// Common activity types include "message", "conversationUpdate", "contactRelationUpdate", etc.
83 /// </remarks>
84 [JsonPropertyName("type")] public string Type { get; set; }
85 /// <summary>
86 /// Gets or sets the unique identifier for the channel on which this activity is occurring.
87 /// </summary>
88 [JsonPropertyName("channelId")] public string? ChannelId { get; set; }
89 /// <summary>
90 /// Gets or sets the unique identifier for the activity.
91 /// </summary>
92 [JsonPropertyName("id")] public string? Id { get; set; }
93 /// <summary>
94 /// Gets or sets the URL of the service endpoint for this activity.
95 /// </summary>
96 /// <remarks>
97 /// This URL is used to send responses back to the channel.
98 /// </remarks>
99 [JsonPropertyName("serviceUrl")] public Uri? ServiceUrl { get; set; }
100
101 /// <summary>
102 /// Gets or sets the identifier of the activity this activity is a reply to.
103 /// </summary>
104 [JsonPropertyName("replyToId")] public string? ReplyToId { get; set; }
105
106 /// <summary>
107 /// Gets or sets the conversation information for this activity.
108 /// </summary>
109 [JsonPropertyName("conversation")] public Conversation? Conversation { get; set; }
110
111 /// <summary>
112 /// Gets or sets the sender account for this activity.
113 /// </summary>
114 [JsonPropertyName("from")] public ChannelAccount? From { get; set; }
115
116 /// <summary>
117 /// Gets or sets the recipient account for this activity.
118 /// </summary>
119 [JsonPropertyName("recipient")] public ChannelAccount? Recipient { get; set; }
120
121 /// <summary>
122 /// Gets the extension data dictionary for storing additional properties not defined in the schema.
123 /// </summary>
124 [JsonExtensionData] public ExtendedPropertiesDictionary Properties { get; set; } = [];
125
126 /// <summary>
127 /// Gets the JSON serializer options used for reflection-based serialization of extended activity types.
128 /// </summary>
129 /// <remarks>
130 /// Uses reflection-based serialization to support custom activity types that extend CoreActivity.
131 /// This is used when serializing/deserializing types not registered in the source-generated context.
132 /// </remarks>
133 private static readonly JsonSerializerOptions ReflectionJsonOptions = new()
134 {
135 WriteIndented = true,
136 DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
137 PropertyNamingPolicy = JsonNamingPolicy.CamelCase
138 };
139
140 /// <summary>
141 /// Creates a new instance of the <see cref="CoreActivity"/> class with the specified activity type.
142 /// Defaults to <see cref="ActivityType.Message"/>.
143 /// </summary>
144 /// <param name="type">The activity type. Defaults to "message".</param>
145 [JsonConstructor]
146 public CoreActivity(string type = ActivityType.Message)
147 {
148 Type = type;
149 }
150
151 /// <summary>
152 /// Creates a new instance of the <see cref="CoreActivity"/> class by copying properties from another activity.
153 /// </summary>
154 /// <param name="activity">The source activity to copy from.</param>
155 protected CoreActivity(CoreActivity activity)
156 {
157 ArgumentNullException.ThrowIfNull(activity);
158
159 Id = activity.Id;
160 ServiceUrl = activity.ServiceUrl;
161 ChannelId = activity.ChannelId;
162 Type = activity.Type;
163 Conversation = activity.Conversation is not null ? new Conversation(activity.Conversation.Id) { Properties = new ExtendedPropertiesDictionary(activity.Conversation.Properties) } : null;
164 From = activity.From is not null ? CloneChannelAccount(activity.From) : null;
165 Recipient = activity.Recipient is not null ? CloneChannelAccount(activity.Recipient) : null;
166 Properties = new ExtendedPropertiesDictionary(activity.Properties);
167 }
168
169#pragma warning disable ExperimentalTeamsTargeted
170 private static ChannelAccount CloneChannelAccount(ChannelAccount source) => new()
171 {
172 Id = source.Id,
173 Name = source.Name,
174 IsTargeted = source.IsTargeted,
175 AgenticAppId = source.AgenticAppId,
176 AgenticUserId = source.AgenticUserId,
177 AgenticAppBlueprintId = source.AgenticAppBlueprintId,
178 Properties = new ExtendedPropertiesDictionary(source.Properties)
179 };
180#pragma warning restore ExperimentalTeamsTargeted
181
182 /// <summary>
183 /// Serializes the current activity to a JSON string.
184 /// </summary>
185 /// <returns>A JSON string representation of the activity.</returns>
186 public virtual string ToJson()
187 => JsonSerializer.Serialize(this, CoreActivityJsonContext.Default.CoreActivity);
188
189 /// <summary>
190 /// Serializes the current activity to a JSON string using the specified <see cref="JsonTypeInfo{T}"/> for source-generated serialization.
191 /// </summary>
192 /// <typeparam name="T">The type of the activity to serialize. Must inherit from <see cref="CoreActivity"/>.</typeparam>
193 /// <param name="jsonTypeInfo">The JSON type info that provides serialization metadata for type <typeparamref name="T"/>.</param>
194 /// <returns>A JSON string representation of the activity.</returns>
195 public string ToJson<T>(JsonTypeInfo<T> jsonTypeInfo) where T : CoreActivity
196 => JsonSerializer.Serialize(this, jsonTypeInfo);
197
198 /// <summary>
199 /// Serializes the specified activity instance to a JSON string using reflection-based serialization.
200 /// </summary>
201 /// <remarks>Uses reflection-based serialization to support custom activity types that extend
202 /// <see cref="CoreActivity"/>. The resulting JSON reflects the public properties of the activity instance.</remarks>
203 /// <typeparam name="T">The type of the activity to serialize. Must inherit from CoreActivity.</typeparam>
204 /// <param name="instance">The activity instance to serialize. Cannot be null.</param>
205 /// <returns>A JSON string representation of the specified activity instance.</returns>
206 public static string ToJson<T>(T instance) where T : CoreActivity
207 => JsonSerializer.Serialize<T>(instance, ReflectionJsonOptions);
208
209 /// <summary>
210 /// Deserializes a JSON string into a <see cref="CoreActivity"/> object.
211 /// </summary>
212 /// <param name="json">The JSON string to deserialize.</param>
213 /// <returns>A <see cref="CoreActivity"/> instance.</returns>
214 public static CoreActivity FromJsonString(string json)
215 => JsonSerializer.Deserialize(json, CoreActivityJsonContext.Default.CoreActivity)!;
216
217 /// <summary>
218 /// Asynchronously deserializes a JSON stream into a <see cref="CoreActivity"/> object.
219 /// </summary>
220 /// <param name="stream">The stream containing JSON data to deserialize.</param>
221 /// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
222 /// <returns>A task that represents the asynchronous operation. The task result contains the deserialized <see cref="CoreActivity"/> instance, or null if deserialization fails.</returns>
223 public static ValueTask<CoreActivity?> FromJsonStreamAsync(Stream stream, CancellationToken cancellationToken = default)
224 => JsonSerializer.DeserializeAsync(stream, CoreActivityJsonContext.Default.CoreActivity, cancellationToken);
225
226 /// <summary>
227 /// Asynchronously deserializes a JSON stream into an instance of type <typeparamref name="T"/> using the specified <see cref="JsonTypeInfo{T}"/> for source-generated serialization.
228 /// </summary>
229 /// <typeparam name="T">The type of the activity to deserialize. Must inherit from <see cref="CoreActivity"/>.</typeparam>
230 /// <param name="stream">The stream containing JSON data to deserialize.</param>
231 /// <param name="jsonTypeInfo">The JSON type info that provides deserialization metadata for type <typeparamref name="T"/>.</param>
232 /// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
233 /// <returns>A <see cref="ValueTask{T}"/> representing the asynchronous operation. The result contains the deserialized activity, or null if deserialization fails.</returns>
234 public static ValueTask<T?> FromJsonStreamAsync<T>(Stream stream, JsonTypeInfo<T> jsonTypeInfo, CancellationToken cancellationToken = default) where T : CoreActivity
235 => JsonSerializer.DeserializeAsync(stream, jsonTypeInfo, cancellationToken);
236
237 /// <summary>
238 /// Asynchronously deserializes a JSON value from the specified stream into an instance of type T.
239 /// </summary>
240 /// <remarks>The caller is responsible for managing the lifetime of the provided stream. The method uses
241 /// default JSON serialization options.</remarks>
242 /// <typeparam name="T">The type of the object to deserialize. Must derive from CoreActivity.</typeparam>
243 /// <param name="stream">The stream containing the JSON data to deserialize. The stream must be readable and positioned at the start of
244 /// the JSON content.</param>
245 /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
246 /// <returns>A ValueTask that represents the asynchronous operation. The result contains an instance of type T if
247 /// deserialization is successful; otherwise, null.</returns>
248 public static ValueTask<T?> FromJsonStreamAsync<T>(Stream stream, CancellationToken cancellationToken = default) where T : CoreActivity
249 => JsonSerializer.DeserializeAsync<T>(stream, ReflectionJsonOptions, cancellationToken);
250
251 /// <summary>
252 /// Creates a new instance of the <see cref="CoreActivityBuilder"/> to construct activity instances.
253 /// </summary>
254 /// <returns>A new <see cref="CoreActivityBuilder"/> instance.</returns>
255 public static CoreActivityBuilder CreateBuilder() => new();
256
257}
258