Even simpler integration with Slack's Incoming/Outgoing webhooks API for .net
This package targets .NET 10 (net10.0) only. Earlier releases targeted .NET Standard 2.0, which also made them usable from .NET Framework; that is no longer the case. Projects on .NET Framework, or on a .NET version earlier than 10, should stay on the last .NET Standard 2.0 release.
Slack ended support for TLS 1.0 and 1.1 on 19 February 2020. .NET 10 negotiates TLS 1.2 or later by default, so no ServicePointManager workaround is needed.
- Target .NET 10 only; drop .NET Standard 2.0 (and therefore .NET Framework) support
- Add Block elements to Attachments
- Fixes potential deadlock issues
- Fix SlackMessage.Clone does not clone all properties
- 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/
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
- Requires .NET 10 or later.
Download:
Package is hosted on Nuget and can be installed from the package manager:
PM> Install-Package Slack.Webhooks
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);