コード例 #1
0
        public SlackChatMessageResult PostMessage(
            string channel,
            string text,
            ICollection <SlackChatMessageAttachment> messageAttachments,
            SlackChatMessageSettings messageSettings)
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException(nameof(messageSettings));
            }

            if (messageAttachments == null)
            {
                throw new ArgumentNullException(nameof(messageAttachments));
            }

            var response = _context.PostMessage(
                channel,
                text,
                messageAttachments,
                messageSettings
                );

            response.Wait();

            return(response.Result);
        }
コード例 #2
0
ファイル: SlackChatApi.cs プロジェクト: kolesnick/Cake.Slack
        private static Dictionary <string, string> GetMessageParams(
            string token,
            string channel,
            string text,
            ICollection <SlackChatMessageAttachment> messageAttachments,
            SlackChatMessageSettings messageSettings)
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException(nameof(messageSettings), "Invalid slack message settings specified");
            }

            if (messageAttachments == null)
            {
                throw new ArgumentNullException(nameof(messageAttachments),
                                                "Invalid slack message attachments specified");
            }

            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentNullException(nameof(token), "Invalid Message Token specified");
            }

            if (string.IsNullOrWhiteSpace(channel))
            {
                throw new ArgumentNullException(nameof(channel), "Invalid Message Channel specified");
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentNullException(nameof(text), "Invalid Message Text specified");
            }

            var messageParams = new Dictionary <string, string>
            {
                { "token", token },
                { "channel", channel },
                { "text", text },
                { "username", messageSettings.UserName ?? "CakeBuild" },
                {
                    "icon_url",
                    messageSettings.IconUrl?.AbsoluteUri ??
                    "https://raw.githubusercontent.com/cake-build/graphics/master/png/cake-small.png"
                },
                { "link_names", messageSettings.LinkNames.ToString().ToLower() },
            };

            if (messageAttachments.Count > 0)
            {
                messageParams.Add("attachments", ToJson(messageAttachments));
            }

            return(messageParams);
        }
コード例 #3
0
        internal static SlackChatMessageResult PostMessage(
            this ICakeContext context,
            string channel,
            string text,
            ICollection <SlackChatMessageAttachment> messageAttachments,
            SlackChatMessageSettings messageSettings
            )
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException(nameof(messageSettings), "Invalid slack message specified");
            }

            if (messageAttachments == null)
            {
                throw new ArgumentNullException(nameof(messageAttachments), "Invalid slack messsage attachment");
            }

            SlackChatMessageResult result;

            if (!string.IsNullOrWhiteSpace(messageSettings.IncomingWebHookUrl))
            {
                result = PostToIncomingWebHook(
                    context,
                    channel,
                    text,
                    messageAttachments,
                    messageSettings
                    );
            }
            else
            {
                var messageParams = GetMessageParams(
                    messageSettings.Token,
                    channel,
                    text,
                    messageAttachments,
                    messageSettings
                    );

                result = context.PostToChatApi(
                    PostMessageUri,
                    messageParams
                    );
            }
            return(result);
        }
コード例 #4
0
        internal static SlackChatMessageResult PostMessage(
            this ICakeContext context,
            string channel,
            string text,
            SlackChatMessageSettings messageSettings
            )
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException(nameof(messageSettings), "Invalid slack message specified");
            }

            SlackChatMessageResult result;

            if (!string.IsNullOrWhiteSpace(messageSettings.IncomingWebHookUrl))
            {
                result = PostToIncomingWebHook(
                    context,
                    channel,
                    text,
                    EmptySlackChatMessageAttachments,
                    messageSettings
                    );
            }
            else
            {
                var messageParams = GetMessageParams(
                    messageSettings.Token,
                    channel,
                    text,
                    EmptySlackChatMessageAttachments,
                    messageSettings
                    );

                result = context.PostToChatApi(
                    PostMessageUri,
                    messageParams
                    );
            }

            if (!result.Ok && messageSettings.ThrowOnFail == true)
            {
                throw new CakeException(result.Error ?? "Failed to send message, unknown error");
            }

            return(result);
        }
