microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Api/Activities/Message/MessageActivity.cs
247lines · 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 | |
| 74 | public MessageActivity() : base(ActivityType.Message) |
| 75 | { |
| 76 | Text ??= string.Empty; |
| 77 | } |
| 78 | |
| 79 | public MessageActivity(string text) : base(ActivityType.Message) |
| 80 | { |
| 81 | Text = text; |
| 82 | } |
| 83 | |
| 84 | public MessageActivity WithText(string text) |
| 85 | { |
| 86 | Text = text; |
| 87 | return this; |
| 88 | } |
| 89 | |
| 90 | public MessageActivity WithSpeak(string speak) |
| 91 | { |
| 92 | Speak = speak; |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | public MessageActivity WithInputHint(InputHint inputHint) |
| 97 | { |
| 98 | InputHint = inputHint; |
| 99 | return this; |
| 100 | } |
| 101 | |
| 102 | public MessageActivity WithSummary(string summary) |
| 103 | { |
| 104 | Summary = summary; |
| 105 | return this; |
| 106 | } |
| 107 | |
| 108 | public MessageActivity WithTextFormat(TextFormat textFormat) |
| 109 | { |
| 110 | TextFormat = textFormat; |
| 111 | return this; |
| 112 | } |
| 113 | |
| 114 | public MessageActivity WithAttachmentLayout(Attachment.Layout attachmentLayout) |
| 115 | { |
| 116 | AttachmentLayout = attachmentLayout; |
| 117 | return this; |
| 118 | } |
| 119 | |
| 120 | public MessageActivity WithSuggestedActions(SuggestedActions suggestedActions) |
| 121 | { |
| 122 | SuggestedActions = suggestedActions; |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | public MessageActivity WithImportance(Importance importance) |
| 127 | { |
| 128 | Importance = importance; |
| 129 | return this; |
| 130 | } |
| 131 | |
| 132 | public MessageActivity WithDeliveryMode(DeliveryMode deliveryMode) |
| 133 | { |
| 134 | DeliveryMode = deliveryMode; |
| 135 | return this; |
| 136 | } |
| 137 | |
| 138 | public MessageActivity WithExpiration(DateTime expiration) |
| 139 | { |
| 140 | Expiration = expiration; |
| 141 | return this; |
| 142 | } |
| 143 | |
| 144 | public MessageActivity AddText(string text) |
| 145 | { |
| 146 | Text += text; |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | public override MessageActivity WithRecipient(Account value) |
| 151 | { |
| 152 | return (MessageActivity)base.WithRecipient(value); |
| 153 | } |
| 154 | |
| 155 | [Experimental("ExperimentalTeamsTargeted")] |
| 156 | #pragma warning disable ExperimentalTeamsTargeted |
| 157 | public override MessageActivity WithRecipient(Account value, bool isTargeted = false) |
| 158 | { |
| 159 | return (MessageActivity)base.WithRecipient(value, isTargeted); |
| 160 | } |
| 161 | #pragma warning restore ExperimentalTeamsTargeted |
| 162 | |
| 163 | public MessageActivity AddAttachment(params Attachment[] value) |
| 164 | { |
| 165 | Attachments ??= []; |
| 166 | |
| 167 | foreach (var attachment in value) |
| 168 | { |
| 169 | Attachments.Add(attachment); |
| 170 | } |
| 171 | |
| 172 | return this; |
| 173 | } |
| 174 | |
| 175 | public MessageActivity AddAttachment(Teams.Cards.AdaptiveCard card) |
| 176 | { |
| 177 | return AddAttachment(new Attachment(card)); |
| 178 | } |
| 179 | |
| 180 | public MessageActivity AddAttachment(Cards.OAuthCard card) |
| 181 | { |
| 182 | return AddAttachment(new Attachment(card)); |
| 183 | } |
| 184 | |
| 185 | public MessageActivity AddAttachment(Cards.SignInCard card) |
| 186 | { |
| 187 | return AddAttachment(new Attachment(card)); |
| 188 | } |
| 189 | |
| 190 | public MessageActivity AddMention(Account account, string? text = null, bool addText = true) |
| 191 | { |
| 192 | var mentionText = text ?? account.Name; |
| 193 | |
| 194 | if (addText) |
| 195 | { |
| 196 | Text = $"<at>{mentionText}</at> {Text}"; |
| 197 | } |
| 198 | |
| 199 | AddEntity(new MentionEntity() |
| 200 | { |
| 201 | Mentioned = account, |
| 202 | Text = $"<at>{mentionText}</at>" |
| 203 | }); |
| 204 | |
| 205 | return this; |
| 206 | } |
| 207 | |
| 208 | public MessageActivity AddStreamFinal() |
| 209 | { |
| 210 | ChannelData ??= new(); |
| 211 | ChannelData.StreamId ??= Id; |
| 212 | ChannelData.StreamType ??= StreamType.Final; |
| 213 | |
| 214 | AddEntity(new StreamInfoEntity() |
| 215 | { |
| 216 | StreamId = Id, |
| 217 | StreamType = StreamType.Final |
| 218 | }); |
| 219 | |
| 220 | return this; |
| 221 | } |
| 222 | |
| 223 | public MentionEntity? GetAccountMention(string accountId) |
| 224 | { |
| 225 | return (MentionEntity?)(Entities ?? []).FirstOrDefault(e => e is MentionEntity mention && mention.Mentioned.Id == accountId); |
| 226 | } |
| 227 | |
| 228 | public MessageActivity Merge(MessageActivity from) |
| 229 | { |
| 230 | base.Merge(from); |
| 231 | |
| 232 | Text ??= from.Text; |
| 233 | Speak ??= from.Speak; |
| 234 | InputHint ??= from.InputHint; |
| 235 | Summary ??= from.Summary; |
| 236 | TextFormat ??= from.TextFormat; |
| 237 | AttachmentLayout ??= from.AttachmentLayout; |
| 238 | SuggestedActions ??= from.SuggestedActions; |
| 239 | Importance ??= from.Importance; |
| 240 | DeliveryMode ??= from.DeliveryMode; |
| 241 | Expiration ??= from.Expiration; |
| 242 | Value ??= from.Value; |
| 243 | AddAttachment(from.Attachments?.ToArray() ?? []); |
| 244 | |
| 245 | return this; |
| 246 | } |
| 247 | } |