-
Star
(124)
You must be signed in to star a gist -
Fork
(41)
You must be signed in to fork a gist
-
-
Save jogleasonjr/7121367 to your computer and use it in GitHub Desktop.
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 string Text { get; set; } | |
} |
void TestPostMessage() | |
{ | |
var urlWithAccessToken = "https://hooks.slack.com/services/{YOUR}/{ACCESS}/{TOKENS}; | |
var client = new SlackClient(urlWithAccessToken); | |
client.PostMessage(username: "Mr. Torgue", | |
text: "THIS IS A TEST MESSAGE! SQUEEDLYBAMBLYFEEDLYMEEDLYMOWWWWWWWW!", | |
channel: "#general"); | |
} |
<Query Kind="Program"> | |
<Reference><RuntimeDirectory>\SMDiagnostics.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Configuration.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Net.Http.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Runtime.Serialization.dll</Reference> | |
<Reference><RuntimeDirectory>\System.ServiceModel.Internals.dll</Reference> | |
<NuGetReference>Newtonsoft.Json</NuGetReference> | |
<Namespace>System.Collections.Specialized</Namespace> | |
<Namespace>System.Net</Namespace> | |
<Namespace>System.Net.Http</Namespace> | |
<Namespace>System.Net.Http.Headers</Namespace> | |
<Namespace>System.Runtime.Serialization.Json</Namespace> | |
<Namespace>Newtonsoft.Json</Namespace> | |
</Query> | |
void Main() | |
{ | |
var urlWithAccessToken = "https://hooks.slack.com/services/{YOUR}/{ACCESS}/{TOKENS}"; | |
var client = new SlackClient(urlWithAccessToken); | |
client.PostMessage(username: "Mr. Torgue", | |
text: "THIS IS A TEST MESSAGE! SQUEEDLYBAMBLYFEEDLYMEEDLsaMOWWWWWWWW!", | |
channel: "#sandbox"); | |
} | |
//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 string Text { get; set; } | |
} |
This is great, thank you... now to just find out how to determine whether the response was a bad one or not. ( ie. response.IsSuccessStatusCode with httpclients )
Up and running in just a few minutes. Thanks!
How can I get the token={your_access_token} value ?
How to upload one image and message to channel?
This is helpful. Thanks
is there a way to tag a user or multiple users?
Does this work for private channels as well?
I'm getting a 404 error at line 40 of the stack client. What all information on the code in this file needs to be changed to fit personal use?
You just need to change the urlWithAccessToken
variable. Something like https://hooks.slack.com/services/{YOUR}/{ACCESS}/{TOKENS}
. If you navigate to the /apps page in your slack workspace settings on the web, find (or add) an integration for Incoming Webhooks -- your URL to use in this bit of code will be on that page.
Thank you!
What are some of the JSON properties for slack.
I want to work with attachments, colors, images and ...
Thanks much.