jjm.one.Slack.Webhooks 1.0.0-alpha.1

This is a prerelease version of jjm.one.Slack.Webhooks.
dotnet add package jjm.one.Slack.Webhooks --version 1.0.0-alpha.1                
NuGet\Install-Package jjm.one.Slack.Webhooks -Version 1.0.0-alpha.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="jjm.one.Slack.Webhooks" Version="1.0.0-alpha.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add jjm.one.Slack.Webhooks --version 1.0.0-alpha.1                
#r "nuget: jjm.one.Slack.Webhooks, 1.0.0-alpha.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install jjm.one.Slack.Webhooks as a Cake Addin
#addin nuget:?package=jjm.one.Slack.Webhooks&version=1.0.0-alpha.1&prerelease

// Install jjm.one.Slack.Webhooks as a Cake Tool
#tool nuget:?package=jjm.one.Slack.Webhooks&version=1.0.0-alpha.1&prerelease                

Slack.Webhooks

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

Forked Project

This is a fork of Slack.Webhooks, which can be found at https://github.com/mrb0nj/Slack.Webhooks.

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 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.

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

Blocks.Divider

slackMessage.Blocks = new List<Block>
    {
        new Blocks.Divider()
    };

Blocks.Image

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

Blocks.Context

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

Blocks.Section

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

Blocks.Section with Accessory

var confirmation = new Confirmation
    {
        Confirm = new TextObject("This OK?"),
        Text = new TextObject("This is the Text"),
        Deny = new TextObject("This is the Deny Text"),
        Title = new TextObject("Title")
    };

var options = new List<Option>
    {
        new Option
            {
                Text = new TextObject("OPtion1"),
                Value = "option1"
            },
        new Option
            {
                Text = new TextObject("OPtion2"),
                Value = "option2"
            },
        new Option
            {
                Text = new TextObject("OPtion3"),
                Value = "option3"
            },
    };

var optionGroups = new List<OptionGroup>
    {
        new OptionGroup
            {
                Label = new TextObject("Label 1"),
                Options = options
            },
        new OptionGroup
            {
                Label = new TextObject("Label 2"),
                Options = options
            }
    };


Elements.Element element;
element = new Button
    {
        Text = new TextObject { Text = "Button Text" },
        ActionId = "Button1_Click",
    };

element = new Elements.Overflow
    {
        Confirm = confirmation,
        Options = options
    };

element = new Elements.DatePicker
    {
        Placeholder = new TextObject("Select a date")
    };

element = new Elements.SelectUsers
    {
        Placeholder = new TextObject("Select a user..."),
        Confirm = confirmation
    };

element = new Elements.SelectChannels
    {
        Placeholder = new TextObject("Select a channel"),
        Confirm = confirmation
    };

element = new Elements.SelectConversations
    {
        Placeholder = new TextObject("Select a conversation"),
        Confirm = confirmation
    };

element = new Elements.SelectStatic
    {
        Options = options,
        Placeholder = new TextObject("Options Placeholder")
    };

element = new Elements.SelectStatic
    {
        OptionGroups = optionGroups,
        Placeholder = new TextObject("OptionGroup placeholder")
    };

element = new Elements.MultiSelectUsers
    {
        Placeholder = new TextObject("Select a user..."),
        Confirm = confirmation
    };

element = new Elements.MultiSelectChannels
    {
        Placeholder = new TextObject("Select a channel")
    };

element = new Elements.MultiSelectConversations
    {
        Placeholder = new TextObject("Select a conversation")
    };

element = new Elements.MultiSelectStatic
    {
        Options = options,
        Placeholder = new TextObject("Options Placeholder")
    };

element = new Elements.MultiSelectStatic
    {
        OptionGroups = optionGroups,
        Placeholder = new TextObject("OptionGroup placeholder")
    };

slackMessage.Blocks = new List<Block>
    {
        new Blocks.Section
            {
                Text = new TextObject("_markdown_")
                        {
                            Type = TextObject.TextType.Markdown
                        },
                Accessory = element
            }
    };

And Post it to Slack

slackClient.Post(slackMessage);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on jjm.one.Slack.Webhooks:

Package Downloads
jjm.one.Serilog.Sinks.SlackWebHook

A basic Slack Sink for the Serilog framwork.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-alpha.1 94 10/8/2024