コード例 #5
0
ファイル: SlackChatApi.cs プロジェクト: RLittlesII/Cake.Slack
        internal static SlackChatMessageResult PostMessage(
            this ICakeContext context,
            string channel,
            string text,
            ICollection<SlackChatMessageAttachment> messageAttachments,
            SlackChatMessageSettings messageSettings
            )
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException(nameof(messageSettings), "Invalid slack message specified");
            }

            if(messageAttachments == null)
            {
                throw new ArgumentNullException(nameof(messageAttachments), "Invalid slack messsage attachment");
            }

            SlackChatMessageResult result;
            if (!string.IsNullOrWhiteSpace(messageSettings.IncomingWebHookUrl))
            {
                result = PostToIncomingWebHook(
                    context,
                    channel,
                    text,
                    messageAttachments,
                    messageSettings
                    );
            }
            else
            {
                var messageParams = GetMessageParams(
                    messageSettings.Token,
                    channel,
                    text,
                    messageAttachments,
                    messageSettings
                    );

                result = context.PostToChatApi(
                    PostMessageUri,
                    messageParams
                    );

            }
            return result;
        }
コード例 #6
0
ファイル: SlackChatApi.cs プロジェクト: RLittlesII/Cake.Slack
        internal static SlackChatMessageResult PostMessage(
            this ICakeContext context,
            string channel,
            string text,
            SlackChatMessageSettings messageSettings
            )
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException(nameof(messageSettings), "Invalid slack message specified");
            }

            SlackChatMessageResult result;
            
            if (!string.IsNullOrWhiteSpace(messageSettings.IncomingWebHookUrl))
            {
                result = PostToIncomingWebHook(
                    context,
                    channel,
                    text,
                    EmptySlackChatMessageAttachments,
                    messageSettings
                    );
            }
            else
            {
                var messageParams = GetMessageParams(
                    messageSettings.Token,
                    channel,
                    text,
                    EmptySlackChatMessageAttachments,
                    messageSettings
                    );

                result = context.PostToChatApi(
                    PostMessageUri,
                    messageParams
                    );
            }

            if (!result.Ok && messageSettings.ThrowOnFail == true)
            {
                throw new CakeException(result.Error ?? "Failed to send message, unknown error");
            }

            return result;
        }
コード例 #7
0
        public SlackChatMessageResult PostMessage(
            string channel,
            string text,
            SlackChatMessageSettings messageSettings
            )
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException(nameof(messageSettings));
            }

            return _context.PostMessage(
                channel,
                text,
                messageSettings
                );
        }
コード例 #8
0
        public SlackChatMessageResult PostMessage(
            string channel,
            string text,
            SlackChatMessageSettings messageSettings
            )
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException(nameof(messageSettings));
            }

            return(_context.PostMessage(
                       channel,
                       text,
                       messageSettings
                       ));
        }
