Even simpler integration with Slack's Incoming/Outgoing webhooks API for .net
We no longer use RestSharp in favour of HttpClient - this however means that .NET 4.0 and below are no longer supported.
Also, the PostAsync method signature has changed. The return type is now Task<bool>
in place of Task<IRestResponse>
which was tied directly to RestSharp.
Requirements:
- You must first enable the Webhooks integration for your Slack Account to get the Token. You can enable it here: https://slack.com/services/new/incoming-webhook
- Slack.Webhooks depends on JSON.net
- Compatible with .NET 4.5+ and .NET Core. If you need .NET 3.5/4 you can use an older release, but this may be out of date.
Download:
Package is hosted on Nuget and can be installed from the package manager:
PM> Install-Package Slack.Webhooks
For older .NET framework support:
PM> Install-Package Slack.Webhooks -Version 0.1.8
Then, create a SlackClient with your Webhook URL.
var slackClient = new SlackClient("[YOUR_WEBHOOK_URL]");
Create a new SlackMessage
var slackMessage = new SlackMessage
{
Channel = "#random",
Text = "Your message",
IconEmoji = Emoji.Ghost,
Username = "nerdfury"
};
By default the text can contain Markdown but this default behaviour can be disabled:
slackMessage.Mrkdwn = false;
More info on message formatting can be found in the Docs
Attachments can be added to a message:
var slackAttachment = new SlackAttachment
{
Fallback = "New open task [Urgent]: <http://url_to_task|Test out Slack message attachments>",
Text = "New open task *[Urgent]*: <http://url_to_task|Test out Slack message attachments>",
Color = "#D00000",
Fields =
new List<SlackField>
{
new SlackField
{
Title = "Notes",
Value = "This is much *easier* than I thought it would be."
}
}
};
slackMessage.Attachments = new List<SlackAttachment> {slackAttachment};
Please see the Docs for further info on attachments.
And Post it to Slack
slackClient.Post(slackMessage);