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.

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.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.
uSkinned

  • More than a theme for Umbraco CMS, take full control of your content and design with a feature-rich, award-nominated & content editor focused website platform.
UmbHost

  • Affordable, Geo-Redundant, Umbraco hosting which gives back to the community by sponsoring an Umbraco Open Source Developer with each hosting package sold.