microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.8

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Apps/Schema/MessageActivity.cs

135lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Text.Json.Serialization;
5using Microsoft.Teams.Apps.Schema.Entities;
6using Microsoft.Teams.Core.Schema;
7
8namespace Microsoft.Teams.Apps.Schema;
9
10/// <summary>
11/// Represents a message activity.
12/// </summary>
13public class MessageActivity : TeamsActivity
14{
15
16 /// <summary>
17 /// Convenience method to create a MessageActivity from a CoreActivity.
18 /// </summary>
19 /// <param name="activity">The CoreActivity to convert.</param>
20 /// <returns>A MessageActivity instance.</returns>
21 public static new MessageActivity FromActivity(CoreActivity activity)
22 {
23 ArgumentNullException.ThrowIfNull(activity);
24 return new MessageActivity(activity);
25 }
26
27 /// <summary>
28 /// Default constructor.
29 /// </summary>
30 [JsonConstructor]
31 public MessageActivity() : base(TeamsActivityType.Message)
32 {
33 }
34
35 /// <summary>
36 /// Initializes a new instance of the <see cref="MessageActivity"/> class with the specified text.
37 /// </summary>
38 /// <param name="text">The text content of the message.</param>
39 public MessageActivity(string text) : base(TeamsActivityType.Message)
40 {
41 Text = text;
42 }
43
44
45 /// <summary>
46 /// Initializes a new instance of the <see cref="MessageActivity"/> class with the specified text.
47 /// </summary>
48 /// <param name="attachments">The list of attachments for the message.</param>
49 public MessageActivity(IList<TeamsAttachment> attachments) : base(TeamsActivityType.Message)
50 {
51 Attachments = attachments;
52 }
53
54 /// <summary>
55 /// Internal constructor to create MessageActivity from CoreActivity.
56 /// </summary>
57 /// <param name="activity">The CoreActivity to convert.</param>
58 protected MessageActivity(CoreActivity activity) : base(activity)
59 {
60 Attachments = activity.Properties.Extract<IList<TeamsAttachment>>("attachments");
61 Text = activity.Properties.Extract<string>("text");
62 TextFormat = activity.Properties.Extract<string>("textFormat");
63 AttachmentLayout = activity.Properties.Extract<string>("attachmentLayout");
64 SuggestedActions = activity.Properties.Extract<SuggestedActions>("suggestedActions");
65 }
66
67 /// <summary>
68 /// Gets or sets the attachments for the message.
69 /// </summary>
70 [JsonPropertyName("attachments")]
71 public IList<TeamsAttachment>? Attachments { get; set; }
72
73 /// <summary>
74 /// Gets or sets the text content of the message.
75 /// </summary>
76 [JsonPropertyName("text")]
77 public string? Text { get; set; }
78
79 /// <summary>
80 /// Gets the message text with the bot (recipient) @mention removed and trimmed.
81 /// In group chats, Teams prepends "&lt;at&gt;botname&lt;/at&gt;" to the text when the bot is mentioned.
82 /// This property strips that mention so handlers can match on the user's intent alone.
83 /// </summary>
84 [JsonIgnore]
85 public string? TextWithoutMentions
86 {
87 get
88 {
89 string? text = Text;
90 if (text is null) return null;
91
92 foreach (MentionEntity mention in this.GetMentions())
93 {
94 if (mention.Mentioned?.Id == Recipient?.Id && mention.Text is not null)
95 {
96 text = text.Replace(mention.Text, string.Empty, StringComparison.OrdinalIgnoreCase);
97 }
98 }
99 return text.Trim();
100 }
101 }
102 /// <summary>
103 /// Gets or sets the text format. See <see cref="TextFormats"/> for common values.
104 /// </summary>
105 [JsonPropertyName("textFormat")]
106 public string? TextFormat { get; set; }
107
108 /// <summary>
109 /// Gets or sets the attachment layout.
110 /// </summary>
111 [JsonPropertyName("attachmentLayout")]
112 public string? AttachmentLayout { get; set; }
113
114}
115
116/// <summary>
117/// String constants for text formats.
118/// </summary>
119public static class TextFormats
120{
121 /// <summary>
122 /// Plain text format.
123 /// </summary>
124 public const string Plain = "plain";
125
126 /// <summary>
127 /// Markdown text format.
128 /// </summary>
129 public const string Markdown = "markdown";
130
131 /// <summary>
132 /// XML text format.
133 /// </summary>
134 public const string Xml = "xml";
135}
136