Note: This is the introductory article for CodeProject's Slack API Challenge. The contest officially began on January 7, 2019, and ends on March 7, 2019.
You’re here because you want to write your first Slack app.
Maybe you use Slack because you absolutely love it. Or maybe you don’t love it, but you use it because it’s less terrible than e-mail.
Or maybe you use it because your employer forces you to, even though you work in a crazy open-plan office, and it would be quicker and less painful if your co-workers would just throw a stapler at you when they need your attention instead of eroding your sanity by continually @ mentioning you in a Slack channel until you respond.
Regardless of why you use Slack, writing an app that adds custom functionality to your Slack channels can be lots of fun - and maybe even profitable! Some developers have built entire businesses around apps that make Slack more useful in some way.
Slack apps can be written in pretty much any language. All you need to be able to do is send and receive HTTP requests. Even if you’re working in an obscure language that doesn’t have any web frameworks or HTTP libraries at all, you can just ask the operating system to open a raw socket, and go from there.
So your tech stack won’t stop you from building apps that use the Slack API. Sorry, PL/I developers - if your boss tells you to develop a Slack app, you probably can’t get out of it. Though if you code in APL and deploy to bare metal on mainframes, you might just be out of luck if you want to develop something that can talk to Slack.
No matter what languages or libraries you use, there are a few steps you’ll need to take to get your app up and running. That’s what we’re going to cover in this article, but first we’re going to take a high-level look at what Slack API apps are, and what they can do.
What is a Slack app?
Broadly speaking, a Slack API app is an application that can read from or write to channels in a Slack workspace. Your app can also react to events that provide information about things that happen in a Slack workspace or channel - such as when a user joins or leaves a channel, or when a workspace’s domain name changes.
Slack has three different APIs that you may end up using to build an app. They are:
Slack’s Web API is an RPC-style HTTP API that lets you query for information about a Slack workspace and push information or messages into a Slack workspace. For simple apps, the Web API may be all you need. And since you only need to be able to make HTTP requests to use it, the Web API is available to just about every programming language. If you absolutely, positively need to write a Slack app in COBOL, the Web API is probably your best (and only) hope.
Real Time Messaging API
If you’re a latte-sipping, Haskell-slinging hipster who needs to use the latest and greatest, then Slack’s Real Time Messaging API™ is for you! It’s also a great choice if you’re an awesome professional app developer who wants to be able to receive and react to events in a Slack channel as quickly as possible.
The RTM API relies on WebSockets. So while it is usable from almost any language, you’re out of luck if your language doesn’t have WebSocket support - though if you’d like to write your own WebSocket library for Algol-60 just so you can interact with Slack, please do so, and then write a CodeProject article about it!
For most apps, the RTM API is a good choice, but the downside is that you’ll receive *all* messages and events that are posted to a channel you’ve subscribed to via the API. This can result in larger than necessary server and bandwidth bills if your app is listening to everything, but only cares about very specific events. If specific events are what you’re interested in, read on.
As its name implies, Slack’s Events API is what you’ll want to use if you’re interested in certain types of events that happen in a Slack channel. Slack helpfully provides a list of events you can subscribe to.
If it looks like all of the events you’ll be interested in are accessible via the Events API, then you can probably keep things simple and avoid a fancy-pants WebSocket based solution.
If the Events API doesn’t cover your needs, then buckle up - you’re going to need a strong pot of coffee and a YOLO attitude, because you’re going to be drinking from the Real Time Messaging firehose!
How to Get Started
Whichever Slack API you decide to use, there are a few common steps you’ll need to follow to get your app started.
The first thing you’ll want to do is create a new workspace to serve as a sandbox in which you can test your app. You can do this by navigating to https://slack.com/create and following the steps to create a new workspace. Slack will email you a confirmation code, ask you a few questions, and then create your workspace for you. After it is created, you’ll automatically be signed into the workspace and added to a channel.
If you’re an administrator, you could also skip this step and just use your employer’s Slack workspace. Depending on how awesome your employer is, doing this could either get you promoted or get you fired.
Now that you’ve created a workspace and signed into it, you’re ready to create your app. To do this, all you need to do is navigate to https://api.slack.com/apps/new. Slack will ask you to name your app. Feel free to use something like “My First Amazing Slack App.” Don’t worry! You’ll be able to change it later. You’ll also be asked to choose a workspace to create the app in. Click on the drop down box and choose one - it can be the test workspace you created in the previous step, or any other workspace you’re signed into.
When you choose a workspace, Slack will give you a frightening warning that if you leave the workspace you chose, you’ll lose your ability to administer you app. This is because all new Slack apps start their life attached to a single workspace.
Later on, you can free your app from its shackles and make it so that any Slack workspace can install it. For this to work, you’ll need to upgrade your app to support SSL and OAuth. This isn’t hard, but we’re not going to worry about this until later on.
And that’s it! We’ve learned about the three available Slack APIs, and created our first Slack app. Next, we’ll be writing some code to make our new app actually do something. I know it can be a bit disappointing to go through this whole article without getting to write any code. After all this is CodeProject, isn't it?
Fear not! We'll be coding soon. Sometimes it's important to learn the fundamentals of an API and lay the groundwork before writing any code, and that's exactly what we've done here.
To continue on and write some code, choose the next tutorial based on your preferred language:
Writing to a Slack Channel - Node.js Edition
Writing to a Slack Channel - .NET Core Edition
Writing to a Slack Channel - Python Edition