Skip to content

Commit ce3fe42

Browse files
author
Adam Warner
committed
Found the lost attachment titles/usernames/icons etc.
Now works with multi-attachment messages, too!
1 parent d003218 commit ce3fe42

File tree

1 file changed

+67
-45
lines changed

1 file changed

+67
-45
lines changed

Matterhook.NET.MatterhookClient/MatterhookClient.cs

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,103 +27,126 @@ public MatterhookClient(string webhookUrl, int timeoutSeconds = 100)
2727
_httpClient.Timeout = new TimeSpan(0, 0, 0, timeoutSeconds);
2828
}
2929

30-
public MattermostMessage CloneMessage(MattermostMessage message)
30+
public MattermostMessage CloneMessage(MattermostMessage inMsg)
3131
{
32-
var retval = new MattermostMessage
32+
var outMsg = new MattermostMessage
3333
{
3434
Text = "",
35-
Channel = message.Channel,
36-
Username = message.Username,
37-
IconUrl = message.IconUrl
35+
Channel = inMsg.Channel,
36+
Username = inMsg.Username,
37+
IconUrl = inMsg.IconUrl
3838
};
3939

40-
//if no attachments on the original, return clone without attachments.
41-
if (retval.Attachments == null) return retval;
42-
43-
//we have attachment(s) on the original, we need at least one attachment on the clone
44-
retval.Attachments[0] = message.Attachments[0];
45-
retval.Attachments[0].Text = "";
46-
47-
return retval;
40+
return outMsg;
41+
}
4842

43+
private static MattermostAttachment CloneAttachment(MattermostAttachment inAtt)
44+
{
45+
var outAtt = new MattermostAttachment
46+
{
47+
AuthorIcon = inAtt.AuthorIcon,
48+
AuthorLink = inAtt.AuthorLink,
49+
AuthorName = inAtt.AuthorName,
50+
Color = inAtt.Color,
51+
Fallback = inAtt.Fallback,
52+
Fields = inAtt.Fields,
53+
ImageUrl = inAtt.ImageUrl,
54+
Pretext = inAtt.Pretext,
55+
ThumbUrl = inAtt.ThumbUrl,
56+
Title = inAtt.Title,
57+
TitleLink = inAtt.TitleLink,
58+
Text = ""
59+
};
60+
return outAtt;
4961
}
5062

5163
/// <summary>
5264
/// Post Message to Mattermost server. Messages will be automatically split if total text length > 4000
5365
/// </summary>
54-
/// <param name="message">The messsage you wish to send</param>
66+
/// <param name="inMessage">The messsage you wish to send</param>
5567
/// <returns></returns>
56-
public async Task<HttpResponseMessage> PostAsync(MattermostMessage message)
68+
public async Task<HttpResponseMessage> PostAsync(MattermostMessage inMessage)
5769
{
70+
5871
try
5972
{
6073
HttpResponseMessage response = null;
61-
var messages = new List<MattermostMessage>();
74+
var outMessages = new List<MattermostMessage>();
6275

63-
var cnt = 0;
76+
var msgCount = 0;
6477

6578
var lines = new string[] { };
66-
if (message.Text != null)
79+
if (inMessage.Text != null)
6780
{
68-
lines = message.Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
81+
lines = inMessage.Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
6982
}
7083

71-
//start with one cloned message in the list
72-
messages.Add(CloneMessage(message));
84+
//start with one cloned inMessage in the list
85+
outMessages.Add(CloneMessage(inMessage));
7386

74-
//add text from original. If we go over 3800, we'll split it to a new message.
87+
//add text from original. If we go over 3800, we'll split it to a new inMessage.
7588
foreach (var line in lines)
7689
{
77-
if (line.Length + messages[cnt].Text.Length > 3800)
90+
91+
if (line.Length + outMessages[msgCount].Text.Length > 3800)
7892
{
79-
cnt += 1;
80-
messages.Add(CloneMessage(message));
93+
94+
msgCount += 1;
95+
outMessages.Add(CloneMessage(inMessage));
8196
}
8297

83-
messages[cnt].Text += $"{line}\r\n";
98+
outMessages[msgCount].Text += $"{line}\r\n";
8499
}
85100

86-
//Length of text on the last (or first if only one) message.
87-
var len = messages[cnt].Text.Length;
101+
//Length of text on the last (or first if only one) inMessage.
102+
var lenMessageText = outMessages[msgCount].Text.Length;
88103

89104
//does our original have attachments?
90-
if (message.Attachments?.Any() ?? false)
105+
if (inMessage.Attachments?.Any() ?? false)
91106
{
107+
outMessages[msgCount].Attachments = new List<MattermostAttachment>();
92108

93-
//loop through them in a similar fashion to the message text above.
94-
foreach (var att in message.Attachments)
109+
//loop through them in a similar fashion to the inMessage text above.
110+
foreach (var att in inMessage.Attachments)
95111
{
96-
messages[cnt].Attachments = new List<MattermostAttachment> { new MattermostAttachment() };
97-
messages[cnt].Attachments[0].Text = "";
112+
//add this attachment to the outgoing message
113+
outMessages[msgCount].Attachments.Add(CloneAttachment(att));
114+
//get a count of attachments on this message, and subtract one so we know the index of the current new attachment
115+
var attIndex = outMessages[msgCount].Attachments.Count - 1;
98116

99-
lines = message.Attachments[0].Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
117+
//Get the text lines
118+
lines = att.Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
100119

101120
foreach (var line in lines)
102121
{
103-
if (len + messages[cnt].Attachments[0].Text.Length + line.Length > 3800)
122+
//Get the total length of all attachments on the current outgoing message
123+
var lenAllAttsText = outMessages[msgCount].Attachments.Sum(a => a.Text.Length);
124+
125+
if (lenMessageText + lenAllAttsText + line.Length > 3800)
104126
{
105-
cnt += 1;
106-
messages.Add(CloneMessage(message));
107-
messages[cnt].Attachments[0].Text = "";
127+
msgCount += 1;
128+
attIndex = 0;
129+
outMessages.Add(CloneMessage(inMessage));
130+
outMessages[msgCount].Attachments = new List<MattermostAttachment> { CloneAttachment(att) };
108131
}
109132

110-
messages[cnt].Attachments[0].Text += $"{line}\r\n";
133+
outMessages[msgCount].Attachments[attIndex].Text += $"{line}\r\n";
111134
}
112135
}
113136
}
114137

115138

116-
if (messages.Count > 1)
139+
if (outMessages.Count > 1)
117140
{
118141
var num = 1;
119-
foreach (var msg in messages)
142+
foreach (var msg in outMessages)
120143
{
121-
msg.Text = $"`({num}/{cnt + 1}): ` " + msg.Text;
144+
msg.Text = $"`({num}/{msgCount + 1}): ` " + msg.Text;
122145
num++;
123146
}
124147
}
125148

126-
foreach (var msg in messages)
149+
foreach (var msg in outMessages)
127150
{
128151
var serializedPayload = JsonConvert.SerializeObject(msg);
129152
response = await _httpClient.PostAsync(_webhookUrl,
@@ -140,5 +163,4 @@ public async Task<HttpResponseMessage> PostAsync(MattermostMessage message)
140163
}
141164
}
142165
}
143-
}
144-
166+
}

0 commit comments

Comments
 (0)