How to make a Slackbot (2022 GUIDE)

onni3000

Onni3000

Posted on June 16, 2022

How to make a Slackbot (2022 GUIDE)

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 and documentation.

REQUIRMENTS:
Computer & Internet Access
Node installed
Slack workspace & basic knowledge of slack
Basic programming knowledge
Visual Studio Code (or other code editor, but VS Code will be used as example)
Prettier (or something similar) installed to VS Code

Go to api.slack.com and click "CREATE AN APP"
api.slack.com

Choose the option to start "From scratch"
click the

Choose a name for your bot and what workspace to develop your bot in
screenshot

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

Socket Mode

Click slash command and then choose "create new command"
create new command

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

Create new folder using:

 mkdir slack-app
Enter fullscreen mode Exit fullscreen mode

Then do:

 cd slack-app
Enter fullscreen mode Exit fullscreen mode

and then:

 npm init
Enter fullscreen mode Exit fullscreen mode

Create index.js
Image

Install slack-bolt for javascript using:

npm install @slack/bolt
Enter fullscreen mode Exit fullscreen mode

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
});
Enter fullscreen mode Exit fullscreen mode

After that put few empty lines and the write:

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

    await respond("Hello, world!");
  });

Enter fullscreen mode Exit fullscreen mode

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

(async () => {
  await app.start(process.env.PORT || 3000);
})();
Enter fullscreen mode Exit fullscreen mode

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

Write the following to the .env file:

SLACK_BOT_TOKEN=
SLACK_SIGNING_SECRET=
SLACK_APP_TOKEN=
Enter fullscreen mode Exit fullscreen mode

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

Basic Information

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

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
Enter fullscreen mode Exit fullscreen mode

then add to the top of the code:

require('dotenv').config()
Enter fullscreen mode Exit fullscreen mode

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);
})();
Enter fullscreen mode Exit fullscreen mode

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
Slack API Documentation

END RESULT

💖 💪 🙅 🚩
onni3000
Onni3000

Posted on June 16, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related