How to make a Slackbot (2022 GUIDE)
Tags: beginners, tutorial, node, javascript
Published: 2022-06-16 11:55:00

In this article I have basically simplified the Getting Started Guide of Slack Bolt to something that I would had wanted to be avaible when I made my own bot. I have taken some code and inspiration from the Slack Bolt for javascript [getting started guide](https://slack.dev/bolt-js/tutorial/getting-started) and [documentation](https://slack.dev/bolt-js/concepts#basic).

REQUIRMENTS:
Computer & Internet Access
[Node ](https://nodejs.org/en/)installed
[Slack ](https://slack.com/)workspace & basic knowledge of slack
Basic programming knowledge
[Visual Studio Code](https://code.visualstudio.com/) (or other code editor, but VS Code will be used as example)
[Prettier ](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)(or something similar) installed to VS Code



Go to [api.slack.com](https://api.slack.com/) and click "CREATE AN APP"
![api.slack.com](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6htllicz2xejg6rbtair.PNG)



Choose the option to start "From scratch"
![click the "from scratch" button"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83ampq7of2tdsv7zum35.png)


Choose a name for your bot and what workspace to develop your bot in
![screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zwdejwrpxr07f15f7bt5.png)

Click socket mode from the left panel and enable it. (Name the token something that you will remember and then click Generate)

![Socket Mode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bedh2bonf17gwo1uzjg.png)
 

Click slash command and then choose "create new command"
![create new command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrenctatmacipoc793wv.png)
 
Give the command name that describes it's functionality (same applies to description and usage hint) for our purposes we are going to create /hello command that will make the bot say hello.
 
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iabodysnkeacqxy2y5ek.png)
 
Create new folder using:

```
 mkdir slack-app
```
Then do:
```
 cd slack-app
```
and then:
```
 npm init
```



Create index.js 
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5r4p8sbukuksbwcr02ts.png)


Install slack-bolt for javascript using: 

```
npm install @slack/bolt
```
 
After it has installed write the following into the index.js:

```
const { App } = require('@slack/bolt');

const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  socketMode: true,
  signingSecret: process.env.SLACK_SIGNING_SECRET
  appToken: process.env.SLACK_APP_TOKEN
});
```

After that put few empty lines and the write: 

```
app.command('/hello', async ({ command, ack, respond }) => {
    await ack();
  
    await respond("Hello, world!");
  });

```
And then few empty lines again.... ...and then write the following there:

```
(async () => {
  await app.start(process.env.PORT || 3000);
})();
```

Now we are done with almost all the coding. We will just have to create the .env file to store all of our Tokens and other secrets.


![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9ki80r96kydvirpf9mm.png)
 
Write the following to the .env file:

```
SLACK_BOT_TOKEN=
SLACK_SIGNING_SECRET=
SLACK_APP_TOKEN=
```

Then go back to the slack app "dashboard" and click basic information from the left panel.


![Basic Information](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a5vmj5mjofx0j3pstlyi.png)



Copy the signing secret and put it after the "SLACK_SIGNING_SECRET=" in the .env file. (Also remember to save the file using Ctrl + S from time to time.)

Next go to the OAuth and Permissions tab on the Slack dashboard (it son the left panel there). and click Install to Workspace.

![Install to workspace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ztjru1ikemp2a49hl38.png)

After you've installed it copy the OAuth token and paste it  after the "SLACK_BOT_TOKEN=" thing.
 
Go back to "basic information" tab and scroll down till you see the "App-Level-Token" and the token you created before. Click it (the blue part of it) and copy the token (by clicking "copy" button). The paste it after the "SLACK_APP_TOKEN=".

Then type the following to the terminal:

```
npm install dotenv --save
```

then add to the top of the code:

```
require('dotenv').config()
```

now you're code should look like this:

```
require('dotenv').config()
const { App } = require('@slack/bolt');

const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  socketMode: true,
  signingSecret: process.env.SLACK_SIGNING_SECRET,
  appToken: process.env.SLACK_APP_TOKEN
});


app.command('/hello', async ({ command, ack, respond }) => {
    await ack();
  
    await respond("Hello, world!");
  });


(async () => {
  await app.start(process.env.PORT || 3000);
})();
```

then type `node index.js`

now go to the workspace you installed the app to.


Thank you for reading this.

Resources I used:
[Slack Bolt for Javascript Documentation](https://slack.dev/bolt-js/tutorial/getting-started)
[Slack API Documentation](https://api.slack.com/#read_the_docs)


![END RESULT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txq0t4x7487wlyi0gkcp.png)