microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Api/Activities/Message/MessageActivity.cs
233lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json.Serialization; |
| 5 | |
| 6 | using Microsoft.Teams.Api.Entities; |
| 7 | using Microsoft.Teams.Common; |
| 8 | |
| 9 | namespace Microsoft.Teams.Api.Activities; |
| 10 | |
| 11 | public partial class ActivityType : StringEnum |
| 12 | { |
| 13 | public static readonly ActivityType Message = new("message"); |
| 14 | public bool IsMessage => Message.Equals(Value); |
| 15 | } |
| 16 | |
| 17 | public class MessageActivity : Activity |
| 18 | { |
| 19 | [JsonPropertyName("text")] |
| 20 | [JsonPropertyOrder(31)] |
| 21 | public string Text { get; set; } |
| 22 | |
| 23 | [JsonPropertyName("speak")] |
| 24 | [JsonPropertyOrder(32)] |
| 25 | public string? Speak { get; set; } |
| 26 | |
| 27 | [JsonPropertyName("inputHint")] |
| 28 | [JsonPropertyOrder(33)] |
| 29 | public InputHint? InputHint { get; set; } |
| 30 | |
| 31 | [JsonPropertyName("summary")] |
| 32 | [JsonPropertyOrder(34)] |
| 33 | public string? Summary { get; set; } |
| 34 | |
| 35 | [JsonPropertyName("textFormat")] |
| 36 | [JsonPropertyOrder(35)] |
| 37 | public TextFormat? TextFormat { get; set; } |
| 38 | |
| 39 | [JsonPropertyName("attachmentLayout")] |
| 40 | [JsonPropertyOrder(121)] |
| 41 | public Attachment.Layout? AttachmentLayout { get; set; } |
| 42 | |
| 43 | [JsonPropertyName("attachments")] |
| 44 | [JsonPropertyOrder(122)] |
| 45 | public IList<Attachment>? Attachments { get; set; } |
| 46 | |
| 47 | [JsonPropertyName("suggestedActions")] |
| 48 | [JsonPropertyOrder(123)] |
| 49 | public SuggestedActions? SuggestedActions { get; set; } |
| 50 | |
| 51 | [JsonPropertyName("importance")] |
| 52 | [JsonPropertyOrder(39)] |
| 53 | public Importance? Importance { get; set; } |
| 54 | |
| 55 | [JsonPropertyName("deliveryMode")] |
| 56 | [JsonPropertyOrder(41)] |
| 57 | public DeliveryMode? DeliveryMode { get; set; } |
| 58 | |
| 59 | [JsonPropertyName("expiration")] |
| 60 | [JsonPropertyOrder(42)] |
| 61 | public DateTime? Expiration { get; set; } |
| 62 | |
| 63 | [JsonPropertyName("value")] |
| 64 | [JsonPropertyOrder(43)] |
| 65 | public object? Value { get; set; } |
| 66 | |
| 67 | [JsonIgnore] |
| 68 | public bool IsRecipientMentioned |
| 69 | { |
| 70 | get => (Entities ?? []).Any(e => e is MentionEntity mention && mention.Mentioned.Id == Recipient.Id); |
| 71 | } |
| 72 | |
| 73 | public MessageActivity() : base(ActivityType.Message) |
| 74 | { |
| 75 | Text ??= string.Empty; |
| 76 | } |
| 77 | |
| 78 | public MessageActivity(string text) : base(ActivityType.Message) |
| 79 | { |
| 80 | Text = text; |
| 81 | } |
| 82 | |
| 83 | public MessageActivity WithText(string text) |
| 84 | { |
| 85 | Text = text; |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | public MessageActivity WithSpeak(string speak) |
| 90 | { |
| 91 | Speak = speak; |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | public MessageActivity WithInputHint(InputHint inputHint) |
| 96 | { |
| 97 | InputHint = inputHint; |
| 98 | return this; |
| 99 | } |
| 100 | |
| 101 | public MessageActivity WithSummary(string summary) |
| 102 | { |
| 103 | Summary = summary; |
| 104 | return this; |
| 105 | } |
| 106 | |
| 107 | public MessageActivity WithTextFormat(TextFormat textFormat) |
| 108 | { |
| 109 | TextFormat = textFormat; |
| 110 | return this; |
| 111 | } |
| 112 | |
| 113 | public MessageActivity WithAttachmentLayout(Attachment.Layout attachmentLayout) |
| 114 | { |
| 115 | AttachmentLayout = attachmentLayout; |
| 116 | return this; |
| 117 | } |
| 118 | |
| 119 | public MessageActivity WithSuggestedActions(SuggestedActions suggestedActions) |
| 120 | { |
| 121 | SuggestedActions = suggestedActions; |
| 122 | return this; |
| 123 | } |
| 124 | |
| 125 | public MessageActivity WithImportance(Importance importance) |
| 126 | { |
| 127 | Importance = importance; |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | public MessageActivity WithDeliveryMode(DeliveryMode deliveryMode) |
| 132 | { |
| 133 | DeliveryMode = deliveryMode; |
| 134 | return this; |
| 135 | } |
| 136 | |
| 137 | public MessageActivity WithExpiration(DateTime expiration) |
| 138 | { |
| 139 | Expiration = expiration; |
| 140 | return this; |
| 141 | } |
| 142 | |
| 143 | public MessageActivity AddText(string text) |
| 144 | { |
| 145 | Text += text; |
| 146 | return this; |
| 147 | } |
| 148 | |
| 149 | public MessageActivity AddAttachment(params Attachment[] value) |
| 150 | { |
| 151 | Attachments ??= []; |
| 152 | |
| 153 | foreach (var attachment in value) |
| 154 | { |
| 155 | Attachments.Add(attachment); |
| 156 | } |
| 157 | |
| 158 | return this; |
| 159 | } |
| 160 | |
| 161 | public MessageActivity AddAttachment(Teams.Cards.AdaptiveCard card) |
| 162 | { |
| 163 | return AddAttachment(new Attachment(card)); |
| 164 | } |
| 165 | |
| 166 | public MessageActivity AddAttachment(Cards.OAuthCard card) |
| 167 | { |
| 168 | return AddAttachment(new Attachment(card)); |
| 169 | } |
| 170 | |
| 171 | public MessageActivity AddAttachment(Cards.SignInCard card) |
| 172 | { |
| 173 | return AddAttachment(new Attachment(card)); |
| 174 | } |
| 175 | |
| 176 | public MessageActivity AddMention(Account account, string? text = null, bool addText = true) |
| 177 | { |
| 178 | var mentionText = text ?? account.Name; |
| 179 | |
| 180 | if (addText) |
| 181 | { |
| 182 | Text = $"<at>{mentionText}</at> {Text}"; |
| 183 | } |
| 184 | |
| 185 | AddEntity(new MentionEntity() |
| 186 | { |
| 187 | Mentioned = account, |
| 188 | Text = $"<at>{mentionText}</at>" |
| 189 | }); |
| 190 | |
| 191 | return this; |
| 192 | } |
| 193 | |
| 194 | public MessageActivity AddStreamFinal() |
| 195 | { |
| 196 | ChannelData ??= new(); |
| 197 | ChannelData.StreamId ??= Id; |
| 198 | ChannelData.StreamType ??= StreamType.Final; |
| 199 | |
| 200 | AddEntity(new StreamInfoEntity() |
| 201 | { |
| 202 | StreamId = Id, |
| 203 | StreamType = StreamType.Final |
| 204 | }); |
| 205 | |
| 206 | return this; |
| 207 | } |
| 208 | |
| 209 | public MentionEntity? GetAccountMention(string accountId) |
| 210 | { |
| 211 | return (MentionEntity?)(Entities ?? []).FirstOrDefault(e => e is MentionEntity mention && mention.Mentioned.Id == accountId); |
| 212 | } |
| 213 | |
| 214 | public MessageActivity Merge(MessageActivity from) |
| 215 | { |
| 216 | base.Merge(from); |
| 217 | |
| 218 | Text ??= from.Text; |
| 219 | Speak ??= from.Speak; |
| 220 | InputHint ??= from.InputHint; |
| 221 | Summary ??= from.Summary; |
| 222 | TextFormat ??= from.TextFormat; |
| 223 | AttachmentLayout ??= from.AttachmentLayout; |
| 224 | SuggestedActions ??= from.SuggestedActions; |
| 225 | Importance ??= from.Importance; |
| 226 | DeliveryMode ??= from.DeliveryMode; |
| 227 | Expiration ??= from.Expiration; |
| 228 | Value ??= from.Value; |
| 229 | AddAttachment(from.Attachments?.ToArray() ?? []); |
| 230 | |
| 231 | return this; |
| 232 | } |
| 233 | } |