// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Text.Json.Serialization;
using Microsoft.Teams.Apps.Schema.Entities;
using Microsoft.Teams.Core.Schema;
namespace Microsoft.Teams.Apps.Schema;
///
/// Represents a message activity.
///
public class MessageActivity : TeamsActivity
{
///
/// Convenience method to create a MessageActivity from a CoreActivity.
///
/// The CoreActivity to convert.
/// A MessageActivity instance.
public static new MessageActivity FromActivity(CoreActivity activity)
{
ArgumentNullException.ThrowIfNull(activity);
return new MessageActivity(activity);
}
///
/// Default constructor.
///
[JsonConstructor]
public MessageActivity() : base(TeamsActivityTypes.Message)
{
}
///
/// Initializes a new instance of the class with the specified text.
///
/// The text content of the message.
public MessageActivity(string text) : base(TeamsActivityTypes.Message)
{
Text = text;
}
///
/// Initializes a new instance of the class with the specified text.
///
/// The list of attachments for the message.
public MessageActivity(IList attachments) : base(TeamsActivityTypes.Message)
{
Attachments = attachments;
}
///
/// Internal constructor to create MessageActivity from CoreActivity.
///
/// The CoreActivity to convert.
internal MessageActivity(CoreActivity activity) : base(activity)
{
Attachments = activity.Properties.Extract>("attachments");
Text = activity.Properties.Extract("text");
TextFormat = activity.Properties.Extract("textFormat");
AttachmentLayout = activity.Properties.Extract("attachmentLayout");
SuggestedActions = activity.Properties.Extract("suggestedActions");
}
///
/// Gets or sets the attachments for the message.
///
[JsonPropertyName("attachments")]
public IList? Attachments { get; set; }
///
/// Gets or sets the text content of the message.
///
[JsonPropertyName("text")]
public string? Text { get; set; }
///
/// Gets the message text with the bot (recipient) @mention removed and trimmed.
/// In group chats, Teams prepends "<at>botname</at>" to the text when the bot is mentioned.
/// This property strips that mention so handlers can match on the user's intent alone.
///
[JsonIgnore]
public string? TextWithoutMentions
{
get
{
string? text = Text;
if (text is null) return null;
foreach (MentionEntity mention in this.GetMentions())
{
if (mention.Mentioned?.Id == Recipient?.Id && mention.Text is not null)
{
text = text.Replace(mention.Text, string.Empty, StringComparison.OrdinalIgnoreCase);
}
}
return text.Trim();
}
}
///
/// Gets or sets the text format. See for common values (plain, markdown, xml, extendedmarkdown).
///
[JsonPropertyName("textFormat")]
public string? TextFormat { get; set; }
///
/// Gets or sets the attachment layout.
///
[JsonPropertyName("attachmentLayout")]
public string? AttachmentLayout { get; set; }
}
///
/// String constants for text formats.
///
public static class TextFormats
{
///
/// Plain text format.
///
public const string Plain = "plain";
///
/// Markdown text format.
///
public const string Markdown = "markdown";
///
/// XML text format.
///
public const string Xml = "xml";
///
/// Extended markdown text format. Supports GFM tables, LaTeX math blocks,
/// and other rich content beyond standard markdown.
///
public const string ExtendedMarkdown = "extendedmarkdown";
}