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