Good morning, this is gonna be a very short post, as it’s truly super easy to integrate the C# with the Slack API. We’ll make use of the latest .NET Core version listed as the dnxcore50 and build a console application that you can use to send the messages to the particular channel.





At first, let’s set up the required dependencies int the project.json file:

"dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0-rc2-3002702" }, "Newtonsoft.Json": "8.0.3", "System.Net.Http": "4.0.1-rc2-24027", "System.Runtime.Serialization.Primitives": "4.1.0-rc2-24027" } 1 2 3 4 5 6 7 8 9 "dependencies" : { "Microsoft.NETCore.App" : { "type" : "platform" , "version" : "1.0.0-rc2-3002702" } , "Newtonsoft.Json" : "8.0.3" , "System.Net.Http" : "4.0.1-rc2-24027" , "System.Runtime.Serialization.Primitives" : "4.1.0-rc2-24027" }

Ok, we’re almost finished (I’m not kidding), just create the following class:

public class SlackClient { private readonly Uri _webhookUrl; private readonly HttpClient _httpClient = new HttpClient(); public SlackClient(Uri webhookUrl) { _webhookUrl = webhookUrl; } public async Task<HttpResponseMessage> SendMessageAsync(string message, string channel = null, string username = null) { var payload = new { text = message, channel, username, }; var serializedPayload = JsonConvert.SerializeObject(payload); var response = await _httpClient.PostAsync(_webhookUrl, new StringContent(serializedPayload, Encoding.UTF8, "application/json")); return response; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class SlackClient { private readonly Uri _webhookUrl ; private readonly HttpClient _httpClient = new HttpClient ( ) ; public SlackClient ( Uri webhookUrl ) { _webhookUrl = webhookUrl ; } public async Task < HttpResponseMessage > SendMessageAsync ( string message , string channel = null , string username = null ) { var payload = new { text = message , channel , username , } ; var serializedPayload = JsonConvert . SerializeObject ( payload ) ; var response = await _httpClient . PostAsync ( _webhookUrl , new StringContent ( serializedPayload , Encoding . UTF8 , "application/json" ) ) ; return response ; } }

And instantiate it within the main Program.cs:

public class Program { public static void Main(string[] args) { Task.WaitAll(IntegrateWithSlackAsync()); } private static async Task IntegrateWithSlackAsync() { var webhookUrl = new Uri("https://hooks.slack.com/services/XXX/YYY/ZZZ"); var slackClient = new SlackClient(webhookUrl); while (true) { Console.Write("Type a message: "); var message = Console.ReadLine(); var response = await slackClient.SendMessageAsync(message); var isValid = response.IsSuccessStatusCode ? "valid" : "invalid"; Console.WriteLine($"Received {isValid} response."); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class Program { public static void Main ( string [ ] args ) { Task . WaitAll ( IntegrateWithSlackAsync ( ) ) ; } private static async Task IntegrateWithSlackAsync ( ) { var webhookUrl = new Uri ( "https://hooks.slack.com/services/XXX/YYY/ZZZ" ) ; var slackClient = new SlackClient ( webhookUrl ) ; while ( true ) { Console . Write ( "Type a message: " ) ; var message = Console . ReadLine ( ) ; var response = await slackClient . SendMessageAsync ( message ) ; var isValid = response . IsSuccessStatusCode ? "valid" : "invalid" ; Console . WriteLine ( $ "Received {isValid} response." ) ; } } }

Yeah, that’s it – just make sure that you set the valid URI of the Webhook.

Additionaly, you may switch a channel or customize the username sending the message.

As always, I’m providing the source code that can be downloaded here.