Skip to content

Instantly share code, notes, and snippets.

@jogleasonjr
Last active October 20, 2023 15:54
Show Gist options
  • Select an option

  • Save jogleasonjr/7121367 to your computer and use it in GitHub Desktop.

Select an option

Save jogleasonjr/7121367 to your computer and use it in GitHub Desktop.

Revisions

  1. jogleasonjr revised this gist Jun 7, 2019. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions SlackClientTest.cs
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    void TestPostMessage()
    {
    string urlWithAccessToken = "https://{your_account}.slack.com/services/hooks/incoming-webhook?token={your_access_token}";
    var urlWithAccessToken = "https://hooks.slack.com/services/{YOUR}/{ACCESS}/{TOKENS};

    SlackClient client = new SlackClient(urlWithAccessToken);
    var client = new SlackClient(urlWithAccessToken);

    client.PostMessage(username: "Mr. Torgue",
    text: "THIS IS A TEST MESSAGE! SQUEEDLYBAMBLYFEEDLYMEEDLYMOWWWWWWWW!",
    4 changes: 2 additions & 2 deletions SlackClientTest.linq
    Original file line number Diff line number Diff line change
    @@ -15,9 +15,9 @@

    void Main()
    {
    string urlWithAccessToken = "https://{your_account}.slack.com/services/hooks/incoming-webhook?token={your_access_token}";
    var urlWithAccessToken = "https://hooks.slack.com/services/{YOUR}/{ACCESS}/{TOKENS}";

    SlackClient client = new SlackClient(urlWithAccessToken);
    var client = new SlackClient(urlWithAccessToken);

    client.PostMessage(username: "Mr. Torgue",
    text: "THIS IS A TEST MESSAGE! SQUEEDLYBAMBLYFEEDLYMEEDLsaMOWWWWWWWW!",
  2. jogleasonjr revised this gist Oct 23, 2013. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions SlackClient.cs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    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
  3. jogleasonjr revised this gist Oct 23, 2013. 1 changed file with 81 additions and 0 deletions.
    81 changes: 81 additions & 0 deletions SlackClientTest.linq
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    <Query Kind="Program">
    <Reference>&lt;RuntimeDirectory&gt;\SMDiagnostics.dll</Reference>
    <Reference>&lt;RuntimeDirectory&gt;\System.Configuration.dll</Reference>
    <Reference>&lt;RuntimeDirectory&gt;\System.Net.Http.dll</Reference>
    <Reference>&lt;RuntimeDirectory&gt;\System.Runtime.Serialization.dll</Reference>
    <Reference>&lt;RuntimeDirectory&gt;\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()
    {
    string urlWithAccessToken = "https://{your_account}.slack.com/services/hooks/incoming-webhook?token={your_access_token}";

    SlackClient 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; }
    }
  4. jogleasonjr revised this gist Oct 23, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion SlackClient.cs
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    //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;
    @@ -41,6 +41,7 @@ public void PostMessage(Payload payload)
    }
    }

    //This class serializes into the Json payload required by Slack Incoming WebHooks
    public class Payload
    {
    [JsonProperty("channel")]
  5. jogleasonjr revised this gist Oct 23, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions SlackClient.cs
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,7 @@ 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()
    @@ -22,6 +23,7 @@ public void PostMessage(string text, string username = null, string channel = nu
    PostMessage(payload);
    }

    //Post a message using a Payload object
    public void PostMessage(Payload payload)
    {
    string payloadJson = JsonConvert.SerializeObject(payload);
  6. jogleasonjr revised this gist Oct 23, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions SlackClient.cs
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,7 @@ public void PostMessage(Payload payload)

    var response = client.UploadValues(_uri, "POST", data);

    //The response text is usually "ok"
    string responseText = _encoding.GetString(response);
    }
    }
  7. jogleasonjr revised this gist Oct 23, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions SlackClient.cs
    Original file line number Diff line number Diff line change
    @@ -41,11 +41,11 @@ public void PostMessage(Payload payload)
    public class Payload
    {
    [JsonProperty("channel")]
    public string Channel { get; set; }
    public string Channel { get; set; }

    [JsonProperty("username")]
    public string Username { get; set; }
    public string Username { get; set; }

    [JsonProperty("text")]
    public string Text { get; set; }
    public string Text { get; set; }
    }
  8. jogleasonjr revised this gist Oct 23, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SlackClientTest.cs
    Original file line number Diff line number Diff line change
    @@ -6,5 +6,5 @@ void TestPostMessage()

    client.PostMessage(username: "Mr. Torgue",
    text: "THIS IS A TEST MESSAGE! SQUEEDLYBAMBLYFEEDLYMEEDLYMOWWWWWWWW!",
    channel: "#general");
    channel: "#general");
    }
  9. jogleasonjr revised this gist Oct 23, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions SlackClientTest.cs
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,6 @@ void TestPostMessage()
    SlackClient client = new SlackClient(urlWithAccessToken);

    client.PostMessage(username: "Mr. Torgue",
    text: "THIS IS A TEST MESSAGE! SQUEEDLYBAMBLYFEEDLYMEEDLYMOWWWWWWWW!",
    channel: "#general");
    text: "THIS IS A TEST MESSAGE! SQUEEDLYBAMBLYFEEDLYMEEDLYMOWWWWWWWW!",
    channel: "#general");
    }
  10. jogleasonjr created this gist Oct 23, 2013.
    51 changes: 51 additions & 0 deletions SlackClient.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    //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);
    }

    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 string Text { get; set; }
    }
    10 changes: 10 additions & 0 deletions SlackClientTest.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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: "Mr. Torgue",
    text: "THIS IS A TEST MESSAGE! SQUEEDLYBAMBLYFEEDLYMEEDLYMOWWWWWWWW!",
    channel: "#general");
    }