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