20 Apr 2016
Slack is my favourite app at the moment, and I've been obsessing about integrating it with everything lately. This post gives you the code and simple instructions on how to create your own custom integration for slack, written in C# for .NET.
On the slack site, go to the page to set up an incoming webhook, sign into your Slack account.
Where it says Post to Channel, choose #general and click on 'Add Incoming WebHooks Integration'
Copy the WebHook URL and paste it in a notepad or somewhere for later.
Add this class somewhere in your project.
//Written by Paul Seal for http://www.codeshare.co.uk
//Free for use by anyone however they want
//Buy me a coffee sometime, or donate on my PayPal link if you want.
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Slack.Net
{
/// <summary>
/// This is the main class for Slack Messages
/// </summary>
public class Message
{
public string PostUrl { get; set; }
public string Text { get; set; }
public string Channel { get; set; }
public string Icon_Emoji { get; set; }
public string Username { get; set; }
private string PostData
{
get
{
StringBuilder postData = new StringBuilder();
postData.Append("payload={");
if (!String.IsNullOrEmpty(this.Text))
{
postData.Append("\"text\":\"" + this.Text + "\"");
}
if (!String.IsNullOrEmpty(this.Channel))
{
postData.Append(",\"channel\":\"" + this.Channel + "\"");
}
if (!String.IsNullOrEmpty(this.Icon_Emoji))
{
postData.Append(",\"icon_emoji\":\"" + this.Icon_Emoji + "\"");
}
if (!String.IsNullOrEmpty(this.Username))
{
postData.Append(",\"username\": \"" + this.Username + "\"");
}
postData.Append("}");
return postData.ToString();
}
}
/// <summary>
/// Constructor for the Message class
/// </summary>
/// <param name="postUrl">The Webhook URL</param>
/// <param name="text">The message to send</param>
/// <param name="channel">The channel name if you want to send the message to a different channel</param>
/// <param name="icon_emoji">The emoji to use for the icon of the BOT user</param>
/// <param name="username">The name to use for the BOT user</param>
public Message(string postUrl, string text, string channel = null, string icon_emoji = null, string username = null)
{
PostUrl = postUrl;
Text = text;
Channel = channel;
Icon_Emoji = icon_emoji;
Username = username;
}
/// <summary>
/// Calls the method to process the web request
/// </summary>
/// <returns>A string value for the response from the server</returns>
public string Send()
{
return ProcessRequest(this.PostUrl, this.PostData);
}
/// <summary>
/// Simple method for processing web requests
/// </summary>
/// <param name="postUrl">The url to post to</param>
/// <param name="postData">The data to post in the web request</param>
/// <returns>A string value for the response from the server</returns>
private string ProcessRequest(string postUrl, string postData)
{
WebRequest request = WebRequest.Create(postUrl);
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
}
}
Now to use this code you can just call it like this, remember to use your WebHook URL:
public class Program
{
public static void Main()
{
string webhookUrl = "https://hooks.slack.com/services/fake-url/not-real-url/just-for-demo-purposes";
string text = "Testing slack integration";
string channelName = "#general";
string emojiIcon = ":computer:";
string username = "codeshare.co.uk";
Slack.Net.Message message = new Slack.Net.Message(webhookUrl, text, channelName, emojiIcon, username);
message.Send();
}
}
In the example above you can see I entered the text of the message, channelName, emojiIcon and username. The last 3 are optional so you can set them as null or leave them off if you want.
If you have created your webhook link you can test it out quickly here on dotnetfiddle.net
Try it out and let me know how you get on.