microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Schema/TeamsAttachment.cs
172lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | using System.Text.Json.Nodes; |
| 6 | using System.Text.Json.Serialization; |
| 7 | using Microsoft.Teams.Bot.Core.Schema; |
| 8 | |
| 9 | namespace Microsoft.Teams.Bot.Apps.Schema; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// Teams attachment content types. |
| 13 | /// </summary> |
| 14 | public static class AttachmentContentType |
| 15 | { |
| 16 | /// <summary> |
| 17 | /// Adaptive Card content type. |
| 18 | /// </summary> |
| 19 | public const string AdaptiveCard = "application/vnd.microsoft.card.adaptive"; |
| 20 | |
| 21 | /// <summary> |
| 22 | /// Hero Card content type. |
| 23 | /// </summary> |
| 24 | public const string HeroCard = "application/vnd.microsoft.card.hero"; |
| 25 | |
| 26 | /// <summary> |
| 27 | /// Thumbnail Card content type. |
| 28 | /// </summary> |
| 29 | public const string ThumbnailCard = "application/vnd.microsoft.card.thumbnail"; |
| 30 | |
| 31 | /// <summary> |
| 32 | /// Office 365 Connector Card content type. |
| 33 | /// </summary> |
| 34 | public const string O365ConnectorCard = "application/vnd.microsoft.teams.card.o365connector"; |
| 35 | |
| 36 | /// <summary> |
| 37 | /// File consent card content type. |
| 38 | /// </summary> |
| 39 | public const string FileConsentCard = "application/vnd.microsoft.teams.card.file.consent"; |
| 40 | |
| 41 | /// <summary> |
| 42 | /// File info card content type. |
| 43 | /// </summary> |
| 44 | public const string FileInfoCard = "application/vnd.microsoft.teams.card.file.info"; |
| 45 | |
| 46 | //TODO : verify these |
| 47 | /* |
| 48 | /// <summary> |
| 49 | /// Receipt Card content type. |
| 50 | /// </summary> |
| 51 | public const string ReceiptCard = "application/vnd.microsoft.card.receipt"; |
| 52 | |
| 53 | /// <summary> |
| 54 | /// Signin Card content type. |
| 55 | /// </summary> |
| 56 | public const string SigninCard = "application/vnd.microsoft.card.signin"; |
| 57 | |
| 58 | /// <summary> |
| 59 | /// Animation content type. |
| 60 | /// </summary> |
| 61 | public const string Animation = "application/vnd.microsoft.card.animation"; |
| 62 | |
| 63 | /// <summary> |
| 64 | /// Audio content type. |
| 65 | /// </summary> |
| 66 | public const string Audio = "application/vnd.microsoft.card.audio"; |
| 67 | |
| 68 | /// <summary> |
| 69 | /// Video content type. |
| 70 | /// </summary> |
| 71 | public const string Video = "application/vnd.microsoft.card.video"; |
| 72 | */ |
| 73 | } |
| 74 | |
| 75 | /// <summary> |
| 76 | /// Attachment layout types. |
| 77 | /// </summary> |
| 78 | public static class TeamsAttachmentLayout |
| 79 | { |
| 80 | /// <summary> |
| 81 | /// List layout - displays attachments in a vertical list. |
| 82 | /// </summary> |
| 83 | public const string List = "list"; |
| 84 | |
| 85 | /// <summary> |
| 86 | /// Grid layout - displays attachments in a grid. |
| 87 | /// </summary> |
| 88 | public const string Grid = "grid"; |
| 89 | |
| 90 | /// <summary> |
| 91 | /// Carousel layout - displays attachments in a horizontal carousel. |
| 92 | /// </summary> |
| 93 | public const string Carousel = "carousel"; |
| 94 | } |
| 95 | |
| 96 | /// <summary> |
| 97 | /// Extension methods for TeamsAttachment. |
| 98 | /// </summary> |
| 99 | public static class TeamsAttachmentExtensions |
| 100 | { |
| 101 | static internal JsonArray ToJsonArray(this IList<TeamsAttachment> attachments) |
| 102 | { |
| 103 | JsonArray jsonArray = []; |
| 104 | foreach (TeamsAttachment attachment in attachments) |
| 105 | { |
| 106 | JsonNode jsonNode = JsonSerializer.SerializeToNode(attachment)!; |
| 107 | jsonArray.Add(jsonNode); |
| 108 | } |
| 109 | return jsonArray; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// <summary> |
| 114 | /// Teams attachment model. |
| 115 | /// </summary> |
| 116 | public class TeamsAttachment |
| 117 | { |
| 118 | static internal IList<TeamsAttachment>? FromJArray(JsonArray? jsonArray) |
| 119 | { |
| 120 | if (jsonArray is null) |
| 121 | { |
| 122 | return null; |
| 123 | } |
| 124 | List<TeamsAttachment> attachments = []; |
| 125 | foreach (JsonNode? item in jsonArray) |
| 126 | { |
| 127 | attachments.Add(item.Deserialize<TeamsAttachment>()!); |
| 128 | } |
| 129 | return attachments; |
| 130 | } |
| 131 | |
| 132 | /// <summary> |
| 133 | /// Content of the attachment. |
| 134 | /// </summary> |
| 135 | [JsonPropertyName("contentType")] public string ContentType { get; set; } = string.Empty; |
| 136 | |
| 137 | /// <summary> |
| 138 | /// Content URL of the attachment. |
| 139 | /// </summary> |
| 140 | [JsonPropertyName("contentUrl")] public Uri? ContentUrl { get; set; } |
| 141 | |
| 142 | /// <summary> |
| 143 | /// Content for the Attachment |
| 144 | /// </summary> |
| 145 | [JsonPropertyName("content")] public object? Content { get; set; } |
| 146 | |
| 147 | /// <summary> |
| 148 | /// Gets or sets the name of the attachment. |
| 149 | /// </summary> |
| 150 | [JsonPropertyName("name")] public string? Name { get; set; } |
| 151 | |
| 152 | /// <summary> |
| 153 | /// Gets or sets the thumbnail URL of the attachment. |
| 154 | /// </summary> |
| 155 | [JsonPropertyName("thumbnailUrl")] public Uri? ThumbnailUrl { get; set; } |
| 156 | |
| 157 | /// <summary> |
| 158 | /// Extension data for additional properties not explicitly defined by the type. |
| 159 | /// </summary> |
| 160 | [JsonExtensionData] public ExtendedPropertiesDictionary Properties { get; set; } = []; |
| 161 | |
| 162 | /// <summary> |
| 163 | /// Creates a builder for constructing a <see cref="TeamsAttachment"/> instance. |
| 164 | /// </summary> |
| 165 | public static TeamsAttachmentBuilder CreateBuilder() => new(); |
| 166 | |
| 167 | /// <summary> |
| 168 | /// Creates a builder initialized with an existing <see cref="TeamsAttachment"/> instance. |
| 169 | /// </summary> |
| 170 | /// <param name="attachment">The attachment to wrap.</param> |
| 171 | public static TeamsAttachmentBuilder CreateBuilder(TeamsAttachment attachment) => new(attachment); |
| 172 | } |