The Hyperpessimist

The grandest failure.

Introducing Slacko

I’ve been using Slack these days quite a bit and overall I quite like the idea. It is basically a more accessible IRC so you can put even non-techies in front of it and tell them to use it. Slack provides a number of integrations with 3rd party services. We use GitHub, Codeship, Trello and Twitter, but I wanted to do my own integration, with our home-brew deployment solution.

So I did what every hacker would do and created a new interface to Slack from scratch and it being implemented in OCaml I called it Slacko. In case you’re wondering, that shape in the Slacko logo is a Fira Sans Heavy Italic uppercase O in the color of the Slack font (wow, that sounds like “Chai Caramel Latte Semi-Soy”, sorry for this).

tl;dr: Get Slacko 0.9.0, test it and report bugs! I plan to collect input and publish a version 1.0.0 soon!

So why did I write it?

The Community-built Slack integrations page features a number of integrations, but zero for OCaml and the Haskell integrations are very rudimentary (though hilarious). It has to run on CoreOS without installing additional stuff, so basically everything which comes with an interpreter is out. Even then, some other integrations, like the Clojure ones are in a kinda sorry state. I’m also not too impressed with the Go integrations (or Go itself, but that’s another matter) and I won’t touch C or C++ for this because I rather waste my time doing something sensible. So I hacked it up using OCaml. Because I can.

Let’s talk about the API: Slacko currently mirrors the Slack REST API 1:1, so everything is stringly typed. For version 1.0 I plan to leave it like that, future versions will have more explicit types built on top of this foundation, but I believe it is already useful as is and can already be used productively. Every call returns a JSON data type that can be traversed to get the information. Future versions will most likely parse this information, but I mostly just use it to post to Slack and don’t need to actually read the information returned from Slack; except for error codes which are already mapped onto error types.

What I really liked in OCaml was the way that I can chain functions with the new @@ and |> operators. I liked $ from Haskell so initially I thought I’ll be using @@ a lot, but it turns out |> was more useful for me. It allowed me to write code that was kinda similar to shell pipes or to code in applicative languages like Factor. Here’s an example:

let im_history token ?latest ?oldest ?count channel =
  let uri = endpoint "im.history"
    |> definitely_add "token" token
    |> definitely_add "channel" channel
    |> optionally_add "latest" latest
    |> optionally_add "oldest" oldest
    |> optionally_add "count" count
  in query uri

So this is a function with 5 arguments, 3 of them optional. I’ve defined helper functions definitely_add and optionally_add which take a URI, add data if there was data to add (i.e. someone supplied the count argument) and return an URI. All these calls are neatly chained using |>, in a similar way like the check it out yourself. It currently is rather thin, partly because I was too lazy, partly because it really is quite straightforward.

I filed a pull request to the OPAM repository which got merged quickly, so you can just install Slacko with a simple

$ opam install slacko

Hope you’ll enjoy and looking forward for your input.