@@ -29,23 +29,37 @@ public MatterhookClient(string webhookUrl, int timeoutSeconds = 100)
2929
3030 public MattermostMessage CloneMessage ( MattermostMessage message )
3131 {
32- return new MattermostMessage
32+ var retval = new MattermostMessage
3333 {
3434 Text = "" ,
3535 Channel = message . Channel ,
3636 Username = message . Username ,
3737 IconUrl = message . IconUrl
3838 } ;
39+
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 ;
48+
3949 }
4050
51+ /// <summary>
52+ /// Post Message to Mattermost server. Messages will be automatically split if total text length > 4000
53+ /// </summary>
54+ /// <param name="message">The messsage you wish to send</param>
55+ /// <returns></returns>
4156 public async Task < HttpResponseMessage > PostAsync ( MattermostMessage message )
4257 {
4358 try
4459 {
4560 HttpResponseMessage response = null ;
4661 var messages = new List < MattermostMessage > ( ) ;
4762
48-
4963 var cnt = 0 ;
5064
5165 var lines = new string [ ] { } ;
@@ -54,8 +68,10 @@ public async Task<HttpResponseMessage> PostAsync(MattermostMessage message)
5468 lines = message . Text . Split ( new [ ] { "\r \n " , "\r " , "\n " } , StringSplitOptions . None ) ;
5569 }
5670
71+ //start with one cloned message in the list
5772 messages . Add ( CloneMessage ( message ) ) ;
5873
74+ //add text from original. If we go over 3800, we'll split it to a new message.
5975 foreach ( var line in lines )
6076 {
6177 if ( line . Length + messages [ cnt ] . Text . Length > 3800 )
@@ -67,26 +83,32 @@ public async Task<HttpResponseMessage> PostAsync(MattermostMessage message)
6783 messages [ cnt ] . Text += $ "{ line } \r \n ";
6884 }
6985
86+ //Length of text on the last (or first if only one) message.
7087 var len = messages [ cnt ] . Text . Length ;
7188
89+ //does our original have attachments?
7290 if ( message . Attachments ? . Any ( ) ?? false )
7391 {
74- messages [ cnt ] . Attachments = new List < MattermostAttachment > { new MattermostAttachment ( ) } ;
75- messages [ cnt ] . Attachments [ 0 ] . Text = "" ;
76-
77- lines = message . Attachments [ 0 ] . Text . Split ( new [ ] { "\r \n " , "\r " , "\n " } , StringSplitOptions . None ) ;
7892
79- foreach ( var line in lines )
93+ //loop through them in a similar fashion to the message text above.
94+ foreach ( var att in message . Attachments )
8095 {
81- if ( len + messages [ cnt ] . Attachments [ 0 ] . Text . Length + line . Length > 3800 )
96+ messages [ cnt ] . Attachments = new List < MattermostAttachment > { new MattermostAttachment ( ) } ;
97+ messages [ cnt ] . Attachments [ 0 ] . Text = "" ;
98+
99+ lines = message . Attachments [ 0 ] . Text . Split ( new [ ] { "\r \n " , "\r " , "\n " } , StringSplitOptions . None ) ;
100+
101+ foreach ( var line in lines )
82102 {
83- cnt += 1 ;
84- messages . Add ( CloneMessage ( message ) ) ;
85- messages [ cnt ] . Attachments = new List < MattermostAttachment > { new MattermostAttachment ( ) } ;
86- messages [ cnt ] . Attachments [ 0 ] . Text = "" ;
103+ if ( len + messages [ cnt ] . Attachments [ 0 ] . Text . Length + line . Length > 3800 )
104+ {
105+ cnt += 1 ;
106+ messages . Add ( CloneMessage ( message ) ) ;
107+ messages [ cnt ] . Attachments [ 0 ] . Text = "" ;
108+ }
109+
110+ messages [ cnt ] . Attachments [ 0 ] . Text += $ "{ line } \r \n ";
87111 }
88-
89- messages [ cnt ] . Attachments [ 0 ] . Text += $ "{ line } \r \n ";
90112 }
91113 }
92114
@@ -110,6 +132,7 @@ public async Task<HttpResponseMessage> PostAsync(MattermostMessage message)
110132
111133 return response ;
112134 }
135+
113136 catch ( Exception e )
114137 {
115138 Console . WriteLine ( e . Message ) ;
0 commit comments