How to make a Slackbot (2022 GUIDE)
Onni3000
Posted on June 16, 2022
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"
Choose the option to start "From scratch"
Choose a name for your bot and what workspace to develop your bot in
Click socket mode from the left panel and enable it. (Name the token something that you will remember and then click Generate)
Click slash command and then choose "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.
Create new folder using:
mkdir slack-app
Then do:
cd slack-app
and then:
npm init
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.
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.
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.
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
Slack API Documentation
Posted on June 16, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.