Skip to content

Instantly share code, notes, and snippets.

@radumg
Forked from jogleasonjr/SlackClient.cs
Last active January 15, 2023 13:04
Show Gist options
  • Save radumg/2c2764e7576ac39c94a9 to your computer and use it in GitHub Desktop.
Save radumg/2c2764e7576ac39c94a9 to your computer and use it in GitHub Desktop.
A simple C# class to post messages to a Slack channel. You must have the Incoming WebHooks Integration enabled. This class uses the Newtonsoft Json.NET serializer available via NuGet. Scroll down for an example test method. Enjoy!
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, string emoji = null, string icon = null)
{
Payload payload = new Payload()
{
Channel = channel,
Username = username,
Text = text,
Emoji = emoji,
Icon = icon
};
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 string Text { get; set; }
[JsonProperty("icon_emoji")]
public string Emoji { get; set; }
[JsonProperty("icon_url")]
public string Icon { get; set; }
}
void TestPostMessage()
{
string urlWithAccessToken = "https://hooks.slack.com/services/TXXXXXXXX/BXXXXXXXX/xxxxxxxxxxxxxxxxxxxxxxxx";
// replace the URL above with the URL Slack provides on the configured Incoming Webhook page. Format included here for reference only.
SlackClient client = new SlackClient(urlWithAccessToken);
try {
// post message with an icon from URL
client.PostMessage(username: "Mr. Kenobi",
text: "This is not the message you are looking for !",
channel: "#general",
icon : "https://slack.com/img/icons/app-57.png"); // icon example from Slack API
}
catch (Exception){
TaskDialog.Show("Could not send message to Slack"); // Autodesk implementation of MessageBox, replace as necessary
throw;
}
try {
// post message with an icon from an emoji
client.PostMessage(username: "Mr. Kenobi",
text: "This is not the message you are looking for !",
channel: "#general",
emoji : ":ghost:"); // parsing of the emoji is handled by Slack
}
catch (Exception){
TaskDialog.Show("Could not send message to Slack"); // Autodesk implementation of MessageBox, replace as necessary
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment