How to build a Slack bot with Node.Js in 2020

3 min read -

Everyday I spend 30m checking stats from this blog, that’s 180 hours a year !

These stats are useful to know if my articles are relevant (if you quit now I’ll know it and be very sad… 👉👈).
But getting these stats is too long, I have to use Google Analytics, Stripe, my database, etc...
The good news is that I know how to get this data automatically, and I’ll teach you how, with a Slack Bot 🤖.

More precisely, we’re going to build a bot that tells me the money on my Stripe account. This money comes from the donations you make to me through Buy Me A Coffee at the bottom right of the page. We will use Node.JS, Slack API and Stripe API !

You can get the final code on GitHub or you can copy my Glitch project.

Here are the steps:

  • Creating a Slack Application
  • Coding a Slack Bot using Bolt.Js
  • Registering to Slack Events
  • Admiring the result !

Creating a Slack Application

To build a Slack Bot, we’ll need to create a Slack App. We have to choose a name and the workspace to install it: Create a Slack App then we add permissions so that Slack knows what it has to allows us to do

  • app_mentions:read: allow to read messages that mention the app.
  • chat:write: allow to write messages.
  • im:history: allow to read direct messages to the app.

The full list of permissions is available in the scope section. Slack App permissions Now we can install the app to our Slack Workspace ! Install a Slack App to a Workspace As a bonus, we can customize the app's name, description, color and icon: Customize a Slack App


Coding a Slack Bot using Bolt.Js

Now that we've created our bot, we can code its behaviors. We’re going to use Bolt.JS (the official framework to build Slack Apps) and Node.Js. To begin with, clone my project from Glitch. Code a Slack Bot with Bolt.Js Glitch allows us to code our bot and make its code live. That’ll be useful to test the bot without deploying it on a server (you can also use Ngrok). To make this code work, we need our Slack API keys.

We have SLACK_SIGNING_SECRET in the Basic Information section: Slack signing secret And SLACK_BOT_TOKEN in Install App: Slack bot token We also need the Stripe Secret API key, which you can find in your Stripe Dashboard: Stripe secret API key We can now enter your API Keys in the .env file: Glitch env file


Registering to Slack Events

The code in index.js has two parts:

  • Configure and launch the bot
  • Listen and respond to events

Bot configuration is done with the following:

const { App } = require("@slack/bolt");
require("dotenv").config(); // Get the API keys from .env file

const app = new App({
  signingSecret: process.env.SLACK_SIGNING_SECRET,
  token: process.env.SLACK_BOT_TOKEN
}); // Configure the Slack App from our API keys

(async () => {
  await app.start(process.env.PORT || 3000); // Launch the bot
  console.log("⚡️ Bolt app is running!");
})();

The following code will listen to messages containing ”Am I rich yet?” and respond in the same channel with the total amount of dollars in the Stripe Balance:

const stripe = require("stripe")(process.env.STRIPE_SECRET);

app.message("Am I rich yet?", async ({ message, say }) => {
  const { available } = await stripe.balance.retrieve();
  say(`Here's your 💸\n${available[0].amount.toFixed(2)}`);
});

If we try our bot now it’ll not work: We didn’t say to Slack which events we want to listen ! We can do it in the Event Subscriptions section: Add request URL to Slack app We need to specify in the Request URL the HTTP address of our bot, which is GLITCH_URL/slack/events.
In my case: https://foul-cotton-medicine.glitch.me/slack/events.

Now, we can subscribe to 2 events:

  • message.im: We listen to messages posted to direct message channels
  • app_mention: We ONLY listen to messages that mention our bot.

Our bot is basically saying to Slack: “tell me when someone is sending me a direct message, but no other message”.

Subscribe to Slack events Now, let’s go to our workspace: Slack bot responding to my message

woohoo! I’m poor!

Conclusion

Today, you built a Slack Bot that saved you minutes everyday ! Congrats ! 🎉

But it’s just the beginning. We only saw a bit of what you can do with a Slack Bot. Feel free to check the Bolt.Js documentation to see the possibilities !

You’d find it useful to deploy your bot on Heroku. Thankfully it’s as easy as it sounds, just don’t forget to update the Request URL of your event subscriptions.

Please let me know if you found this article useful and show me what you built on Twitter !