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?

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 solve the error assets file project.assets.json not found in Visual Studio

This post tells you how to solve the error assets file project.assets.json not found in Visual Studi…

Read Post

Code to help you debug an umbraco issue on a remote site

This post gives you some razor code to help you see the values of the IPublishedContent item's prope…

Read Post

How to set the default page base type to UmbracoViewPage in Umbraco

In this post I share with you what Ronald Barendse taught us about setting the pageBaseType in Umbra…

Read Post

How to fix corrupt accented characters in Excel for a C# export as CSV

This post will help you fix the issue where you csv export has corrupt characters when opening it in…

Read Post