Slack Integration with C#


Slack is a free messaging app for teams. It is designed to help teams to improve their communication. Not just your messages, but all your files, images, PDFs, documents, and spreadsheets can be dropped right into Slack and shared with anyone you want.

Slack helps you work in the moment, making it faster and easier to communicate with your team and with all the collective knowledge at your fingertips. Using Slack is nothing but more productivity, more transparency, and less email.

The following are the features of Slack:

  • Group Conversation
  • File Sharing
  • Deep, Contextual search
  • Always in sync
  • Over 80 integrations
  • Security

Need of Integration:

All your tools in one place: Connect all the tools you use to Slack and avoid all that constant switching among apps. Set up your integration so that you get all your notifications directly within Slack.

Slack integrates with over 80 external services to help you pull information from outside tools in a way that’s timely, relevant and always searchable. No more switching among apps. Bring it all together in one simple place.

How to Integrate:

To integrate your application with Slack, you need to use the following procedure:

  • If you haven’t registered an account on Slack then you need to first register on Slack. To register on Slack go to And then click on Sign up for free link.
  • To send notifications to the Slack you must have the Incoming Webhooks integration enabled. To enable Incoming Webhooks use the following procedure:
  • Go to the following URL and click on the Incoming Webhooks link as shown in the figure.

  • If you have already signed in then there is no need to sign in again otherwise to proceed further you are asked to sign in here. After signing in you will see the following window:
  • Now click on the choose a channel drop down list and select a channel where you want to send notifications to.
  • And click on Add Incoming Webhooks Integration.
  • Now you get the window where you can see Setup Instructions, Message Attachments and Integration Settings options. In the Integration settings option you can change the Channel name, you can provide a descriptive label and can customize the name and the icon. Now click on Copy URL so that we can use that URL in our C# code. Finally click on the Save Settings button available at the end.
  • Now write a C# class to post messages to a Slack channel. This class uses the Newtonsoft Json.NET serializer available via NuGet.

To install Json.NET, open your project in Visual Studio. Then right-click on the project name in Solution Explorer and select Manage Nuget Packages. You will see a new window. In that choose Online and search for a Newtonsoft as shown in the following image. You will get Json.NET. Now click on the Install button.

  • Now write the SlackClient class as follows:
using Newtonsoft.Json;  
using System;  
using System.Collections.Specialized;  
using System.Net;  
using System.Text;  
//A simple C# class to post messages to a Slack channel  
//Note: This class uses the Newtonsoft Json.NET serializer available via NuGet  
public class SlackClient  
    private readonly Uri _uri;  
    private readonly Encoding _encoding = new UTF8Encoding();  
    public SlackClient(string urlWithAccessToken)  
        _uri = new Uri(urlWithAccessToken);  
    //Post a message using simple strings  
    public void PostMessage(string text, string username = null, string channel = null)  
        Payload payload = new Payload()  
            Channel = channel,  
            Username = username,  
            Text = text  
    //Post a message using a Payload object  
    public void PostMessage(Payload payload)  
        string payloadJson = JsonConvert.SerializeObject(payload);  
        using (WebClient client = new WebClient())  
            NameValueCollection data = new NameValueCollection();  
            data["payload"] = payloadJson;  
            var response = client.UploadValues(_uri, "POST", data);  
            //The response text is usually "ok"  
            string responseText = _encoding.GetString(response);  
//This class serializes into the Json payload required by Slack Incoming WebHooks  
public class Payload  
    public string Channel { get; set; }  
    public string Username { get; set; }  
  • Now write SlackClientTest.cs as follows:
void TestPostMessage()  
     string urlWithAccessToken = "https://{your_account}{your_access_token}";  
     SlackClient client = new SlackClient(urlWithAccessToken);  
     client.PostMessage(username: "kamleshbhor",  
                text: "THIS IS A TEST MESSAGE!!",  
                channel: "#general");  
  • Now paste in the Webhook URL you copied instead of {your_access_token} in the TestPostMessage() method and test the code.

You may also be interested in...

Article By Kamlesh Bhor

Feel free to comment below about this article.

Discuss about post

Subscribe to my weekly newsletter