Created
October 10, 2020 10:40
-
-
Save MisterSingh/cc773aa9d741722305888ce22f62e1cd to your computer and use it in GitHub Desktop.
Simple Slack webhook in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a basic slack webhook integration using C#. Use the slack documentation to format your messages.