Esempio n. 1
0
        private MattermostMessage CloneMessage(MattermostMessage inMsg)
        {
            var outMsg = new MattermostMessage
            {
                Text     = "",
                Channel  = inMsg.Channel,
                Username = inMsg.Username,
                IconUrl  = inMsg.IconUrl
            };

            return(outMsg);
        }
Esempio n. 2
0
        /// <summary>
        /// Post Message to Mattermost server. Messages will be automatically split. (Mattermost actually already auto splits long messages, but this will preserve whole words, rather than just splitting on message length alone.
        /// </summary>
        /// <param name="inMessage">The messsage you wish to send</param>
        /// <param name="maxMessageLength">(Optional) Defaulted to 4000, but can be set to any value (Check with your Mattermost server admin!)</param>
        /// <returns></returns>
        public async Task <HttpResponseMessage> PostAsync(MattermostMessage inMessage, int maxMessageLength = 4000)
        {
            try
            {
                HttpResponseMessage response = null;

                maxMessageLength -= 10; //To allow for adding a message number indicator at the front end of the message.

                var outMessages = new List <MattermostMessage>
                {
                    CloneMessage(inMessage)
                };

                var msgIdx = 0;

                if (inMessage.Text != null)
                {
                    //Split messages text into chunks of maxMessageLength in size.
                    var textChunks = SplitTextIntoChunks(inMessage.Text, maxMessageLength);

                    //iterate through chunks and create a MattermostMessage object for each one and add it to outMessages list.
                    foreach (var chunk in textChunks)
                    {
                        outMessages[msgIdx].Text = chunk;
                        if (msgIdx < textChunks.Count() - 1)
                        {
                            outMessages.Add(CloneMessage(inMessage));
                            msgIdx++;
                        }
                    }
                }

                //next check for attachments on the original message object
                if (inMessage.Attachments?.Any() ?? false)
                {
                    outMessages[msgIdx].Attachments = new List <MattermostAttachment>();
                    var msgCnt = msgIdx;

                    foreach (var att in inMessage.Attachments)
                    {
                        outMessages[msgIdx].Attachments.Add(CloneAttachment(att));
                        var attIdx = outMessages[msgIdx].Attachments.Count - 1;

                        var attTextChunks = SplitTextIntoChunks(att.Text, 6600);     //arbitrary limit. MM files suggest limit is 7600, but that still results in attachments being truncated...

                        foreach (var attChunk in attTextChunks)
                        {
                            outMessages[msgIdx].Attachments[attIdx].Text = attChunk;

                            if (msgIdx < msgCnt + attTextChunks.Count() - 1)
                            {
                                outMessages.Add(CloneMessage(inMessage));
                                msgIdx++;
                                outMessages[msgIdx].Attachments = new List <MattermostAttachment> {
                                    CloneAttachment(att)
                                };
                            }
                        }
                    }
                }

                if (outMessages.Count > 1)
                {
                    var num = 1;
                    foreach (var msg in outMessages)
                    {
                        msg.Text = $"`({num}/{msgIdx + 1}): ` " + msg.Text;
                        num++;
                    }
                }

                foreach (var msg in outMessages)
                {
                    var serializedPayload = JsonConvert.SerializeObject(msg);
                    response = await _httpClient.PostAsync(_webhookUrl,
                                                           new StringContent(serializedPayload, Encoding.UTF8, "application/json"));
                }

                return(response);
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }