Note: This documentation is for version 1 of the Now platform. For the latest features, please see the version 2 documentation. If you have yet to upgrade, see the upgrade guide.

Building a Slash Command for Slack

For many people in the web community, Slack has become an essential part of their everyday workflow. It has turned into a wonderful tool for sharing text, working collaboratively, and building community.

To support this wide range of use cases, and to empower those who use the service, this tutorial will show you how to build a slash command for Slack with Now.

In this guide, you'll learn how to retrieve weather information for a specific location by running a command, /weather, inside Slack. This creates a nice base for building a more complex command of your choice.

Prerequisites

Slack account

First, ensure that you have a team account on Slack's platform. If you don't have one create an account here.

Node.js

Next, make sure that your instance of Node is up-to-date. You should have the latest "Current" release (compare it with this). If not, click the download button on the site to upgrade to the newest version.

You can use this command to check the version tag of your local instance:

node -v

Once the output of the command matches the latest version on the site, go on to the next section.

Step 1: Setup

Creating a new directory

Now that Slack knows where to send requests to when the command is run, we can start building the service that responds to these requests. Let's start by creating and moving into a new empty directory:

mkdir slash-command && cd slash-command

Next, create the project's package.json file in that directory:

{
  "version": 1,
  "name": "slash-command",
  "scripts": {
    "start": "micro index.js"
  }
}

The code above tells Now the name of the project ("slack-cmd") and also to execute the index.js file (using micro, which we'll install in the next step) when the npm start command is run in your terminal or on the server.

Executing index.js with micro

We'll need to install micro, a Zeit library for easily building a microservice. In addition, we'll need the yahoo-weather package, which will provide us with weather data.

Run this command in your terminal to install both using npm:

npm install --save micro yahoo-weather

Once that's done, create the index.js file and populate it with code. Load yahoo-weather and URL (a native module for parsing URLs) as follows:

const weather = require('yahoo-weather')
const url = require('url')

Next, we need to export the code which specifies the output rendered when the service is accessed by Slack. In this case, it's a function that retrieves the current weather and returns a message containing the condition in degrees Celsius:

module.exports = async request => {
  const query = url.parse(request.url, true).query

  if (!query.text) {
    return 'No location defined!'
  }

  const weatherInfo = await weather(query.text.toLowerCase())
  const temperature = weatherInfo.item.condition.temp

  return 'It is ' + temperature + ' degrees Celsius in ' + query.text + ' right now!'
}

Step 2: Deploying

Now that we've covered the project's files, you can deploy it by running this command:

now

Once Now has finished uploading the files, you'll see a URL that points to your freshly-created slash command service. We're now ready to tell the Slack platform about the service so that it can send requests there.

Note: In the case of a real service (not used for testing purposes), you would now have to assign an alias to it.

Step 3: Registering the Command

Telling Slack about the command is pretty easy.

First, search the Slack App Directory for the "Slash Commands" application. Once you've found it, click on "Add Configuration" in the sidebar:

You'll be redirected to another page where you can enter the name of the new slash command. In our case, it will be /weather. Enter it and click on the green button at the bottom.

Now you should see a section named Integration Settings. There, we need to do the following:

  1. Add the address of our deployment to the URL field
  2. Select GET as the Method and
  3. Enter "Weather" in the Customize Name input.

Optionally, you can also make the command show up in the Autocomplete help text section:

And hit "Save Integration" once you're done!

You should now be able to run the /weather command from within Slack.