Slack
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 www.slack.com. And then click on Sign up for free link.
- To sending 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 Jason.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;
-
-
-
- public class SlackClient
- {
- private readonly Uri _uri;
- private readonly Encoding _encoding = new UTF8Encoding();
-
- public SlackClient(string urlWithAccessToken)
- {
- _uri = new Uri(urlWithAccessToken);
- }
-
-
- public void PostMessage(string text, string username = null, string channel = null)
- {
- Payload payload = new Payload()
- {
- Channel = channel,
- Username = username,
- Text = text
- };
-
- PostMessage(payload);
- }
-
-
- 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);
-
-
- string responseText = _encoding.GetString(response);
- }
- }
- }
-
-
- 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.