Building a Slack bot with Nodejs 
Tags: node, bot, slack
Published: 2020-04-19 19:27:07

Building  a Slack bot is actually easy and straightforward, unfortunately, a lot of tutorials covering this topic are fairly outdated now, in this article, I would walk you through on how to build a simple one with Nodejs and the [Bolt library](https://github.com/slackapi/bolt)

- First, you would need to create a slack workspace [here](https://slack.com/intl/en-ng/create#teamname)
after creating a team
![create team](https://res.cloudinary.com/raphaelnoriode/image/upload/v1587317035/tutorials/1createbot_psl1ek.png)

- head over to [api.slack.com/apps](https://api.slack.com/apps) to create a new bot app. 
![create app](https://res.cloudinary.com/raphaelnoriode/image/upload/v1587317036/tutorials/createapp2_gbb3ij.png)

Click on `Create app` button, type in a name and select a workspace, ideally the one you initially created, then create the app.

![Setup bot](https://res.cloudinary.com/raphaelnoriode/image/upload/v1587317036/tutorials/configurebot_fkhkcb.png)

From this window, you get to set up what the app needs, for this app, we only care about "Event subscription", "Bots" and "Permissions".


### Event Subscription
Click on `Event subscription` button and turn it on 
![Event setup](https://res.cloudinary.com/raphaelnoriode/image/upload/v1587317688/tutorials/eventsub_wsv1dw.png)

Before we can set up the event, we need to get our request URL, this is easy using the bolt library.

#### First install bot to your workspace 
- Click on `Oauth and permissions`
- Click on `Install App to Workspace` button

Ideally, you should select the bot scopes before adding the app to your Workspace, for every added scope you would have to reinstall the app to the workspace.
For the app, we used the `app:mention` bot scope, basically, we want to listen for when the app is mentioned. 

N:B The difference between the bot scope and the user scope is that the user scope allows the bot to act on behalf of a slack user when given the permission to.  

Now you should see your `Bot User OAuth Access Token` this would be needed to configure the app.

The needed code to generate the request URL goes below; 

- create app folder
- create index.js file
- `npm install @slack/bolt dotenv`

copy the below code into the index.js file 
```
require('dotenv').config();
const { App } = require('@slack/bolt');

const bot = new App({
  signingSecret: process.env.SLACK_SIGNING_SECRET,
  token: process.env.SLACK_BOT_TOKEN,
});

(async () => {
  // Start the app
  await bot.start(process.env.PORT || 3000);

  console.log('⚡️ Bolt app is running!');
})();
```
check out how to use `dotenv` if you don't know already, but you want to use it to hide these credentials.
Get the `Signing Secret` from the "Basic Information" page

Run the program and route localhost to a live server like ngrok, now copy the link and paste as shown below;

![request URL](https://res.cloudinary.com/raphaelnoriode/image/upload/v1587320202/tutorials/ngrok_qge7zi.png)

the bolt library listens on the slack/events endpoint, hence why you should add it to your URL, if it is verified as seen above you are good to go. 
The Basic information page should look like this
![basic one](https://res.cloudinary.com/raphaelnoriode/image/upload/v1587320742/tutorials/basic_kouw1p.png)
Features and functionalities
![Basic2](https://res.cloudinary.com/raphaelnoriode/image/upload/v1587320744/tutorials/basic2_xdtchh.png)

We would not be covering distribution in this article, I'll link another article for that. 

Now in your slack workspace, the bot should show up under Apps
![see apps](https://res.cloudinary.com/raphaelnoriode/image/upload/v1587321309/tutorials/appshow_fkhkb5.png)

But no action is performed by the bot yet 
![no action](https://res.cloudinary.com/raphaelnoriode/image/upload/v1587321309/tutorials/appshow_fkhkb5.png)

N:B we would need to respond to mentions, so go to the `OAuth and Permissions` tab add `chat:write` to the bot scope.

Add this code to your index.js file just after declaring the initializing APP 

```
bot.event("app_mention", async ({ context, event }) => {
  
  try{
    await bot.client.chat.postMessage({
    token: context.botToken,
    channel: event.channel,
    text: `Hey yoo <@${event.user}> you mentioned me`
  });
  }
  catch (e) {
    console.log(`error responding ${e}`);
  }
  
});
```
[see here](https://api.slack.com/events/app_mention) to read more on the app mention API

- Add app to a channel and mention the app
![see response](https://res.cloudinary.com/raphaelnoriode/image/upload/v1587322779/tutorials/response_xstkz5.png)

And that is it right there, simple way to build a slack bot with the @slack/bolt library and Nodejs. For more insight on what is possible check out this [Github repository](https://github.com/Oghenebrume50/slack-readExport) 

After building your Slack app, distributing it is necessary for visibility but it can get really tricky, I will be writing on that next.