Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Write Your Own Slack Chatbot in Golang

DZone's Guide to

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 ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

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!

Preparation

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

Code

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!

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

Topics:
chatbot ,golang ,slack ,integration

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}