What is Slack

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

Sending Slack messages programmatically

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:

New member registered

I wrote a NuGet Package called SlackBotMessages, as of writing this post the version is 2.0.0

NuGet

Install via NuGet:

Install-Package SlackBotMessages

Or click here to go to the NuGet package landing page

Registering for your WebHook Url with Slack

Click here to set up an incoming WebHook

  • Sign in to Slack
  • Choose a channel to post to
  • Then click on the green button Add Incoming WebHooks integration
  • You will be given a WebHook Url. Keep this private. Use it when you set up the SBMClient. See example below.

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.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

How to include scripts from partial views in MVC and Umbraco

This post will show you how you can include scripts from inside an MVC partial view rather than havi…

Read Post

How to fix the error "Authentication failed because the remote party has closed the transport stream"

This post will show you how to fix the error "Authentication failed because the remote party has clo…

Read Post

How to solve the error Could not load file or assembly System.Security.Principal.Windows

This post help you solve the error System.IO.FileNotFoundException Could not load file or assembly S…

Read Post

How to solve the error "Could not load file or assembly 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0 ..."

This post will show you how to solve the error "Could not load file or assembly 'Microsoft.CodeDom.P…

Read Post