コード例 #9
0
ファイル: SlackChatApi.cs プロジェクト: devlead/Cake.Slack
        private static SlackChatMessageResult PostToIncomingWebHook(
            ICakeContext context,
            string channel,
            string text,
            SlackChatMessageSettings messageSettings
            )
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException("messageSettings", "Invalid slack message specified");
            }

            if (string.IsNullOrWhiteSpace(messageSettings.IncomingWebHookUrl))
            {
                throw new NullReferenceException("Invalid IncomingWebHookUrl supplied.");
            }


            context.Verbose(
                "Posting to incoming webhook {0}...",
                string.Concat(
                    messageSettings
                        .IncomingWebHookUrl
                        .TrimEnd('/')
                        .Reverse()
                        .SkipWhile(c => c != '/')
                        .Reverse()
                    )
                );

            var postJson = JsonMapper.ToJson(
                new
                {
                    channel,
                    text,
                    username = messageSettings.UserName ?? "CakeBuild",
                    icon_url =
                        messageSettings.IconUrl != null
                            ? messageSettings.IconUrl.AbsoluteUri
                            : "https://raw.githubusercontent.com/cake-build/graphics/master/png/cake-small.png"
                }
                );

           context.Debug("Parameter: {0}", postJson);

            using (var client = new WebClient())
            {
                var postBytes = Encoding.UTF8.GetBytes(postJson);
                
                var resultBytes = client.UploadData(
                        messageSettings.IncomingWebHookUrl,
                        "POST",
                        postBytes
                        );

                var result = Encoding.UTF8.GetString(
                    resultBytes
                    );

                var parsedResult = new SlackChatMessageResult(
                    StringComparer.OrdinalIgnoreCase.Equals(result, "ok"),
                    channel,
                    string.Empty,
                    StringComparer.OrdinalIgnoreCase.Equals(result, "ok") ? string.Empty : result
                    );

                context.Debug("Result parsed: {0}", parsedResult);
                return parsedResult;
            }
        }
コード例 #10
0
ファイル: SlackChatApi.cs プロジェクト: devlead/Cake.Slack
        private static NameValueCollection GetMessageParams(
            string token,
            string channel,
            string text,
            SlackChatMessageSettings messageSettings
            )
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException("messageSettings", "Invalid slack message specified");
            }

            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentNullException("token","Invalid Message Token specified");
            }

            if (string.IsNullOrWhiteSpace(channel))
            {
                throw new ArgumentNullException("channel", "Invalid Message Channel specified");
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentNullException("text", "Invalid Message Text specified");
            }

            var messageParams = new NameValueCollection
            {
                
                {"token", token},
                {"channel", channel},
                {"text", text},
                {"username", messageSettings.UserName ?? "CakeBuild"},
                {
                    "icon_url",
                    messageSettings.IconUrl != null
                        ? messageSettings.IconUrl.AbsoluteUri
                        : "https://raw.githubusercontent.com/cake-build/graphics/master/png/cake-small.png"
                }
            };
            return messageParams;
        }
コード例 #11
0
        private static SlackChatMessageResult PostToIncomingWebHook(
            ICakeContext context,
            string channel,
            string text,
            ICollection <SlackChatMessageAttachment> messageAttachments,
            SlackChatMessageSettings messageSettings
            )
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException(nameof(messageSettings), "Invalid slack message specified");
            }

            if (string.IsNullOrWhiteSpace(messageSettings.IncomingWebHookUrl))
            {
                throw new NullReferenceException("Invalid IncomingWebHookUrl supplied.");
            }

            if (messageAttachments == null)
            {
                throw new ArgumentNullException(nameof(messageAttachments), "Invalid attachment supplied");
            }

            context.Verbose(
                "Posting to incoming webhook {0}...",
                string.Concat(
                    messageSettings
                    .IncomingWebHookUrl
                    .TrimEnd('/')
                    .Reverse()
                    .SkipWhile(c => c != '/')
                    .Reverse()
                    )
                );

            var postJson = ToJson(
                new
            {
                channel,
                text,
                username    = messageSettings.UserName ?? "CakeBuild",
                attachments = messageAttachments,
                icon_url    = messageSettings.IconUrl?.AbsoluteUri ?? "https://raw.githubusercontent.com/cake-build/graphics/master/png/cake-small.png"
            });

            context.Debug("Parameter: {0}", postJson);

            using (var client = new WebClient())
            {
                var postBytes = Encoding.UTF8.GetBytes(postJson);

                var resultBytes = client.UploadData(
                    messageSettings.IncomingWebHookUrl,
                    "POST",
                    postBytes
                    );

                var result = Encoding.UTF8.GetString(
                    resultBytes
                    );

                var parsedResult = new SlackChatMessageResult(
                    StringComparer.OrdinalIgnoreCase.Equals(result, "ok"),
                    channel,
                    string.Empty,
                    StringComparer.OrdinalIgnoreCase.Equals(result, "ok") ? string.Empty : result
                    );

                context.Debug("Result parsed: {0}", parsedResult);
                return(parsedResult);
            }
        }
