Start tracking your progress
Trailhead Home
Trailhead Home

Send Messages and Store Information

So far, you’ve learned how to set up a Slack App on the api.slack.com website. You’ve also built a basic bot that listens for when a new user joins your workspace. Now it’s time to level up! 

This step shows you how to store data relating to whether or not users have accepted the terms in your welcome message in a file on your machine called database.json

Note

Note

Using a file like this is for instructional purposes only. In practice, developers typically use an actual database to store this information.

Send a Message to Your New User

Now, we’ll write code to send a message to the user!

  1. Go back to your text editor and click the index.js file.
  2. Find the line const http = require('http'); (line 3). Press return to add a new line after it.
  3. Copy and paste this block of code on that new line.
const message = {
  "text": "Welcome to the team! We're glad you're here.",
  "attachments": [
    {
      "title": "What is Slack?",
      "text": "Slack is where work happens. Welcome aboard, brave adventurer, to the world of Slack!",
      "color": "#3AA3E3",
      "attachment_type": "default"
    },
    {
      "title": "Code of Conduct",
      "text": " Here at this company, we treat each other with respect and empathy.",
      "callback_id": "accept_terms",
      "color": "#3f1daf",
      "actions": [
        {
          "name": "accept",
          "text": "I Accept",
          "style": "primary",
          "type": "button",
          "value": "accept"
        }
      ]
    }
  ]
};
  1. Save your work.

Believe it or not, you’ve just created a message with two Slack attachments and a button!

Let’s break it down:

  • The first attachment offers text that says, “Welcome to the team! We’re glad you’re here.”
  • The second one has the Code of Conduct to accept with a message that says, “Here at this company, we treat each other with respect and empathy.”
  • Then, there is a button for the user to accept the Code of Conduct.

Good job, Welcome Trailhead can now greet new users!
With the newly added code, Welcome Trailhead can now post the welcome message, introduce Slack, and offer the Code of Conduct to accept.

  1. Now, locate the code that attaches listeners to events. This should start on line 44 in your editor with // Attach listeners to events by Slack Event “type”. It’s time to give this an upgrade. Copy the below code and replace the entire block under that comment (lines 45-47).
slackEvents.on('team_join', (event) => {
  slackWeb.chat.postMessage({
    channel: event.user.id,
    text: message.text,
    attachments: message.attachments
  }).then((res) => {
    console.log(`Message sent: ${res.ts}`);
  }).catch(console.error);
});
  1. Save your code.

You’re about halfway finished. Let’s keep going!

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

retargeting