Back in late 2015, I wrote a small Slack bot to control the Kontena Platform by chatting with a bot. A lot has happened since, both for the Kontena Platform and also 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.
Actually it's quite funny to read that old blog, as I'm introduced there as a guest writer, at that time working for some other company than Kontena Inc. 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 basically executes the commands sent to it via Slack messages. The bot then responds back with the results of the command. It's pretty easy and straight forward. The biggest benefit is that all the command output is also visible for all the people in the channel, no more copy-pasting and guessing what's happening.
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. But also the CLI has seen lot of updates and the new versions of the CLI pretty much broke the bot.
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 going on. 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. And 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 too. The terminal sizing is needed since some of the tabular outputs automatically format based on the terminal size.
The biggest "difficulty" I had was to filter out the command output properly. As the commands are executed through the node-pty library, the bot sees the full output always. And by full I mean that it gets all the ANSI escape codes and other control chars that in the normal user terminal are seen as nice spinners etc... For filtering out ANSI escape codes I ended up using the strip-ansi library. For the spinners and other "control" things I ended up using good-old regex. :D Somehow writing regex always reminds me of this comment by Jamie Zawinski from 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. Naturally, as the Slack side API has changed a lot it also meant pretty much a re-write for the bot itself. Luckily, the bot is around 100 lines of code only, so not really a big thing to re-write every now and then. :)
As with any open source project, the bot is nowhere near to being "completed". It still has some rough corners and might not be totally bug free. Some of the things I'm planning to still work 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 somehow so that creating a new Kontena CLI image 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.
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