Building your first slack bot using Nodejs and Bolt SDK

Durgesh Mahajan
syngenta-digitalblog
6 min readAug 16, 2024

In this blog post, we’ll explore how to build a simple Slack bot using Nodejs and the Bolt SDK. Slack bots are a great way to automate tasks, provide information, and enhance collaboration within your team or organization. The Bolt SDK, developed by Slack, makes it easy to create and deploy Slack apps and bots.

Slack bots are essentially software applications that integrate with the Slack messaging platform. They can be programmed to perform various tasks, automate workflows, and provide useful information or services within Slack channels or direct messages. Slack bots can significantly streamline and simplify many routine processes, making teams more productive and efficient.

This article provides an insight into the development of the slack bot developed and used in our organization named “DevOps&SRE” which simplifies various tasks like Master access request, SSO config generation, with just a single command. You may need to refer to this article to understand some things in the next articles which explain how these features were implemented in the bot.

What will you need -

Before we dive into the implementation, make sure you have the following prerequisites:

  • Nodejs installed on your machine (Download here)
  • A Slack workspace (you can create a new one or use an existing one. Refer here)
  • A Slack app created in your workspace (Refer below)

Create a slack app -

Go to https://api.slack.com/apps. Make sure you are signed in to slack. Click on the ‘Create New App’ button.

Choose the ‘From scratch’ option.

Enter app name and choose workspace created by you to install your app in. Click on ‘Create App’.

Kudos!!! You have created your first slack app!

Setting the slack app for development -

Click on this link: https://api.slack.com/apps, and choose the app that you created just now. Here, you will see all the info and configs for your slack app. You can configure anything related to your slack app on this page. Take some time to familiarize yourself with this portal to understand how to configure your slack apps better.

Now, for development, we will need some credentials like App and Bot tokens to connect our app to our server code. Scroll down to the ‘App Credentials’ section and copy the Signing Secret. Store it safely in a .env file in the root directory of the project.

Now, just below that in ‘App-Level Tokens’, Click on ‘Generate Token and Scopes’ button. Enter any token name and click on the ‘Add Scope’ button. Select connections:write from the list. Then click on ‘Generate’. After this, you will get the token in another dialog box. This is your bot’s App Token. Copy it and store it in the .env file.

Now, in the left menu, in the Features section, click on ‘OAuth & Permissions.’ Scroll down to the ‘Bot Token Scopes’ in the ‘Scopes’ Section. Click on the ‘Add an OAuth Scope’ button. Here, you can add permissions for your slack bot. Read the descriptions of these permissions carefully to understand what these scopes allow the slack bot to do. For now, select chat:write , channels:read , groups:read and groups:write . Now, scroll up in the ‘OAuth Tokens for Your Workspace’ section. Click on the ‘Install to Workspace’ button. It will ask for permission, click ‘Allow’. Then, in the same section, you will see a Bot Token generated in the ‘Bot User OAuth Token’ box. Copy it and store it in the .env file.

Finally, your .env file should have all of these 3 credentials till now:

SIGNING_SECRET = "<YOUR_SIGNING_SECRET>"
BOT_TOKEN = "xoxb-*******..."
APP_TOKEN = "xapp-*******..."

One last thing, we will be building this bot in socket mode which allows a better communication between the app and the bot’s server. Also, this eliminates the need to define and use http endpoints for any actions, events or triggers. Hence, to enable that, go to the Settings section in the left panel and click on ‘Socket Mode’. Enable it through the ‘Enable Socket Mode’ toggle button. That’s it!!

Great!!! You have successfully set up slack app for development!

Creating the slack bot’s server -

  1. Set up a project folder and open it in any editor like VSCode.
  2. As mentioned earlier, we will be making this bot in Javascript and Nodejs. So make sure that Nodejs is installed in your system. Now, in the project’s root directory, open the terminal in your editor and type npm init -y to initialize your node project. Once done, install some libraries using this command: npm install @slack/bolt dotenv nodemon
  3. Now, create a new file named index.js in your project’s root directory.
  4. Add the below code in that file:
const { App } = require("@slack/bolt");
const dotenv = require("dotenv");

dotenv.config();

const app = new App({
token: process.env.BOT_TOKEN,
signingSecret: process.env.SIGNING_SECRET,
socketMode: true,
appToken: process.env.APP_TOKEN
});

(async () => {
await app.start(3001); // Using port 3001 here, you can use any other one
console.log('⚡️ Bolt app is running!');
})();

5. Start the server using the nodemon index.js command. You should see the below output in the terminal:

Amazing!!! You have successfully built the bot’s server!

Now, to add interactivity through commands, modals, actions, app mentions, etc. you can modify the code. Refer this page for more info on interactivity: https://api.slack.com/interactivity. Here, let’s see how to add interactivity through commands.

For commands, you will need to configure the app in the app’s portal from where we extracted the app’s credentials like signing key, etc. Scroll up to find the link. Click on ‘Slash Commands’ in the Features section. Click on the ‘Create New Command’ button. Here, define your command, and once it is done, click on ‘Save.’

It will ask for reinstalling your app in the workspace. Do it by clicking on the link in the message displayed on top of the window.

Now go to your code editor and modify the code like this:

...

(async () => {
await app.start(3001); // Using port 3001 here, you can use any other one
console.log("⚡️ Bolt app is running!");

// Add this
app.command("/greet", async ({ command, client, ack, say, respond }) => {
await ack();
await respond(`Hello ${command.user_name.split(".")[0]}`);
})
})();

Finally, go to your slack application or web version and enter the slash command in any channel or in your own DM. You will get a response as shown below:

That’s it!!! You have successfully built a complete slack bot workflow with commands interactivity!

Now, this is how you will be able to build and set up slack bots. You can add many features like automating and simplifying your tasks, creating task reminders, creating alert bots, etc. by improving logic on the server. Make sure to explore more about slack API’s documentation (https://api.slack.com/docs) to get an overview of how to build more interactivity using slack bots!

--

--

Durgesh Mahajan
syngenta-digitalblog

👨‍💻 DevOps and SRE Engineer at Syngenta 🚀 Want to be the best of what I can