コード例 #12
0
        private static async Task <SlackChatMessageResult> PostToIncomingWebHook(
            ICakeContext context,
            string channel,
            string text,
            ICollection <SlackChatMessageAttachment> messageAttachments,
            SlackChatMessageSettings messageSettings)
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException(nameof(messageSettings), "Invalid slack message specified");
            }

            if (string.IsNullOrWhiteSpace(messageSettings.IncomingWebHookUrl))
            {
                throw new NullReferenceException("Invalid IncomingWebHookUrl supplied.");
            }

            if (messageAttachments == null)
            {
                throw new ArgumentNullException(nameof(messageAttachments), "Invalid attachment supplied");
            }

            context.Verbose(
                "Posting to incoming webhook {0}...",
                string.Concat(
                    messageSettings
                    .IncomingWebHookUrl
                    .TrimEnd('/')
                    .Reverse()
                    .SkipWhile(c => c != '/')
                    .Reverse()
                    )
                );

            var json = ToJson(
                new
            {
                channel,
                text,
                username    = messageSettings.UserName ?? "CakeBuild",
                attachments = messageAttachments,
                icon_url    =
                    messageSettings.IconUrl?.AbsoluteUri ??
                    "https://raw.githubusercontent.com/cake-build/graphics/master/png/cake-small.png",
                link_names = messageSettings.LinkNames.ToString().ToLower(),
            });

            context.Debug("Parameter: {0}", json);

            using (var client = new HttpClient())
            {
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                var httpResponse = await client.PostAsync(messageSettings.IncomingWebHookUrl, content);

                var response = await httpResponse.Content.ReadAsStringAsync();

                context.Debug($"Status Code: {httpResponse.StatusCode}");
                context.Debug($"Response: {response}");

                var parsedResult = new SlackChatMessageResult(
                    StringComparer.OrdinalIgnoreCase.Equals(response, "ok"),
                    channel,
                    string.Empty,
                    StringComparer.OrdinalIgnoreCase.Equals(response, "ok") ? string.Empty : response
                    );

                context.Debug("Result parsed: {0}", parsedResult);
                return(parsedResult);
            }
        }
コード例 #13
0
ファイル: SlackChatApi.cs プロジェクト: RLittlesII/Cake.Slack
        private static NameValueCollection GetMessageParams(
            string token,
            string channel,
            string text,
            ICollection<SlackChatMessageAttachment> messageAttachments,
            SlackChatMessageSettings messageSettings
            )
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException(nameof(messageSettings), "Invalid slack message settings specified");
            }

            if (messageAttachments == null)
            {
                throw new ArgumentNullException(nameof(messageAttachments), "Invalid slack message attachments specified");
            }

            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentNullException(nameof(token), "Invalid Message Token specified");
            }

            if (string.IsNullOrWhiteSpace(channel))
            {
                throw new ArgumentNullException(nameof(channel), "Invalid Message Channel specified");
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentNullException(nameof(text), "Invalid Message Text specified");
            }

            var messageParams = new NameValueCollection
            {

                {"token", token},
                {"channel", channel},
                {"text", text},
                {"username", messageSettings.UserName ?? "CakeBuild"},
                {
                    "icon_url",
                    messageSettings.IconUrl?.AbsoluteUri ?? "https://raw.githubusercontent.com/cake-build/graphics/master/png/cake-small.png"
                }
            };

            if (messageAttachments.Count > 0)
            {
                messageParams.Add("attachments", ToJson(messageAttachments));
            }

            return messageParams;
        }