Slack Messaging in C#

Chris Smith
3 min readJan 17, 2023

--

Photo by Stephen Phillips - Hostreviews.co.uk on Unsplash

Introduction
Slack is a messaging app that connects people within organisations. It provides colleagues with instant messaging or full team collaboration within a dedicated workspace channel.

Slack currently provides easy integration with C# allowing an application to POST a message to a Slack workspace. A few examples of why an application would POST to Slack are:

  • Alerting on failures within a system
  • Reporting on specific metrics/events which have occurred during a time period
  • Informing the team that a new member is now on board

In this article we are going to generate a message for the third bullet point — informing the team of a new member.

What isn’t covered in this article or within the sample code is dependency injection, error handling, testing or any similar software coding practices. It’s expected that these should be added as standard into any production level code/application.

All sample code is written in C# and can be found within my GitHub.

Slack Webhook Setup
In order to send (POST) messages from the C# client we require an endpoint (URL) within our Slack workspace. The endpoint URL is created as part of a Webhook and is setup by:

  • Creating a Slack app
  • Enabling Incoming Webhook
  • Creating an Incoming Webhook

I won’t go through the individual steps here, however full instructions can be found on the Slack website.

NuGet
As a prerequisite the Slack.Webhooks NuGet package should be added to the C# project. This can be installed either by using the NuGet package manager window or via the package manager command:

Install-Package Slack.Webhooks

C# Client Application
In my Slack Integration sample app I have 4 key files:

  • SlackClient.cs
    This class is responsible for the creation of the Webhook client and posting the message to Slack using that client. The Webhook URL is injected into the class within the SlackConfig object.
  • SlackMessageBuilder.cs
    A wrapper class around some of the main components available when creating a Slack message. It provides the functionality to add individual sections, section fields and a divider to our Slack message.
    There are many other components and formatting available, please refer to the official Slack documentation for these.
  • SlackConfig.cs
    Configuration file containing a single property to hold the Webhook URL. This is injected into the constructor of the SlackClient class.
  • Program.cs
    Entry point of the application and is used to create the SlackConfig and SlackClient objects. It is also responsible for building the Slack message and calling the slackClient.PostMessage method. Note that the calls to the overloaded slackMessageBuilder.Add method are used to format different sections of the message with individual styling.

Final Slack Message
On running the application the message is generated and sent to Slack.

The sample message built using lines 6 to 11 of Program.cs is displayed below as a fully formatted message within the Slack application.

Conclusion
In this article I’ve provided a lightweight application demonstrating the integration required to send a message between C# and Slack. Hopefully this is helpful and provides you with a starting point for your own Slack messaging.

Thanks for reading and I’d appreciate it if you could give this post a like and share.

--

--

Chris Smith
Chris Smith

Written by Chris Smith

Senior Software Engineer at Kainos who is normally found on a golf course or watching football. All views expressed here are my own.

No responses yet