Build your Slack bot step by step
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.
- Create App at Slack.
- Test at local docker.
- 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,
- Docker for Mac, Docker for Windows or other Docker options.
- Localtunnel, or just run
npm install -g localtunnel
.
To create a Slack app.
- Go to Slack API page to create a APP.
- Set up incomming webhook, click "Add New Webhook to Team" and copy webhook URL to
chatbot.env
file.
- Run
docker-compose up -d
to start our docker. - 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
-
Click "Slash commands" and "Create New Command" in Slack API page.
-
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).
-
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.
-
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,
- Click "Bot Users" tab in Slack API page.
- Enter bot default name and Save Change.
- 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.
- You could then find "Bot User OAuth Access Token" in "Install App" and "OAuth & Permission" tab.
- Copy and paste it to chatbot.env.
- Run
docker-compsoe -d --build
to rebuild and restart docker. - 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.
- 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. - Set env variable(WEBHOOK_URL, COMMAND_TOKE, and BOT_TOKEN) by
heroku config:set {ENV_VAR_NAME}={ENV_VAR}
. - Commit your code change.
- Run
git push heroku master
. - 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
- 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...
- 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?