26 Jun 2019
Slack is an excellent tool used by teams, especially those the tech industry.
You can communicate in realtime with your team mates.
I am a member of lots of different Slack teams, from work ones to community ones.
I even have a Slack team for this website, that only I am a member of.
One of the great things about Slack is that you can have it on your phone too and you can receive notifications.
In this post I will show you how you can send messages to Slack programmatically using C#.
The scenario here is that you have a new member who has registered on your website.
You want the website to send a slack message which will look like this:
I wrote a NuGet Package called SlackBotMessages, as of writing this post the version is 2.0.0
Install via NuGet:
Install-Package SlackBotMessages
Or click here to go to the NuGet package landing page
Click here to set up an incoming WebHook
Then you can send the message like this:
var WebHookUrl = "https://hooks.slack.com/services/Your/WebHook/Url"; var client = new SbmClient(WebHookUrl); var message = new Message("New member registered.") .SetUserWithEmoji("Website", Emoji.Loudspeaker); message.AddAttachment(new Attachment() .AddField("Name", "Paul Seal", true) .AddField("Website", "codeshare.co.uk", true) .AddField("Job Title", "Umbraco Developer", true) .AddField("Company", "Moriyama", true) .AddField("Bio", "Paul is an Umbraco MVP, working for the Umbraco Gold Partner Moriyama. He is passionate about Umbraco and Web Development in general. He loves to create open source packages and likes to share his experience and knowledge through his website codeshare.co.uk and his YouTube channel.") .SetThumbUrl("https://codeshare.co.uk/media/1508/paul-seal-profile-2019.jpg?width=500&height=500&mode=crop&anchor=top") .SetColor("#f96332") ); client.Send(message);
That should be it. If you replaced the WebHookUrl with the one you registered then it should just work.
You can find out more about SlackBotMessages by visiting the project on GitHub.