Slack is a popular messaging app for team communication. A nice thing about Slack is that you can easily integrate external services to provide extra features. For example, out-of-the-box integrations are available for services like GitHub, Google Drive, Heroku, Jira, and many others. In this example, we'll show how easy it is to integrate a swagger-node API with Slack.
Download or clone the swagger-node-slack project on GitHub.
After downloading the app, do the following:
-
cd to the root project directory
swagger-node-slack
. -
Execute this command to get the Node.js dependencies:
npm install
-
Execute this command to install
swagger-node
on your system. This step installsswagger-node
and puts theswagger
CLI in your path:npm install -g swagger
In this example, a swagger-node API implementation provides the back-end "services" that we will integrate into Slack. We created two sample API implementations for you to try out with Slack:
-
An API that reverses whatever text you enter in a Slack conversation.
-
An API that fetches a stock quote and prints it to a Slack team conversation.
In this tutorial, we'll show you how to set up Slack integrations that call these swagger-node
APIs.
If you're going to try to do this tutorial, you'll need to do these steps first:
-
You need to either be a member of or create a new Slack team. In either case, you need to have permission to create integrations.
-
Your app has to be reachable by Slack via HTTP, and it must be deployed to a platform that supports Node.js. We're going to deploy it to Apigee Edge Cloud. To do that, you'll need to sign up for an Apigee Account. If you don't want to do that, you can deploy the app to any other Cloud platform that supports Node.js, like Heroku or AWS. However, we won't cover any other deployment options besides Apigee Edge in this topic.
Here's a brief overview of the integrations we'll build here.
Slack "slash commands" let you execute a function by entering it directly in a Slack conversation. Here's how the Text Reverser integration we'll build here works. You'll enter it like this in Slack:
/reverse Hello World
And get a reply with the characters reversed:
Incoming WebHook integrations let you post data from an external source/service into Slack. For this example, we'll call a swagger-node
API using a curl command. This API will then post a reply directly to a Slack channel. We'll call it like this...
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" http://docs-test.apigee.net/slash/ticker -d "text=AAPL
...and get back a nicely formatted response in Slack, like this:
Let's walk through the steps for integrating the "text reverser" API with Slack. We're not going to go overboard to explain how to set things up in Slack, but we'll give pointers to keep you on track. It's remarkably easy.
Take a look at the swagger-node-slack
project. It's a swagger-node
project created with the swagger project create
commmand. If you're not familiar with swagger-node
, you can check out the docs, and try the quick-start tutorial if you like.
The key to understanding how the swagger-node-slack
API works is to look at these two files:
-
./swagger-node-slack/api/swagger/swagger.yaml
-- This is the Swagger definition for the API. Note that it defines the paths, operations, and parameters for the service. You can use the built in Swagger editor to make changes withswagger project edit
.paths: /reverse: # binds a127 app logic to a route x-swagger-router-controller: reverse post: description: take text and reverses it # used as the method name of the controller operationId: reverse consumes: + application/x-www-form-urlencoded parameters: + $ref: "#/parameters/token" + $ref: "#/parameters/command" + $ref: "#/parameters/text" + $ref: "#/parameters/team_id" + $ref: "#/parameters/team_domain" + $ref: "#/parameters/channel_id" + $ref: "#/parameters/channel_name" + $ref: "#/parameters/user_id" + $ref: "#/parameters/user_name"
-
./swagger-node-slack/api/controllers/*.js
-- In this example project, there are two controller files, one for the/reverse
path and one for the/ticker
path. As you can see, these files implement the actual operation logic for each path. Here's thereverse()
function that gets executed when the/reverse
path is called.function reverse(req, res) { checkToken(req, res, function(err, reply) { if (err) { return res.json(err); } else if (req.swagger.params.text.value === "") { res.json("Please enter some text to reverse!"); } else { console.log(reply); var gnirts = req.swagger.params.text.value.split('').reverse().join(''); res.json(gnirts); } }); }
You can run the swagger-node-slack
project locally, and hit the API just to see what it does.
-
cd to the
swagger-node-slack
directory on your system. -
If you haven't done so previously, execute this command to update the Node.js dependencies:
npm install
-
Start the project:
swagger project start
-
Open the file
./swagger-node-slack/api/controllers/reverse.js
in a text editor. -
Note that the
token
var is set tosecret123
. The API requires atoken
parameter that matches this value, as we'll see. Later, we'll replace this value with a token issued by Slack.var token = "secret123"
-
In a separate terminal window, call the API as shown below with the
token
andtext
parameters.curl -X POST -H "Content-Type: application/x-www-form-urlencoded" http://localhost:10010/reverse -d "token=secret123&text=hello"
The API reverses the string specified by the
text
parameter and returns:""olleh""
Note that the API also validates the token. If you call the API with a different token, you'll get an error. For example:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" http://localhost:10010/reverse -d token=secret234&text=hello"
"Error: Sorry, the token key is invalid."
Now, let's go over to the Slack side.
-
Log in to your Slack account.
-
From your Slack team menu, choose Configure Integrations.
-
Scroll down to DYI Integrations & Customizations and click Slash Commands.
-
In Choose Commands, enter the command name
/reverse
. -
Click Add Slash Command Configuration.
-
Fill out the integration settings:
a. Command:
/reverse
.b. URL: http://{your apigee org name}-{the apigee environment name}.apigee.net/slack/reverse
For example: http://docs-test.apigee.net/slack/reverse
c. Method: POST
d. Token: Copy the token; you'll need it shortly.
-
Click Save Integration. You'll see your Slash Command integration listed in the Configured Integrations tab, as shown in this screen shot:
Now, we're going to use the token that Slack issued when we created the Slash command.
Note: When you enter the
/reverse
slash command in a Slack conversation, Slack sends a POST request to theswagger-node
app with a bunch of parameters. One of them is the token. Code in./swagger-node-slack/api/controllers/reverse.js
validates this token, ensuring that the command was sent from your Slack team.
-
Copy the token from the Slash command UI in Slack.
-
Open the file
./swagger-node-slack/api/controllers/reverse.js
in a text editor. -
Enter the token string from Slack as the value of
var token
. For example:var token = "6dIWuDTuheXsVVJcjDVVdA"
-
Save the file.
For this example, we'll deploy the app to the Apigee Edge cloud platform. As mentioned previously, you'll need an Apigee account (it's free) to do this. (However, you can publish it to any platform that supports Node.js if you wish.)
-
Install apigeetool. This utility lets you deploy Node.js apps to Apigee Edge.
npm install -g apigeetool
-
Make sure
apigeetool
is in your PATH. Just enterapigeetool
at the command line. If you see a list of valid commands, you're good to go.apigeetool
-
Be sure you're in the root directory of the
swagger-node
project:cd swagger-node-slack
-
Use
apigeetool
to deploy the app:apigeetool deploynodeapp -u <your Apigee account email address> -o <your Apigee organization name> -e <An environment in your org> -n slash -d . -m app.js -b /slash -v default,secure
For example:
apigeetool deploynodeapp -u [email protected] -o jdoe -e test -n 'slash' -d . -m app.js -b /slash
For help on this command, enter
apigeetool deploynodeapp -h
.
Now we can hit our API directly from a Slack! In your Slack team channel, enter the /reverse
command with some text to reverse:
/reverse The quick brown fox jumps over the lazy dog
And Slack returns the letters in reverse:
The Slack Slash Command Integration called the swagger-node-slack
app that was deployed on Apigee Edge. Slack retrieved the response and printed it to the chat window.
The Ticker-bot is an API implemented in swagger-node
and added to Slack as an Incoming WebHook integration. This type of integration is designed to fetch some external data and display it in Slack. In this case, we implemented a swagger-node
API that takes a stock symbol and sends the stock price to Slack.
Let's go over to the Slack side first.
-
Log in to your Slack account.
-
From your Slack team menu, choose Configure Integrations.
-
Scroll down to DYI Integrations & Customizations and click Incoming WebHooks.
-
In Post to Channel, select the channel to post the API response to. In other words, whenever someone calls the Ticker-bot, a stock price is posted to this channel for everyone to see.
-
Click Add Incoming WebHooks Integration.
-
Review the setup instructions.
-
Copy the Webhook URL.
-
In the name field, change the default name to "Ticker-bot".
-
Add an icon or emoji if you wish.
-
Open the file
swagger-node-slack/api/controllers/ticker.js
in a text editor. -
Locate this variable and uncomment it:
var URL = "https://hooks.slack.com/services/https://hooks.slack.com/services/GET/SLACK URL";
-
Replace the value of the URL variable with the Webhook URL. For example:
var URL = "https://hooks.slack.com/services/https://hooks.slack.com/services/X012434/BT3899/PSbPEfQybmoyqXM10ckdQoa";
-
Save the file.
A nice thing about projects built with swagger-node
is that you can build and test them locally on the built-in HTTP server. Let's try out our new Ticker-bot!
Remember, with an Incoming WebHooks integration, the idea is to send a message FROM another service INTO a Slack channel.
-
cd to the
swagger-node-slack
directory. -
If you haven't done so previously, execute this command to update the Node.js dependencies:
npm install
-
Start the project:
swagger project start
-
In another terminal window, call the API, like this...
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" http://localhost:10010/ticker -d "text=AAPL&user_name=marsh"
...and you get back a nicely formatted response in your Slack session, like this:
-
Deploy the Ticker-bot app to the Cloud and test it.
-
Try creating a Slack Slash command
/ticker
that calls the Ticker-bot API. The command will let users enter something like this in Slack to post a stock price:/ticker AAPL
Be sure to add token validation to the API's controller, as implemented in the
/reverse
example.