Kontena Slack Bot renewed

Jussi Nummelin
Kontena Blog
Published in
4 min readDec 19, 2017

--

Back in late 2015, I wrote a small Slack bot to control the Kontena Platform through chatting with a bot. A lot has happened since, both for the Kontena Platform and on the Slack side. In this short blog, I’ll re-introduce the Kontena Slack bot and also share some of the reasons why it needed an almost total re-write.

It’s quite funny to read that old blog, as I’m introduced as a guest writer. At that time I was working for another company, fast-forward roughly two years, and now I’m working for Kontena Inc. as a developer advocate.

Kontena Slack Bot

The idea is still the same, you can control your Kontena Platform via Slack by “chatting” with a bot. The bot is connected to your Kontena Platform and it executes the commands sent to it via Slack messages. The bot then responds back with the results of the command. Overall, it’s pretty easy and straight forward. The biggest benefit is all of the command outputs are visible to all of the people in the channel, so no more copy-pasting and guessing what’s happening.

Kontena updates

Kontena has had many, many updates since the early days of the bot. Of course as the bot integrates with the Kontena Platform using the bundled CLI tool, the platform updates do not matter that much. However, the CLI has seen lot of updates, as well, and the new versions of the CLI pretty much broke the bot.

TTY required

Many features and commands on the CLI need TTY capable input and output streams. All of the long running commands, such as stack deploy etc., use a spinner to indicate that the work is still ongoing. Naturally, the spinner also uses ANSI escape codes to make the terminal look nice for humans, but it also makes the command output difficult for computers to handle; by computer, I mean NodeJS in this case, as the bot is still written in NodeJS.

In the end, I ended up using node-pty to execute the commands. This basically emulates TTY capable execution for the Kontena CLI commands and allows you to control the emulated terminal size as well. The terminal sizing is needed since some of the tabular outputs automatically format based on the terminal size.

The biggest “difficulty” I had was filtering out the command output properly. As the commands are executed through the node-pty library, the bot sees the full output always; as in, it gets all the ANSI escape codes and other control chars that in the normal user terminal are seen as nice spinners, etc. To filter out ANSI escape codes, I decided on using the strip-ansi library. For the spinners and other “control” things I used good-old regex. :D Somehow writing regex always reminds me of this comment from Jamie Zawinskifrom back in ~2007:

Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.

Slack client updates

Since October 2015, the Slack NodeJS library has also seen a lot of updates. In fact, now coming back to it, it seems it’s almost totally re-written. Of course one of the reasons is that the NodeJS environment has also seen a lot of updates. :)

The newer Slack library supports real-time messaging API through web sockets. Of course, since the Slack side API changed a lot, it also meant a re-write for the bot itself. Luckily, the bot is only around 100 lines of code, so it’s not really a big thing to re-write every now and then. :)

TODOs

As with any open source project, the bot is nowhere near to being “completed”. It still has some rough corners and may not be totally bug free. Some things I’m planning to continue working on:

  • Formatting of the messages. It would be nice to get proper formatting for the command output.
  • Code beautification.
  • Proper tagging for the image, so that it follows Kontena CLI versioning. Preferably in a way that when creating a new Kontena CLI image, it triggers a build somewhere that takes the new version as input, and automatically builds a new bot version. Otherwise keeping up with Kontena CLI versions will be a nightmare.

Please go and try the bot out, create issues and PR’s on it to make it better.

Image Credit: Siyan Ren.

About Kontena

Kontena provides the most easy-to-use, fully integrated solution for DevOps and software development teams to deploy, run, monitor and operate containers on the cloud. The underlying Kontena Platform technology is open source and available under Apache 2.0 license. It is used by hundreds of startups and software development teams working for some of the biggest enterprises in the world. www.kontena.io

--

--

Jussi Nummelin
Kontena Blog

Engineer, Dad, Fly-fisher, Husband; in varying order. Currently fiddling with Kubernetes at Mirantis