DEV Community

sige for ConfigCat

Posted on

Complete guide to build a Slack Chatbot in 7 minutes and host it for free

Based on GitHub's Hubot. Connected to Slack. Deployed to Heroku.

I have a thing for robots and wanted to build a chatbot as an experiment that calls my service's (ConfigCat.com) \health endpoint and returns if everything is okay. It took me quite a long time to go trough the possible frameworks and docs, read all the outdated guides to find the quickest and cheapest way. I feel like it might worth sharing.

The tools needed:

Adding Hubot integration to your Slack Workspace

Find Hubot in Slack App Directory

Connect to Workspace

connect

name

Take a note of the API Token, you'll need it later

token

Running Hubot on local machine

Install Yeoman and Hubot generator

npm install -g yo generator-hubot
Enter fullscreen mode Exit fullscreen mode

Scaffold a Hubot project

mkdir catbot
cd catbot
yo hubot --adapter=slack
Enter fullscreen mode Exit fullscreen mode

Start Hubot using API Token

HUBOT_SLACK_TOKEN=xoxb-271695489427-739714865891-Z5gPPiuTORKDFO4QvqKe1B9y ./bin/hubot --adapter slack
Enter fullscreen mode Exit fullscreen mode

Open Slack and start a conversation

The chatbot should be available under Apps.

slack

Test with help command

help

Recognizing health command

And make a HTTP GET call to ConfigCat's /health endpoint and reply back the results via Slack.

I created a configcat.coffee under /scripts folder with the following code:

module.exports = (robot) ->
    robot.hear /health/i, (reply) ->
        robot.http("https://api.configcat.com/api/v1/health")
        .get() (err, res, body) ->
            reply.send body
Enter fullscreen mode Exit fullscreen mode

See the complete source code on GitHub.

I used CoffeeScript because I like to experiment and it feels fancy. But you can use JavaScript as well.

Test if the health check works

health

Deploying to Heroku

Check Node.js version

node --version
Enter fullscreen mode Exit fullscreen mode

Open package.json and check Node.js version. In my case the generated package.json was "node": "0.10.x" so I changed it to:

"engines": {
  "node": "10.16"
}
Enter fullscreen mode Exit fullscreen mode

Git commit

Make sure you’ve created a git repository, and that your work is committed.

git init
git add .
git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

Heroku CLI

Install the Heroku CLI. then log in.

heroku login
Enter fullscreen mode Exit fullscreen mode

Create app.

heroku create
Enter fullscreen mode Exit fullscreen mode

Set environment variable for API Token.

heroku config:set HUBOT_SLACK_TOKEN=xoxb-271695489427-739714865891-Z5gPPiuTORKDFO4QvqKe1B9yt --app=arcane-dusk-29327
Enter fullscreen mode Exit fullscreen mode

Git push

git push heroku master
Enter fullscreen mode Exit fullscreen mode

Avoiding sleep mode

Since I'm using Heroku's free plan, the app will eventually go to sleep mode. To avoid that add hubot-eroku-keepalive script to your chatbot.

Reference docs

Top comments (0)