Skip to content

Instantly share code, notes, and snippets.

@MisterSingh
Created October 10, 2020 10:40
Show Gist options
  • Save MisterSingh/cc773aa9d741722305888ce22f62e1cd to your computer and use it in GitHub Desktop.
Save MisterSingh/cc773aa9d741722305888ce22f62e1cd to your computer and use it in GitHub Desktop.
Simple Slack webhook in C#
using System;
using Newtonsoft.Json;
using System.Text;
using System.Net;
using System.Collections.Specialized;
public class SlackWebhook
{
private readonly Encoding _encoding = new UTF8Encoding();
public void PostMessage()
{
var uri = new Uri("https://hooks.slack.com/services/xxxAccessTokenxxx");
var message = new { text = "Webhook is working"};
string payloadJson = JsonConvert.SerializeObject(message);
using (WebClient client = new WebClient())
{
NameValueCollection data = new NameValueCollection();
data["payload"] = payloadJson;
var response = client.UploadValues(uri, "POST", data);
string responseText = _encoding.GetString(response);
}
}
}
@MisterSingh
Copy link
Author

MisterSingh commented Oct 10, 2020

This is a basic slack webhook integration using C#. Use the slack documentation to format your messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment