microsoft/teams.net

Public

mirrored from https://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

Libraries/Microsoft.Teams.Api/Activities/Message/MessageActivity.cs

313lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using System.Diagnostics.CodeAnalysis;
5using System.Text.Json.Serialization;
6
7using Microsoft.Teams.Api.Entities;
8using Microsoft.Teams.Common;
9
10namespace Microsoft.Teams.Api.Activities;
11
12public partial class ActivityType : StringEnum
13{
14 public static readonly ActivityType Message = new("message");
15 public bool IsMessage => Message.Equals(Value);
16}
17
18public class MessageActivity : Activity
19{
20 [JsonPropertyName("text")]
21 [JsonPropertyOrder(31)]
22 public string Text { get; set; }
23
24 [JsonPropertyName("speak")]
25 [JsonPropertyOrder(32)]
26 [Obsolete("This will be removed by end of summer 2026.")]
27 public string? Speak { get; set; }
28
29 [JsonPropertyName("inputHint")]
30 [JsonPropertyOrder(33)]
31 [Obsolete("This will be removed by end of summer 2026.")]
32 public InputHint? InputHint { get; set; }
33
34 [JsonPropertyName("summary")]
35 [JsonPropertyOrder(34)]
36 public string? Summary { get; set; }
37
38 [JsonPropertyName("textFormat")]
39 [JsonPropertyOrder(35)]
40 public TextFormat? TextFormat { get; set; }
41
42 [JsonPropertyName("attachmentLayout")]
43 [JsonPropertyOrder(121)]
44 public Attachment.Layout? AttachmentLayout { get; set; }
45
46 [JsonPropertyName("attachments")]
47 [JsonPropertyOrder(122)]
48 public IList<Attachment>? Attachments { get; set; }
49
50 [JsonPropertyName("suggestedActions")]
51 [JsonPropertyOrder(123)]
52 public SuggestedActions? SuggestedActions { get; set; }
53
54 [JsonPropertyName("importance")]
55 [JsonPropertyOrder(39)]
56 [Obsolete("This will be removed by end of summer 2026.")]
57 public Importance? Importance { get; set; }
58
59 [JsonPropertyName("deliveryMode")]
60 [JsonPropertyOrder(41)]
61 public DeliveryMode? DeliveryMode { get; set; }
62
63 [JsonPropertyName("expiration")]
64 [JsonPropertyOrder(42)]
65 [Obsolete("This will be removed by end of summer 2026.")]
66 public DateTime? Expiration { get; set; }
67
68 [JsonPropertyName("value")]
69 [JsonPropertyOrder(43)]
70 public object? Value { get; set; }
71
72 [JsonIgnore]
73 public bool IsRecipientMentioned
74 {
75 get => (Entities ?? []).Any(e => e is MentionEntity mention && mention.Mentioned.Id == Recipient.Id);
76 }
77 /// <summary>
78 /// Get all quoted reply entities from this message.
79 /// </summary>
80 public IReadOnlyList<QuotedReplyEntity> GetQuotedMessages()
81 {
82 return Entities?.OfType<QuotedReplyEntity>().ToList()
83 ?? new List<QuotedReplyEntity>();
84 }
85
86 public MessageActivity() : base(ActivityType.Message)
87 {
88 Text ??= string.Empty;
89 }
90
91 public MessageActivity(string text) : base(ActivityType.Message)
92 {
93 Text = text;
94 }
95
96 public MessageActivity WithText(string text)
97 {
98 Text = text;
99 return this;
100 }
101
102 [Obsolete("This will be removed by end of summer 2026.")]
103 public MessageActivity WithSpeak(string speak)
104 {
105#pragma warning disable CS0618
106 Speak = speak;
107#pragma warning restore CS0618
108 return this;
109 }
110
111 [Obsolete("This will be removed by end of summer 2026.")]
112 public MessageActivity WithInputHint(InputHint inputHint)
113 {
114#pragma warning disable CS0618
115 InputHint = inputHint;
116#pragma warning restore CS0618
117 return this;
118 }
119
120 public MessageActivity WithSummary(string summary)
121 {
122 Summary = summary;
123 return this;
124 }
125
126 public MessageActivity WithTextFormat(TextFormat textFormat)
127 {
128 TextFormat = textFormat;
129 return this;
130 }
131
132 public MessageActivity WithAttachmentLayout(Attachment.Layout attachmentLayout)
133 {
134 AttachmentLayout = attachmentLayout;
135 return this;
136 }
137
138 public MessageActivity WithSuggestedActions(SuggestedActions suggestedActions)
139 {
140 SuggestedActions = suggestedActions;
141 return this;
142 }
143
144 [Obsolete("This will be removed by end of summer 2026.")]
145 public MessageActivity WithImportance(Importance importance)
146 {
147#pragma warning disable CS0618
148 Importance = importance;
149#pragma warning restore CS0618
150 return this;
151 }
152
153 public MessageActivity WithDeliveryMode(DeliveryMode deliveryMode)
154 {
155 DeliveryMode = deliveryMode;
156 return this;
157 }
158
159 [Obsolete("This will be removed by end of summer 2026.")]
160 public MessageActivity WithExpiration(DateTime expiration)
161 {
162#pragma warning disable CS0618
163 Expiration = expiration;
164#pragma warning restore CS0618
165 return this;
166 }
167
168 public MessageActivity AddText(string text)
169 {
170 Text += text;
171 return this;
172 }
173
174 public override MessageActivity WithRecipient(Account value)
175 {
176 return (MessageActivity)base.WithRecipient(value);
177 }
178
179 [Experimental("ExperimentalTeamsTargeted")]
180#pragma warning disable ExperimentalTeamsTargeted
181 public override MessageActivity WithRecipient(Account value, bool isTargeted = false)
182 {
183 return (MessageActivity)base.WithRecipient(value, isTargeted);
184 }
185#pragma warning restore ExperimentalTeamsTargeted
186
187 /// <summary>
188 /// Add a quoted message reference and append a placeholder to text.
189 /// Teams renders the quoted message as a preview bubble above the response text.
190 /// If text is provided, it is appended to the quoted message placeholder.
191 /// </summary>
192 /// <param name="messageId">the ID of the message to quote</param>
193 /// <param name="text">optional text, appended to the quoted message placeholder</param>
194 public MessageActivity AddQuote(string messageId, string? text = null)
195 {
196 Entities ??= new List<IEntity>();
197 Entities.Add(new QuotedReplyEntity
198 {
199 QuotedReply = new QuotedReplyData { MessageId = messageId }
200 });
201 AddText($"<quoted messageId=\"{messageId}\"/>");
202 if (text != null)
203 {
204 AddText($" {text}");
205 }
206 return this;
207 }
208
209 /// <summary>
210 /// Prepend a QuotedReply entity and placeholder before existing text.
211 /// Used by Reply()/Quote() for quote-above-response.
212 /// </summary>
213 public MessageActivity PrependQuote(string messageId)
214 {
215 Entities ??= new List<IEntity>();
216 Entities.Add(new QuotedReplyEntity
217 {
218 QuotedReply = new QuotedReplyData { MessageId = messageId }
219 });
220 var placeholder = $"<quoted messageId=\"{messageId}\"/>";
221 var hasText = !string.IsNullOrWhiteSpace(Text);
222 Text = hasText ? $"{placeholder} {Text}" : placeholder;
223 return this;
224 }
225
226
227 public MessageActivity AddAttachment(params Attachment[] value)
228 {
229 Attachments ??= [];
230
231 foreach (var attachment in value)
232 {
233 Attachments.Add(attachment);
234 }
235
236 return this;
237 }
238
239 public MessageActivity AddAttachment(Teams.Cards.AdaptiveCard card)
240 {
241 return AddAttachment(new Attachment(card));
242 }
243
244 public MessageActivity AddAttachment(Cards.OAuthCard card)
245 {
246 return AddAttachment(new Attachment(card));
247 }
248
249 public MessageActivity AddAttachment(Cards.SignInCard card)
250 {
251 return AddAttachment(new Attachment(card));
252 }
253
254 public MessageActivity AddMention(Account account, string? text = null, bool addText = true)
255 {
256 var mentionText = text ?? account.Name;
257
258 if (addText)
259 {
260 Text = $"<at>{mentionText}</at> {Text}";
261 }
262
263 AddEntity(new MentionEntity()
264 {
265 Mentioned = account,
266 Text = $"<at>{mentionText}</at>"
267 });
268
269 return this;
270 }
271
272 public MessageActivity AddStreamFinal()
273 {
274 ChannelData ??= new();
275 ChannelData.StreamId ??= Id;
276 ChannelData.StreamType = StreamType.Final;
277
278 AddEntity(new StreamInfoEntity()
279 {
280 StreamId = Id,
281 StreamType = StreamType.Final
282 });
283
284 return this;
285 }
286
287 public MentionEntity? GetAccountMention(string accountId)
288 {
289 return (MentionEntity?)(Entities ?? []).FirstOrDefault(e => e is MentionEntity mention && mention.Mentioned.Id == accountId);
290 }
291
292 public MessageActivity Merge(MessageActivity from)
293 {
294 base.Merge(from);
295
296 Text ??= from.Text;
297#pragma warning disable CS0618
298 Speak ??= from.Speak;
299 InputHint ??= from.InputHint;
300 Importance ??= from.Importance;
301 Expiration ??= from.Expiration;
302#pragma warning restore CS0618
303 Summary ??= from.Summary;
304 TextFormat ??= from.TextFormat;
305 AttachmentLayout ??= from.AttachmentLayout;
306 SuggestedActions ??= from.SuggestedActions;
307 DeliveryMode ??= from.DeliveryMode;
308 Value ??= from.Value;
309 AddAttachment(from.Attachments?.ToArray() ?? []);
310
311 return this;
312 }
313}