Skip to content

Commit b7d4a97

Browse files
authored
Merge pull request #2 from PromoFaux/fix/missingAttTitles
Fix/missing att titles
2 parents 6601ea0 + b926260 commit b7d4a97

File tree

2 files changed

+83
-38
lines changed

2 files changed

+83
-38
lines changed

Matterhook.NET.MatterhookClient/MatterhookClient.cs

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,81 +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-
return 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
};
39+
40+
return outMsg;
3941
}
4042

41-
public async Task<HttpResponseMessage> PostAsync(MattermostMessage message)
43+
private static MattermostAttachment CloneAttachment(MattermostAttachment inAtt)
4244
{
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;
61+
}
62+
63+
/// <summary>
64+
/// Post Message to Mattermost server. Messages will be automatically split if total text length > 4000
65+
/// </summary>
66+
/// <param name="inMessage">The messsage you wish to send</param>
67+
/// <returns></returns>
68+
public async Task<HttpResponseMessage> PostAsync(MattermostMessage inMessage)
69+
{
70+
4371
try
4472
{
4573
HttpResponseMessage response = null;
46-
var messages = new List<MattermostMessage>();
74+
var outMessages = new List<MattermostMessage>();
4775

48-
49-
var cnt = 0;
76+
var msgCount = 0;
5077

5178
var lines = new string[] { };
52-
if (message.Text != null)
79+
if (inMessage.Text != null)
5380
{
54-
lines = message.Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
81+
lines = inMessage.Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
5582
}
5683

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

87+
//add text from original. If we go over 3800, we'll split it to a new inMessage.
5988
foreach (var line in lines)
6089
{
61-
if (line.Length + messages[cnt].Text.Length > 3800)
90+
91+
if (line.Length + outMessages[msgCount].Text.Length > 3800)
6292
{
63-
cnt += 1;
64-
messages.Add(CloneMessage(message));
93+
94+
msgCount += 1;
95+
outMessages.Add(CloneMessage(inMessage));
6596
}
6697

67-
messages[cnt].Text += $"{line}\r\n";
98+
outMessages[msgCount].Text += $"{line}\r\n";
6899
}
69100

70-
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;
71103

72-
if (message.Attachments?.Any() ?? false)
104+
//does our original have attachments?
105+
if (inMessage.Attachments?.Any() ?? false)
73106
{
74-
messages[cnt].Attachments = new List<MattermostAttachment> { new MattermostAttachment() };
75-
messages[cnt].Attachments[0].Text = "";
107+
outMessages[msgCount].Attachments = new List<MattermostAttachment>();
76108

77-
lines = message.Attachments[0].Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
78-
79-
foreach (var line in lines)
109+
//loop through them in a similar fashion to the inMessage text above.
110+
foreach (var att in inMessage.Attachments)
80111
{
81-
if (len + messages[cnt].Attachments[0].Text.Length + line.Length > 3800)
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;
116+
117+
//Get the text lines
118+
lines = att.Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
119+
120+
foreach (var line in lines)
82121
{
83-
cnt += 1;
84-
messages.Add(CloneMessage(message));
85-
messages[cnt].Attachments = new List<MattermostAttachment> { new MattermostAttachment() };
86-
messages[cnt].Attachments[0].Text = "";
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)
126+
{
127+
msgCount += 1;
128+
attIndex = 0;
129+
outMessages.Add(CloneMessage(inMessage));
130+
outMessages[msgCount].Attachments = new List<MattermostAttachment> { CloneAttachment(att) };
131+
}
132+
133+
outMessages[msgCount].Attachments[attIndex].Text += $"{line}\r\n";
87134
}
88-
89-
messages[cnt].Attachments[0].Text += $"{line}\r\n";
90135
}
91136
}
92137

93138

94-
if (messages.Count > 1)
139+
if (outMessages.Count > 1)
95140
{
96141
var num = 1;
97-
foreach (var msg in messages)
142+
foreach (var msg in outMessages)
98143
{
99-
msg.Text = $"`({num}/{cnt + 1}): ` " + msg.Text;
144+
msg.Text = $"`({num}/{msgCount + 1}): ` " + msg.Text;
100145
num++;
101146
}
102147
}
103148

104-
foreach (var msg in messages)
149+
foreach (var msg in outMessages)
105150
{
106151
var serializedPayload = JsonConvert.SerializeObject(msg);
107152
response = await _httpClient.PostAsync(_webhookUrl,
@@ -110,12 +155,12 @@ public async Task<HttpResponseMessage> PostAsync(MattermostMessage message)
110155

111156
return response;
112157
}
158+
113159
catch (Exception e)
114160
{
115161
Console.WriteLine(e.Message);
116162
throw;
117163
}
118164
}
119165
}
120-
}
121-
166+
}

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 1.2.2.{build}
1+
version: 1.2.3.{build}
22
pull_requests:
33
do_not_increment_build_number: true
44
image: Visual Studio 2017
@@ -7,7 +7,7 @@ dotnet_csproj:
77
patch: true
88
file: '**\*.csproj'
99
version: '{version}'
10-
package_version: '{version}'
10+
package_version: '1.2.3'
1111
nuget:
1212
disable_publish_on_pr: true
1313
before_build:

0 commit comments

Comments
 (0)