microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
kavin/conversation-fix

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Bot.Apps/Schema/TeamsActivity.cs

209lines · modecode

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