microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feat/a365-mcp

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

193lines · 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 if (activity.Properties.TryGetValue("summary", out var summary))
68 {
69 Summary = summary?.ToString();
70 activity.Properties.Remove("summary");
71 }
72 if (activity.Properties.TryGetValue("deliveryMode", out var deliveryMode))
73 {
74 DeliveryMode = deliveryMode?.ToString();
75 activity.Properties.Remove("deliveryMode");
76 }
77 */
78 }
79
80 /// <summary>
81 /// Gets or sets the attachments for the message.
82 /// </summary>
83 [JsonPropertyName("attachments")]
84 public IList<TeamsAttachment>? Attachments { get; set; }
85
86 /// <summary>
87 /// Gets or sets the text content of the message.
88 /// </summary>
89 [JsonPropertyName("text")]
90 public string? Text { get; set; }
91
92 /// <summary>
93 /// Gets the message text with the bot (recipient) @mention removed and trimmed.
94 /// In group chats, Teams prepends "&lt;at&gt;botname&lt;/at&gt;" to the text when the bot is mentioned.
95 /// This property strips that mention so handlers can match on the user's intent alone.
96 /// </summary>
97 [JsonIgnore]
98 public string? TextWithoutMentions
99 {
100 get
101 {
102 string? text = Text;
103 if (text is null) return null;
104
105 foreach (MentionEntity mention in this.GetMentions())
106 {
107 if (mention.Mentioned?.Id == Recipient?.Id && mention.Text is not null)
108 {
109 text = text.Replace(mention.Text, string.Empty, StringComparison.OrdinalIgnoreCase);
110 }
111 }
112 return text.Trim();
113 }
114 }
115 /// <summary>
116 /// Gets or sets the text format. See <see cref="TextFormats"/> for common values.
117 /// </summary>
118 [JsonPropertyName("textFormat")]
119 public string? TextFormat { get; set; }
120
121 /// <summary>
122 /// Gets or sets the attachment layout.
123 /// </summary>
124 [JsonPropertyName("attachmentLayout")]
125 public string? AttachmentLayout { get; set; }
126
127
128
129 //TODO : Review properties
130 /*
131 /// <summary>
132 /// Gets or sets the summary of the message.
133 /// </summary>
134 [JsonPropertyName("summary")]
135 public string? Summary { get; set; }
136
137 /// <summary>
138 /// Gets or sets the delivery mode. See <see cref="DeliveryModes"/> for common values.
139 /// </summary>
140 [JsonPropertyName("deliveryMode")]
141 public string? DeliveryMode { get; set; }
142 */
143
144}
145
146/// <summary>
147/// String constants for text formats.
148/// </summary>
149public static class TextFormats
150{
151 /// <summary>
152 /// Plain text format.
153 /// </summary>
154 public const string Plain = "plain";
155
156 /// <summary>
157 /// Markdown text format.
158 /// </summary>
159 public const string Markdown = "markdown";
160
161 /// <summary>
162 /// XML text format.
163 /// </summary>
164 public const string Xml = "xml";
165}
166
167/*
168/// <summary>
169/// String constants for delivery modes.
170/// </summary>
171public static class DeliveryModes
172{
173 /// <summary>
174 /// Normal delivery mode.
175 /// </summary>
176 public const string Normal = "normal";
177
178 /// <summary>
179 /// Notification delivery mode.
180 /// </summary>
181 public const string Notification = "notification";
182
183 /// <summary>
184 /// Ephemeral delivery mode.
185 /// </summary>
186 public const string Ephemeral = "ephemeral";
187
188 /// <summary>
189 /// Expected replies delivery mode.
190 /// </summary>
191 public const string ExpectedReplies = "expectReplies";
192}
193*/