Using Slack in your terminal with WeeChat

This blog explains why I decided to use WeeChat for Slack, and how to do it.

July 6, 2015
productivity hacks

Many development and ops teams rely on chat for communications. This isn’t in any way a new concept, since IRC has been with us since 1988. Nowadays, however, IRC is losing marketshare to somewhat more modern alternatives like HipChat and Slack. They offer native mobile and desktop apps, centralized logging (so no need for BNC software), drag and drop filesharing, etcetera.

‘Awesome!’ you might think. And in a lot of ways it is. Yet, today I dusted off my trusty old IRC client, WeeChat, to solve one of the glaring shortcomings of Slack. That’s right, I went back to a text-based interface because the modern and polished native app doesn’t have functionality I need. In a world where every application has tabs, and multiple windows, the Slack app offers me just a single window, with tabs. How about detaching a tab to make a new window? Not an option because, well, it’s a single-window app. Splitting a window to show multiple tabs at once then? Nope.

So basically, I have a modern native app that is not capable of doing what many old-school text-based IRC client were already capable of doing 20 years ago: showing multiple things at once.

That sucks, especially when you are using Slack for notifications as well as discussions. If I’m in the middle of a discussion about branching models, I won’t see that builds are failing, or that some AWS instance has decided to stop existing.

The Solution

OK, so now we’ve established the fact that the native Slack app has a massive usability issue, let’s look at a way to solve it.

The guys at Slack have been so kind as to acknowledge that not everyone wants to use the native app or the web version, so they are offering so-called gateways for IRC and XMPP. This way, you can use your IRC or XMPP client of choice and just connect to Slack using that. This ofcourse means that some of the added functionality will be removed or less easy to use.

These Gateways are disabled by default, but can be enabled by a Team admin at https://my.slack.com/admin/settings. The use of SSL can also be enforced there.

If the gateway has been activated, you can find your gateway credentials at https://my.slack.com/account/gateways.

Setting up WeeChat for Slack

If you have WeeChat running, enter the following to add a new server for your Slack team:

/server add SOMENAME YOURSLACKTEAM.irc.slack.com/6667 -autoconnect -ssl -password=YOURGATEWAYPASSWORD -username=YOURGATEWAYUSERNAME -nicks=YOURSLACKUSERNAME -ssl_dhkey_size=512

Now, connect to Slack:

/connect SOMENAME

If, like me, you get SSL verification errors when connecting, check if gnutls is looking in the right place for the CA certificates bundle. On my shellbox, running Debian Wheezy, the following fixed it:

/set weechat.network.gnutls_ca_file "/etc/ssl/certs/ca-certificates.crt"

Update: Twitter user @dimitrieh pointed out that on OSX you may run into the same issue. To fix it, install OpenSSL using Homebrew and then fix the gnutls_ca_file using:

/set weechat.network.gnutls_ca_file "/usr/local/etc/openssl/cert.pem"

Disclaimer: I personally run weechat on OSX in a Docker container, so I haven’t actually tested this. A blog on weechat in Docker is coming, though.

If you are connected, you are automatically joined to all channels you are a member of. If you have a ‘vanilla’ install of WeeChat and would like a bit of insight in where all your channels are, refer to the WeeChat docs to find out about installing the buffers.pl plugin.

Slack specific optimizations

With a few tweaks and plugins we can replace most of the functionality we lost by moving from the native app to WeeChat.

Slacklog

Slacklog is a plugin created by the folks at Thoughtbot. It pulls the recent history of any channel from the Slack server whenever you join it. Follow the instructions in the Github repo to install it. However, if you are using a terminal with a Solarized Dark colorscheme, changing the logger.color.backlog_line setting to darkgray is a pretty bad idea. I changed it to blue.

Slackcat

Slackcat is a utility you can use to upload files or snippets to Slack or even just post from your command line. You can install it via RubyGems, using the method of your choice. I prefer RVM, but you can also install it system-wide.

Note for Debian Wheezy: if you install the gem system-wide as root, change the permissions on /var/lib/gems to allow read and execute for world, otherwise only root can use slackcat.

For ease of use, slackcat can read the API token from an environment variable, SLACK_TOKEN. But what if you are a member of multiple Slack teams? I solved this by adding this to my Bash configuration:

declare -A slack_token
slack_token[teama]="< my token here >"
slack_token[teamb]="< my token here >"

slackpost() {
  export SLACK_TOKEN=${slack_token[${1}]}

  slackcat ${*:2}
}

Now I can upload a file to a specific channel on a specific team:

$ slackpost teamb -c somechannel foo.txt

Or simply send some text to my friend Simon:

$ echo "Hello World!" | slackpost teama -u simon -p

Or upload the contents of a buffer in Vim directly to a channel:

:%! slackpost teamb -c mychannel

It might not be as fancy as drag and drop, but it gets the job done and even opens the door for easy automation.

Remove Mode messages

Slack is not directly compatible with the IRC protocol, and as a result of that some things behave a little strange. For instance, you will notice channels getting flooded with +v and -v mode changes, simply because users are becoming active or inactive on a channel. To filter these out, add the following command:

/filter add hidevoices irc.SOMENAME.* irc_mode (\+|\-)v

Again with SOMENAME being the identifier of your Slack server in WeeChat. Please not though, that Slacklog will not suppress those messages, so sometimes the history as presented by Slacklog might look a little weird.

That’s all?

If you want, you can customize WeeChat much, much further with all sorts of scripts and plugins. Take a look at the WeeChat docs, and if you feel lucky, build your own plugins. Then ofcourse, you will setup a Slack integration with your CI tools so every build job will report to a Slack channel that you can now keep track of all the time, while still taking part in another discussion about branching models. Or pizza.