Skip to content

Latest commit

History

130 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Slack.Webhooks Continuous StuffNuGet Version

Even simpler integration with Slack's Incoming/Outgoing webhooks API for .net

IMPORTANT

On Feb 19th 2020 Slack will end support for TLS version 1.0 and 1.1. This means you may (depending on your .NET version) need to force the use of TLS1.2.

If you receive an error stating that "The underlying connection was closed:" it's quite possibly a TLS issue. You can work around this by setting the default TLS version using the following:

System.Net.ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12;

Change Log

v1.1.4

  • Add Block elements to Attachments

v1.1.3

  • Fixes potential deadlock issues

v1.1.2

  • Fix SlackMessage.Clone does not clone all properties

v1.1.0

  • Support Slack's Block Kit with SlackMessage.Blocks property
  • Support Thread replies with SlackMessage.ThreadId property
  • Changed the whole Emoji setup. SlackMessage.Emoji is now a string and Emoji.* are constants. This shouldn't cause problems in the most part!
  • Allow HttpClient to be injected into SlackClient.
  • SlackClient implements IDisposable to match the contained HttpClient instance even though this isn't the recommended usage. SlackClient, like HttpClient, there should only be a single instance of this class within the lifecycle of your application, for more information on why see: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

v1.0.0 BREAKING CHANGES

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.

Outgoing Webhooks

Blog Post

Incoming Webhooks

Requirements:

  1. 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
  2. Slack.Webhooks depends on JSON.net
  3. Compatible with .NET Standard 2.0. If you need .NET Framework 4.5 or older .NET Standard support, use version 1.1.5 or earlier.

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.

varslackClient=newSlackClient("[YOUR_WEBHOOK_URL]");

Create a new SlackMessage

varslackMessage=newSlackMessage{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:

varslackAttachment=newSlackAttachment{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=newList<SlackField>{newSlackField{Title="Notes",Value="This is much *easier* than I thought it would be."}}};slackMessage.Attachments=newList<SlackAttachment>{slackAttachment};

Please see the Docs for further info on attachments.

You can also provide a list of Block objects in SlackMessage.Blocks to create more interactive content:

Blocks.Divider

slackMessage.Blocks=newList<Block>{newBlocks.Divider()};

Blocks.Image

slackMessage.Blocks=newList<Block>{newBlocks.Image{AltText="Sexy Skyline",ImageUrl="https://placekitten.com/500/500",Title=newTextObject("Hello, this is text")}};

Blocks.Context

slackMessage.Blocks=newList<Block>{newBlocks.Context{Elements=newList<Interfaces.IContextElement>{newTextObject("_markdown_"){Type=TextObject.TextType.Markdown},newElements.Image{ImageUrl="https://placekitten.com/200/300",AltText="Random Kitten"}}}};

Blocks.Section

slackMessage.Blocks=newList<Block>{newBlocks.Section{Text=newTextObject("_markdown_"){Type=TextObject.TextType.Markdown},Fields=newList<TextObject>{newTextObject{Text="Field 1"},newTextObject{Text="Field 2"},newTextObject{Text="Field 3"}}}};

Blocks.Section with Accessory

varconfirmation=newConfirmation{Confirm=newTextObject("This OK?"),Text=newTextObject("This is the Text"),Deny=newTextObject("This is the Deny Text"),Title=newTextObject("Title")};varoptions=newList<Option>{newOption{Text=newTextObject("OPtion1"),Value="option1"},newOption{Text=newTextObject("OPtion2"),Value="option2"},newOption{Text=newTextObject("OPtion3"),Value="option3"},};varoptionGroups=newList<OptionGroup>{newOptionGroup{Label=newTextObject("Label 1"),Options=options},newOptionGroup{Label=newTextObject("Label 2"),Options=options}};Elements.Elementelement;element=newButton{Text=newTextObject{Text="Button Text"},ActionId="Button1_Click",};element=newElements.Overflow{Confirm=confirmation,Options=options};element=newElements.DatePicker{Placeholder=newTextObject("Select a date")};element=newElements.SelectUsers{Placeholder=newTextObject("Select a user..."),Confirm=confirmation};element=newElements.SelectChannels{Placeholder=newTextObject("Select a channel"),Confirm=confirmation};element=newElements.SelectConversations{Placeholder=newTextObject("Select a conversation"),Confirm=confirmation};element=newElements.SelectStatic{Options=options,Placeholder=newTextObject("Options Placeholder")};element=newElements.SelectStatic{OptionGroups=optionGroups,Placeholder=newTextObject("OptionGroup placeholder")};element=newElements.MultiSelectUsers{Placeholder=newTextObject("Select a user..."),Confirm=confirmation};element=newElements.MultiSelectChannels{Placeholder=newTextObject("Select a channel")};element=newElements.MultiSelectConversations{Placeholder=newTextObject("Select a conversation")};element=newElements.MultiSelectStatic{Options=options,Placeholder=newTextObject("Options Placeholder")};element=newElements.MultiSelectStatic{OptionGroups=optionGroups,Placeholder=newTextObject("OptionGroup placeholder")};slackMessage.Blocks=newList<Block>{newBlocks.Section{Text=newTextObject("_markdown_"){Type=TextObject.TextType.Markdown},Accessory=element}};

And Post it to Slack

slackClient.Post(slackMessage);

About

Even simpler integration with Slack's Incoming and Outgoing webhooks

Resources

Stars

172 stars

Watchers

11 watching

Forks

Releases

Packages

Used by

Contributors

Languages