When I am searching through tutorial about build slack bot, I found that most of tutorials is not comprehensive. They are always missing some part and it is hard to find the missing puzzle. After few days of exploring, I finally successfully build my slack bot.

Here is a brief plan to build this slack bot.

  1. Create App at Slack.
  2. Test at local docker.
  3. Deploy on Heroku.

So lets start working on this.

First of all, you need to code your server, this will teach your bot how to interact with certain message. You can download my code form my github or git clone https://github.com/waitingduck/Scon-Breaker.git. This is basically a express server so it should be familiar to many.

Now we can start or plan. Before we start, you will need few things,

  1. Docker for Mac, Docker for Windows or other Docker options.
  2. Localtunnel, or just run npm install -g localtunnel.

To create a Slack app.

  1. Go to Slack API page to create a APP.
  2. Set up incomming webhook, click "Add New Webhook to Team" and copy webhook URL to chatbot.env file.
  3. Run docker-compose up -d to start our docker.
  4. Run lt --port 3000 --subdomain {YOUR_SUB_DOMAIN} to expose your loaclhost:3000 to outside. Remember this URL, we will use it later.
$ lt --port 3000 --subdomain sconbreaker
your url is: https://sconbreaker.localtunnel.me

Now we have an App, so we can integrate more functionality into this App. Two basic functionality you could consider is Slash Command and Bot User.

To integrate a slash command into your app, you need to

  1. Click "Slash commands" and "Create New Command" in Slack API page.

  2. Fill the form and your Request URL will be https://{YOUR_SUB_DOAMIN}.localtunnel.me/{COMMAND_PATH} (In this example, this command path is /commands/eatcake).

  3. Reinstall your app for your team(Click "Install App" and you will see "Reinstall App" button). So it will update new command in your slack.

  4. Try the command in your slack channel. It should fail, but it will send command token to our server.

    Command request body should loos like this.

       token: 'ICeyeUzM5DeW2sJtHQkZ6iJJJP9',
       team_id: 'ABCDEFG',
       team_domain: 'AABBCCD',
       channel_id: 'AAAAAAA',
       channel_name: 'name',
       user_id: 'HAHAHAHHA',
       user_name: 'aaaaaaa',
       command: '/eatcake',
       text: '',
       response_url: 'https://...' 

5 . You could log the request and retrieve the command token and put it into chatbot.env.
6. Run docker-compsoe -d --build to rebuild and restart docker. And you should be able to run your command in slack.

To integrate bot user to slack app,

  1. Click "Bot Users" tab in Slack API page.
  2. Enter bot default name and Save Change.
  3. Reinstall your app for your team(Click "Install App" and you will see "Reinstall App" button) so it will generate Bot User OAuth Access Token.
  4. You could then find "Bot User OAuth Access Token" in "Install App" and "OAuth & Permission" tab.
  5. Copy and paste it to chatbot.env.
  6. Run docker-compsoe -d --build to rebuild and restart docker.
  7. Invite your bot by /invite {YOUR_BOT_NAME} in slack channel. And you should be able to talk to your bot.

After testing on local docker, we are ready to deploy to server. There are several options, but I choose Heroku. In this step, you will need Heroku Toolbelt for the following steps.

  1. Run heroku create {optional-app-name} to create heroku repo. This project already has a heroku repo so you can add https://git.heroku.com/{OPTIONAL-APP-NAME}.git to your remote.
  2. Set env variable(WEBHOOK_URL, COMMAND_TOKE, and BOT_TOKEN) by heroku config:set {ENV_VAR_NAME}={ENV_VAR}.
  3. Commit your code change.
  4. Run git push heroku master.
  5. Run heroku open.

Conclusion: I feel it is very difficult to build a slack app in the begging. You will require a lot of tutorials and documents to find some missing puzzle. Two main drawback is

  1. Their Slack API require a lot of "Reinstall". You must reinstall your app so you can continue to next step. It also generate new webhook every time you reinstall. You have to go to "Incomming Webhooks" to remove them. Before I know I can remove it, I have almost ten webhooks...
  2. They have too many tokens and distributed in different places. They have "Client token", "Client secret", "Commend token"(this one is not even viable in UI...), "Oauth token" and "Bot token".
    However, once you understand all this stuff, it should be piece of cake.

I actually have take a look of FB chat bot tutorial, it is much easier, you tell them your entry point and set a secret token and done. Isn't it much easier?