Creating Slack Slash Commands using Go

Emily Goldfein
Sep 1, 2018 · 4 min read

Slack slash commands: you know them, you love them (or maybe you only use the /giphy command — I’m not here to judge). And now, you want to learn how to make them.

In this tutorial, I’ll walk you through the process of writing your first Slack slash command in Go. Together, we’ll make a simple weather command that returns the weather conditions for a zip code.


First and foremost, we’ll have to do some setup before we can begin developing.

Get an Open Weather Map API Key

We’ll be using the Open Weather Map API to retrieve weather information. You’ll need to sign up on https://openweathermap.org/appid in order to get your own API Key.

Set up ngrok

You’ll also need to setup ngrok, which allows you to generate secure URLs for your localhost. This will allow us test our slash command locally in Slack. You can read more about using ngrok for local Slack development here.

Once you have ngrok installed, you can generate a public URL for your localhost by running the command ./ngrok http 8080.

Set up Slack slash command

If you don’t have your own Slack workspace to test in, you’ll first need to create that.

Next, go to https://api.slack.com/apps and click Create New App. Give your app a name, such as Weather, and select your development workspace.

Go to your apps main page and find Slash Commands in the sidebar. Click Create New Command.

Here, you’ll input the command, request URL (which will be your ngrok https URL — eventually we’ll add an endpoint, but for now we can just use the generated URL), a short description, and a usage hint.

Finally, you will need to install the app in your workspace.

Now that we’ve done our basic setup, we can move on to the fun stuff: the code!


Within your go directory, create a folder called slack-weather-app. To keep things simple for the purpose of this tutorial, we’ll put everything into main.go. Keep in mind that while I’m keeping this tutorial code pretty rudimentary, you would ideally want to structure your project according to best practices and include tests and proper error handling.

Set the Environment Variables

You will need to create an environment.env file to store your environment variables:

Create main()

We’ll be using the godotenv package to load our environment variables. Our main function will look as follows (Note: the full code for main.go will be available at the end of this post, including imports):

In it, we’ll load our environment variables, which will be our Slack verification token and our Open Weather Map API Key. Then, we’ll create a /receive endpoint that will initiate our slashCommandHandler function (Note: remember to update the request URL for your command in Slack to include the receive endpoint, e.g. https://12345.ngrok.io/recieve) Finally, we’ll start up the server on port 8080.

Create slashCommandHandler()

Next, we’ll get started on our slashCommandHandler function. We’re going to use the Go Slack package from github.com/nlopes/slack to access the Slack API. First, let’s just setup our slash command to make sure we get a response.

Our handler will parse the received slash command, validate our slack verification token, and then switch on the command string. We’ll respond to Slack with a simple message, “You asked for the weather for [zip code]”.

Now, we’re ready to test locally in Slack! With ngrok running in a separate terminal and your request URL updated in Slack, run go build and then ./slack-weather-app.

The command:

should return:

Retrieve Weather Data

So now we’ve got our Slack command responding. Our next step is to respond to the user with actual weather data.

We’ll first create a struct for the JSON response we’ll receive from the Open Weather Map API:

API Response struct within main.go

Next, we’ll add the API GET request with the parameters zip code, the API key, and units=imperial on the end to return the temperature in Fahrenheit. Since, the temperature is returned as a float64, I also rounded the temperature so it displays nicely.

Our full main.go file should look like this:

Let’s try running our app locally again. Now, when we request the weather for 90210:

We’ll get the current weather in Beverly Hills:

If you feel like getting fancy, you could even add emojis based on the weather code returned:

Summary

So, there you have it — your very own Slack slash command! I hope you enjoyed this tutorial, and as always, happy coding!

Emily Goldfein

Written by

Software Engineer | Lover of All Things Design

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade