1. 程式人生 > >How I built an HR Slack Bot with Node and Botkit

How I built an HR Slack Bot with Node and Botkit

Why create a Slack Bot ?

I am an HR professional. More specifically I am a Human Resources Information System (HRIS) Consultant. I work with Application Tracking Systems, Learning Management Systems, and Core HR. But I have never had the opportunity to work with an HR Bot. Which may be the Future of HR.

I read a lot about bots on Slack and Messenger, and used some of them in my daily life — Product Hunt, GitHub and Trello. But for HR purposes, I have never had the opportunity to work with a tool tailored for my needs.

That’s why I decided to work on my own bot.

Me starting to work

My Goals

My bot should be able to manage all the needs a small company could have on Slack:

  • Onboarding
  • Putting people in touch
  • Reminders
  • Announcements
  • Birthdays /Anniversary
  • And many more

Reviewing the basics

For this program, I’ll use:

  • Botkit
  • Node JS
  • Express Server
  • MongoDB
  • Slack API & of course

Botkit is:

One easy way to build bot users, especially if you already work with Node.js, is Howdy’s Botkit
. Botkit is a framework that takes care of most these API gymnastics, so you can focus on your bot’s behavior.

Exactly what I was looking for :-)

Botkit provides a boilerplate for Slack. But I have chosen to start from scratch to have a better understanding of my bot. However, it’s a good idea to train yourself with a bot created on Glitch.

How do Slack bots work?

I am not an expert. I have read again and again Slack and Botkit’s official documentation. I’m still not sure I understood everything. Here is my understanding of a Slack bot’s behavior:

Every App on Slack has a “scope” which is a perimeter on which an app can read or perform actions. A bot is part of an application created and installed on Slack.

Therefore, when you install an app on Slack, you give access to some information and permissions to it. For your bot, you want it to be, at least, able to send and reply to messages of other users.

There are then two cases:

  1. You want your bot to react to events happening directly in Slack
  2. You want your bot to react to events happening on your server

We will view both of them in this post!

Getting Started

Before anything else, you will need a server. In my case, Express.

Below you’ll find my server.js file:

This port must be public and accessible, not just on a localhost.

For the moment, this server is a blank page, showing and processing nothing.

You’ll then need a Slack App: just follow this link to create one.

Then, you’ll have to configure your controller. The controller is the brain of your bot. It contains every skill and configuration. Below is my botkit.js file. It has almost the same content found in Botkit’s Starter kit available here: https://github.com/howdyai/botkit-starter-slack

Unlocking the first case: react to the events happening on Slack

When you give the right permissions to your app, every time a message is sent on a channel, Slacks sends a request to your server with some information — the channel ID, the user, the timestamp and most importantly, the content of the message.

If we want our bot to react to a simple message like “Hi”, we have to give Slack an address to send the information to.

In a routes.js file write:

var Request = require('request')var slack = require('../controllers/botkit')
module.exports = function(app) { app.post('/slack/receive', function(req,res){
//respond to Slack that the webhook has been received.    res.status(200);
// Now, pass the webhook into be processed    slack.controller.handleWebhookPayload(req, res)  })}

Once Slack is informed of this route via the Event Subscriptions page of your Slack App, it will be able to send it JSON. You will be able to receive it thanks to the parsing part of the server.js file above.

Here is a (simple) schema to explain the process behind it: