Пример #1
0
        public void ShouldPostToMultipleChannels()
        {
            //arrange
            const string hookUrl            = "https://hooks.slack.com/invalid";
            var          channelsPostedTo   = new List <string>();
            var          httpMessageHandler = GetMockHttpMessageHandler(callback: (req, token) =>
            {
                var json          = req.Content.ReadAsStringAsync().GetAwaiter().GetResult();
                var postedMessage = SlackClient.DeserializeObject(json);
                channelsPostedTo.Add(postedMessage.Channel);
            });

            var httpClient       = new HttpClient(httpMessageHandler.Object, false);
            var client           = new SlackClient(hookUrl, httpClient: httpClient);
            var slackMessage     = GetSlackMessage();
            var channelsToPostTo = new List <string> {
                "#test1", "#test2", "test3"
            };

            //act
            client.PostToChannels(slackMessage, channelsToPostTo);

            //assert
            Assert.Equal(channelsToPostTo.Count, channelsPostedTo.Count);
            foreach (var c in channelsToPostTo)
            {
                Assert.Contains(c, channelsPostedTo);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Please enter your message to send by slack");
                string message = Console.ReadLine();
                Console.WriteLine("Please mention which channel you want to add this message");
                var channelss = Console.ReadLine();

                //Regex reg = new Regex(@"/([#&][^\x07\x2C\s]{,200})/");
                //Match match;
                //var results = new List<string>();
                //for (match=reg.Match(channelss); match.Success; match=match.NextMatch())
                //{
                //    if (!results.Contains(match.Value))
                //    {
                //        results.Add(match.Value);
                //    }
                //}

                Console.WriteLine($"Your message '{message}' is sent!");
                SlackClient client   = new SlackClient("https://hooks.slack.com/services/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");
                var         channels = new List <string>();
                if (channelss.Count() != 0 && channelss.Any())
                {
                    foreach (var channel in channelss.Split(','))
                    {
                        channels.Add("#" + channel);
                    }
                }
                var slackMessage = new SlackMessage()
                {
                    Username  = "******",
                    IconEmoji = ":umbrella:",
                    Channel   = "#general",
                    Text      = message
                };
                var a = new SlackAttachment();
                a.AuthorLink = @"C:\temp\data.txt";
                // slackMessage.Attachments.Add(a);
                // client.PostToChannels(slackMessage, channels);
                client.PostToChannels(slackMessage, channels);
            }


            //Console.ReadLine();
        }