Slack Integration with C#
Introduction:
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 https://slack.com. 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 api.slack.com as shown in the figure.
- Now click on the link incoming webhook integration.
- 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 }; PostMessage(payload); } //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 { [JsonProperty("channel")] public string Channel { get; set; } [JsonProperty("username")] public string Username { get; set; } [JsonProperty("text")] Public
- Now write SlackClientTest.cs as follows:
void TestPostMessage() { string urlWithAccessToken = "https://{your_account}.slack.com/services/hooks/incoming-webhook?token={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...
- Slack Integration with C#
- Xamarin Forms: Getting Started
- Part 1: Introduction to C#
- Part 2: Arrays and Function in C#
- Part 3: OOPs Concepts in C#
- Part 4: Constructor/Destructor in C#
- Part 5: Abstract class in C#
- Part 6: Indexer,Delegates, Anonymous Method, Lambda expression in C#
- Part 7: Collections in C#
- Part 8: Generics in C#
- Part 9: Attributes in C#
- Part 10: Sealed/Partial class in C#
- Part 11: MultiThreading in C#
Discuss about post