Over a million developers have joined DZone.

How to Write Your Own Slack Chatbot in Golang

Chatbots are all the rage nowadays, so I’ll show you how to plug one into your own Slack channel. It’s easy, fun, and, best of all, completely free!

· Integration Zone

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and dPaaS could cost you down the road. Brought to you in partnership with Liaison Technologies.

Slack Chatbot

Don't think Bender would dig the "non-sentient" classification.

Chatbots are all the rage nowadays, so I’ll show you how to plug one into your own Slack channel. It’s easy, fun, and, best of all, completely free!


Of course, you’ll need a Slack team which I’ll refer to as yourteam.slack.com throughout this post. The free plan includes up to 10 apps or service integrations and 5GB total file storage for the team which is more than enough for hooking up GitHub, your bot and your CI tool of choice. Semaphore, Travis, and Circle CI are all natively supported OOTB.

From there, you can create a custom integration for your bot:

Image title

Now, you’ve got an API token that allows your bot to connect to Slack! Copy this and keep it handy for the next step:

Image title


I used Norberto Lopes’ Slack API as the connector glue for my bot.

Pay particular attention how I get the bot to only react when it's called so it doesn’t get into any weird infinite loops (like an argument with itself).

Now for the fun part: writing your own custom commands so the bot can do some heavy lifting for you. As a simple prototype, I’ve asked it to fetch the 10-day forecast for my town via an sw cmd trigger. Otherwise, it just repeats back what you said to it in the form of a question.

My next idea is to set a timer that will have it periodically go out and perform a task (i.e., check out how the Cubs are advancing in the Pennant Race). The possibilities are only limited by your imagination so let loose and get creative!

Continuous Deployment

I use CircleCI (also free for building public repos) to build and deploy my bot as a Docker container to a DigitalOcean instance with every git push. It’s much easier than it sounds and the entire process (building a new Golang binary hosted inside a container and deployed to CoreOS) takes less than three minutes!

Note where the code loads the Slack API token from the environment. I’ve set this up in my CircleCI build settings:

Image title

Because sometimes tokens have undigestible characters which can get strangely escaped, I’ve base64 encoded the original Slack API one and decode it at Docker runtime.

Let me know if you have any questions or suggestions!

Discover the unprecedented possibilities and challenges, created by today’s fast paced data climate and why your current integration solution is not enough, brought to you in partnership with Liaison Technologies.

chatbot ,golang ,slack ,integration

Published at DZone with permission of Daniel Ackerson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}