// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Text.Json.Serialization;
namespace Microsoft.Teams.Apps.Handlers;
///
/// Adaptive card response types.
///
public static class AdaptiveCardResponseTypes
{
///
/// Message type - displays a message to the user.
///
public const string Message = "application/vnd.microsoft.activity.message";
///
/// Card type - updates the card with new content.
///
public const string Card = "application/vnd.microsoft.card.adaptive";
}
///
/// Response for adaptive card action activities.
///
public class AdaptiveCardResponse
{
///
/// HTTP status code for the response.
///
[JsonPropertyName("statusCode")]
public int StatusCode { get; set; } = 200;
///
/// Type of response. See for common values.
///
[JsonPropertyName("type")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Type { get; set; }
///
/// Value for the response. Can be a string message or card content.
///
[JsonPropertyName("value")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object? Value { get; set; }
///
/// Creates a new builder for AdaptiveCardResponse.
///
public static AdaptiveCardResponseBuilder CreateBuilder()
{
return new AdaptiveCardResponseBuilder();
}
///
/// Creates an with a message response.
///
/// The message to display to the user.
/// The HTTP status code (default: 200).
public static InvokeResponse CreateMessageResponse(string message, int statusCode = 200)
{
return new InvokeResponse(statusCode, new AdaptiveCardResponse
{
StatusCode = statusCode,
Type = AdaptiveCardResponseTypes.Message,
Value = message
});
}
///
/// Creates an with a card response.
///
/// The card content to display.
/// The HTTP status code (default: 200).
public static InvokeResponse CreateCardResponse(object card, int statusCode = 200)
{
return new InvokeResponse(statusCode, new AdaptiveCardResponse
{
StatusCode = statusCode,
Type = AdaptiveCardResponseTypes.Card,
Value = card
});
}
}
///
/// Builder for AdaptiveCardResponse.
///
public class AdaptiveCardResponseBuilder
{
private int _statusCode = 200;
private string? _type;
private object? _value;
///
/// Sets the HTTP status code for the response.
///
public AdaptiveCardResponseBuilder WithStatusCode(int statusCode)
{
_statusCode = statusCode;
return this;
}
///
/// Sets the type of the response. See for common values.
///
public AdaptiveCardResponseBuilder WithType(string type)
{
_type = type;
return this;
}
///
/// Sets the value for the response.
///
public AdaptiveCardResponseBuilder WithValue(object value)
{
_value = value;
return this;
}
///
/// Builds the and wraps it in an .
///
public InvokeResponse Build()
{
return new InvokeResponse(_statusCode, new AdaptiveCardResponse
{
StatusCode = _statusCode,
Type = _type,
Value = _value
});
}
}