microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Handlers/AdaptiveCardHandler.Response.cs
136lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json.Serialization; |
| 5 | |
| 6 | namespace Microsoft.Teams.Bot.Apps.Handlers; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Adaptive card response types. |
| 10 | /// </summary> |
| 11 | public static class AdaptiveCardResponseType |
| 12 | { |
| 13 | /// <summary> |
| 14 | /// Message type - displays a message to the user. |
| 15 | /// </summary> |
| 16 | public const string Message = "application/vnd.microsoft.activity.message"; |
| 17 | |
| 18 | /// <summary> |
| 19 | /// Card type - updates the card with new content. |
| 20 | /// </summary> |
| 21 | public const string Card = "application/vnd.microsoft.card.adaptive"; |
| 22 | } |
| 23 | |
| 24 | /// <summary> |
| 25 | /// |
| 26 | /// Response for adaptive card action activities. |
| 27 | /// </summary> |
| 28 | public class AdaptiveCardResponse |
| 29 | { |
| 30 | /// <summary> |
| 31 | /// HTTP status code for the response. |
| 32 | /// </summary> |
| 33 | [JsonPropertyName("statusCode")] |
| 34 | public int StatusCode { get; set; } = 200; |
| 35 | |
| 36 | /// <summary> |
| 37 | /// Type of response. See <see cref="AdaptiveCardResponseType"/> for common values. |
| 38 | /// </summary> |
| 39 | [JsonPropertyName("type")] |
| 40 | [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| 41 | public string? Type { get; set; } |
| 42 | |
| 43 | /// <summary> |
| 44 | /// Value for the response. Can be a string message or card content. |
| 45 | /// </summary> |
| 46 | [JsonPropertyName("value")] |
| 47 | [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| 48 | public object? Value { get; set; } |
| 49 | |
| 50 | /// <summary> |
| 51 | /// Creates a new builder for AdaptiveCardResponse. |
| 52 | /// </summary> |
| 53 | public static AdaptiveCardResponseBuilder CreateBuilder() |
| 54 | { |
| 55 | return new AdaptiveCardResponseBuilder(); |
| 56 | } |
| 57 | |
| 58 | /// <summary> |
| 59 | /// Creates a InvokeResponse with a message response. |
| 60 | /// </summary> |
| 61 | /// <param name="message">The message to display to the user.</param> |
| 62 | /// <param name="statusCode">The HTTP status code (default: 200).</param> |
| 63 | public static InvokeResponse<AdaptiveCardResponse> CreateMessageResponse(string message, int statusCode = 200) |
| 64 | { |
| 65 | return new InvokeResponse<AdaptiveCardResponse>(statusCode, new AdaptiveCardResponse |
| 66 | { |
| 67 | StatusCode = statusCode, |
| 68 | Type = AdaptiveCardResponseType.Message, |
| 69 | Value = message |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | /// <summary> |
| 74 | /// Creates a InvokeResponse with a card response. |
| 75 | /// </summary> |
| 76 | /// <param name="card">The card content to display.</param> |
| 77 | /// <param name="statusCode">The HTTP status code (default: 200).</param> |
| 78 | public static InvokeResponse<AdaptiveCardResponse> CreateCardResponse(object card, int statusCode = 200) |
| 79 | { |
| 80 | return new InvokeResponse<AdaptiveCardResponse>(statusCode, new AdaptiveCardResponse |
| 81 | { |
| 82 | StatusCode = statusCode, |
| 83 | Type = AdaptiveCardResponseType.Card, |
| 84 | Value = card |
| 85 | }); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// <summary> |
| 90 | /// Builder for AdaptiveCardResponse. |
| 91 | /// </summary> |
| 92 | public class AdaptiveCardResponseBuilder |
| 93 | { |
| 94 | private int _statusCode = 200; |
| 95 | private string? _type; |
| 96 | private object? _value; |
| 97 | |
| 98 | ///<summary> |
| 99 | ///</summary> |
| 100 | public AdaptiveCardResponseBuilder WithStatusCode(int statusCode) |
| 101 | { |
| 102 | _statusCode = statusCode; |
| 103 | return this; |
| 104 | } |
| 105 | |
| 106 | /// <summary> |
| 107 | /// Sets the type of the response. See <see cref="AdaptiveCardResponseType"/> for common values. |
| 108 | /// </summary> |
| 109 | public AdaptiveCardResponseBuilder WithType(string type) |
| 110 | { |
| 111 | _type = type; |
| 112 | return this; |
| 113 | } |
| 114 | |
| 115 | /// <summary> |
| 116 | /// Sets the value for the response. |
| 117 | /// </summary> |
| 118 | public AdaptiveCardResponseBuilder WithValue(object value) |
| 119 | { |
| 120 | _value = value; |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | /// <summary> |
| 125 | /// Builds the AdaptiveCardResponse and wraps it in a InvokeResponse. |
| 126 | /// </summary> |
| 127 | public InvokeResponse<AdaptiveCardResponse> Build() |
| 128 | { |
| 129 | return new InvokeResponse<AdaptiveCardResponse>(_statusCode, new AdaptiveCardResponse |
| 130 | { |
| 131 | StatusCode = _statusCode, |
| 132 | Type = _type, |
| 133 | Value = _value |
| 134 | }); |
| 135 | } |
| 136 | } |
| 137 | |