Create a Slack Bot with NodeJS
Tags: node, javascript, webdev, tutorial
Published: 2021-10-21 13:37:39

Written by Lior Ben David.

---

###Intro

Platforms like Slack, Teams, and Discord have become much more than just a communication mechanism. In the world of remote work, these platforms become the very venue through which our teams operate.

In this tutorial, we're going to be using NodeJS to create a Slack Bot which you can customize to handle HR operations, pull data, or just create a more enjoyable slack experience for your team.

If you're interested in learning how to create bots on other communication platforms, let us know down below.

---

###Project Setup

Our project file system is going to be relatively simple. We're just going to need a standard node project which we can initialize with:

`npm init`

Let's then add Slack's Bolt API for creating Slack Apps:

`npm i @slack/bolt`

Finally, create a file called `index.js`

Next, we need to actually register our app with Slack. To do this, visit https://api.slack.com/apps/

And create an App from scratch.

![nameapp](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13az0wabga1098xgep6t.png)

Name your app and pick the workspace you want to test it in.
From there, there are a number of settings that we are going to edit.

First, in Basic information, go to App-Level Tokens and generate a new Token. Give this token the scopes "connections: write" and "authorizations: read"

![generateapptoken](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xfakz9syso9mpdq8hax.png)

Next, go to Socket Mode and enable Socket Mode. This will allow us to connect to the Slack API with WebSockets. If you are publicly distributing your Slack App you are going to want to host it somewhere. More on that later.

Next, go to OAuth & Permissions, and find the Scopes section. Add the following Bot Token Scopes. Depending on what you are doing you may want to add others, but for this tutorial, this is all we will need.

![scopes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ng1kwiylcbvbf11wyrve.png)


That's all we need for configuration. Finally, we can initialize our app in `index.js`:

{% gist https://gist.github.com/LiorB-D/460620b1a0c668f20a918defe4ea2ba7 %}

If we run this file and don't get any errors, then we've successfully authenticated our app!

---

###Slash Commands

The first way that we can add functionality to our Slack bot is by creating Slash commands. These are exactly what you might imagine. Someone in our workspace can simply type /command-name and call some sort of function.

To add a command, go to the Slash Commands tab in the Slack API dashboard and press "Create New Command"

![socketmode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4vty9ecgj5yivi50s9h.png)

We can then add functionality to this command in `index.js`:

{% gist https://gist.github.com/LiorB-D/1440f3039c8f2a5ca4cf25b91b5943a2 %}

Once you reinstall the app to your workspace (which can be done from the oAuth & Permissions Tab), you can use it like so:

![squaregif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ay6ve5ut6k5pbf4jabqp.gif)

---

###Handling Events

The next way we can add functionality to our Slack Bot is through events. An event can be anything from direct messaging our bot or a new user joining our workspace. We can set up our bot to listen to these events and do certain things when the event is triggered.

To set this up, go to the Events and Subscriptions tab and enable Events. From there, add

![subscribetoBots](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4ac5c8itwhf16ng4s97.png)

From there, we can add the following code to our `index.js`


{% gist https://gist.github.com/LiorB-D/5b24b9501ac2cea055c1bfe8329249d5 %}


And use it like so (again don't forget to reinstall the app to your workspace)


![helloGif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikqo4re6qp3ryqzk28ed.gif)

---

###Using Slack Apps to Optimize Your Business Operations

Now obviously the examples we built here are pretty rudimentary, but the key thing to note here is that you can now interact with Slack however you want. This can allow you to build any number of tools such as:

* Commands to pull certain metrics (just pull from API's in NodeJS)
* Run internal polls
* Find meeting availability
* Get progress reports (perhaps even integrate with Trello or Asana)

Now that the code is at your fingertips, anything you can do with NodeJS you can now do in Slack.

---

###Publishing Your App

If you want to publish your bot so that anyone can integrate it into their workspace, you're going to need to deploy it somewhere. If you've ever built a bot that requires live deployment, you know how difficult and time-consuming it can be to test your app in the cloud when you can only develop it locally.

**At [Codesphere](https://link.codesphere.com/ES), we're building a cloud IDE so that you can code and deploy seamlessly.** With Codesphere's live test environment, you can make and test ad-hoc changes to your Slack bot instead of wasting time deploying after each minor change you make.

So what kind of bot are you going to build? Let us know down below!

Happy coding!