Start tracking your progress
Trailhead Home
Trailhead Home

Write Code for Your Server to Receive Events

In the following steps, you work with functioning code. Then you modify that same code, adding lines and whole code blocks in some instances. This is meant to demonstrate the various aspects of the Slack APIs. 

First, let’s set up your Slack App for success.

  1. Retrieve your signing secret.
    • Go to https://api.slack.com.
    • Click Your Apps in the top right of the screen.
    • Select the Welcome Trailhead bot, which should take you to the Basic Information page.
    • Scroll down to App Credentials.
    • Find the Signing Secret field near the bottom of the section and click Show.
    • Copy the signing secret.
  2. In your text editor, go to the file you made in the previous lesson, called .env.
  3. On a new line enter export SLACK_SIGNING_SECRET=<signingSecretYouCopied> replacing <signingSecretYouCopied> with the signing secret.
  4. Save the file.

Code the Bot

Now that you’ve got your tokens available, it’s time to write some code.

  1. Back in your text editor, click the File option in the application menu, select New File, then Save As index.js.
  2. In index.js, copy and paste the following.
require('dotenv').load();
  
// Initialize http
const http = require('http');
// Initialize the Slack Events API using signing secret from environment variables
const createEventAdapter = require('@slack/events-api').createEventAdapter;
const slackEvents = createEventAdapter(process.env.SLACK_SIGNING_SECRET);
const port = process.env.PORT || 3000;

// Initialize the Web API Client
const { WebClient } = require('@slack/client');
const token = process.env.SLACK_TOKEN;
const slackWeb = new WebClient(token);

// Setting Up Express to be used with the Events API
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

// Setting up this route to be used with the Slack Events middleware
app.use('/slack/events', slackEvents.expressMiddleware());
// Attach listeners to events by Slack Event "type".
slackEvents.on('team_join', (event) => {
  console.log(`Received a team_join event: user ${event.user.name} has joined.`);
});

// Handle errors (see `errorCodes` export)
slackEvents.on('error', console.error);

// Start a basic HTTP server
http.createServer(app).listen(port, () => {
  console.log(`server listening on port ${port}`);
});
  1. Save your file.

You just created a server to receive news about when a new member joins your team! 

Result in terminal when a user joins reads "Received a team_join event: user [userName] has joined."

Listen Test

Try it out.

  1. Go to terminal and type node index.js. This is what actually gets your server running.
  2. Then invite someone (or yourself with another email address) to your workspace.
  3. When they join, watch the console display "Received a team_join event: user [name] has joined."

Once you’re done admiring your glorious creation, enter control+c to stop your running Node server. 

We won't check any of your setup. Click Verify Step to go to the next step in the project.

retargeting