DiegoOnSoftware.Slack.Net
1.0.0
dotnet add package DiegoOnSoftware.Slack.Net --version 1.0.0
NuGet\Install-Package DiegoOnSoftware.Slack.Net -Version 1.0.0
<PackageReference Include="DiegoOnSoftware.Slack.Net" Version="1.0.0" />
<PackageVersion Include="DiegoOnSoftware.Slack.Net" Version="1.0.0" />
<PackageReference Include="DiegoOnSoftware.Slack.Net" />
paket add DiegoOnSoftware.Slack.Net --version 1.0.0
#r "nuget: DiegoOnSoftware.Slack.Net, 1.0.0"
#:package DiegoOnSoftware.Slack.Net@1.0.0
#addin nuget:?package=DiegoOnSoftware.Slack.Net&version=1.0.0
#tool nuget:?package=DiegoOnSoftware.Slack.Net&version=1.0.0
Slack.Net
Slack.Net is .NET client for Slack Incoming Webhooks with message throttling support.
Setup and Overview
Package Manager:
Install-Package DiegoOnSoftware.Slack.Net
.NET CLI:
dotnet add package DiegoOnSoftware.Slack.Net
Paket CLI:
paket add DiegoOnSoftware.Slack.Net
How to use it with AspNet Core
Edit Startup.cs and add the following line to configure the SlackNotifier service:
services.AddSlackNotifier(new SlackNotifierConfig { IsActive = true, IncomingWebhookUrl = "<your incoming webhook url here>" });If IsActive property is set to false no message will be dispatched. It is useful if you don't want your application to send messages in development environment.
You should store configuration data in appsettings.json file. Example:
"SlackNotifierConfig": { "IsActive": true, "IncomingWebhookUrl": "<your incoming webhook url here>" }Then in Startup.cs:
services.AddSlackNotifier(Configuration.GetSection("SlackNotifierConfig").Get<SlackNotifierConfig>());With configuration in place, inject the ISlackNotifier interface when needed. Example:
public ExampleController(ISlackNotifier notifier) { _notifier = notifier; }To send a notification to a slack channel, just call Notificate method:
await _notifier.Notificate(new SimpleMessage(message.Text, message.Channel));
Message types
You can choose Simple or Rich message types.
Simple message
A simple text message.
await _notifier.Notificate(new SimpleMessage(message.Text, "#channel_name"));
The code above will produce a message like this:
Rich message
Build distinctive messages referencing images, external websites, and highlight relevant pieces of data. Then simplify workflows by attaching interactive buttons. You can get more info on attachment options here.
var message = new RichMessage("#channel_name");
var attachment = new Attachment
{
Fallback = "Required plain-text summary of the attachment.",
Color = "#2eb886",
PreText = "Optional text that appears above the attachment block",
AuthorName = "Bobby Tables",
AuthorLink = "http://flickr.com/bobby/",
AuthorIcon = "https://pbs.twimg.com/profile_images/1037487541958664192/bPU4tGL4_bigger.jpg",
Title = "Slack API Documentation",
TitleLink = "https://api.slack.com/",
Text = "Optional text that appears within the attachment",
Footer = "DiegoOnSoftware Slack.Net",
Timestamp = (int)DateTime.Now.ToFileTimeUtc()
};
attachment.AddField(new Field
{
Title = "Priority",
Value = "High"
});
message.AddAttachment(attachment);
await _notifier.Notificate(message);
The code above will produce a message like this:
Slack.Net also supports Message Buttons:
var message = new RichMessage();
message
.AddAttachment(new Attachment { Text = "This is an awesome action description" }
.AddAction(new LinkButton("Approve order", new Uri("http://localhost/approve-order")))
.AddAction(new LinkButton("Cancel order", new Uri("http://localhost/cancel-order"), "danger")));
await notifier.Notificate(message);
The code above will produce a message like this:
Message throttling
Commonly developers use Slack as tool for application monitoring. It's very useful to send real-time notifications to a Slack's channel when something fail (in a try/catch block, for example). The problem with this approach is that you channel might get flooded with repeated messages when it would be enough to get notified once.
For scenarios like this you may want to use Slack.Net message throttling. In the example below, the message "Hello, Slack!" is going to be dispatched just once, as it has a throttle time of 60 seconds. When message's throttle time expire the message will be dispatched, then will again be throttled for the next 60 seconds.
private async Task SendThrottledMessage(ISlackNotifier notifier)
{
var message = new SimpleMessage("Hello, Slack!").Throttle(60);
foreach(var iteration in Enumerable.Range(1, 10))
{
await notifier.Notificate(message);
}
}
Slack.Net supports message throttling for both SimpleMessage and RichMessage.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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 was computed. |
| .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. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 2.1.1)
- Newtonsoft.Json (>= 11.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 992 | 11/3/2018 |