Talky Jukebox bot

8 Mar 2016

LShift  Python 

Originally posted at https://tech.labs.oliverwyman.com/blog/2016/03/08/talky-jukebox-bot/

Previously I talked about our soon-to-be-new Jukebox (which is currently blocked on going live due to a Mopidy bug). At the bottom of that post, I mentioned Slack support, but hadn’t figured out what sort of form this support would take. The Mopidy search interface is perfectly good, but having something that notifies our #jukebox Slack channel about new songs would be useful, especially when trying to later figure out context when someone says “this is a good song” or “I like this guitar section”. However, given the relatively low traffic in that channel, telling it about every single song would be somewhat overwhelming. There’s a good middle ground though – if someone says something in the channel, then mention the current song (unless we’ve already told the channel about the current song).

For this, I’ve built a separate Mopidy extension called Mopidy-Tachikoma (named after the adorable spider robots from Ghost in the Shell). Given the existence of both the Mopidy API and the Slackclient for Python, actually building it was quite easy. Testing looked a bit harder, given it’s a bridge between two services that I didn’t want to give Travis access to, but it turns out there’s a bunch of good ways to mock such things these days. In the case of Mopidy, I could get away with just building some mock objects instead because this extension would normally run in the Mopidy context. Slackclient wanted to do some HTTP and websocket work, but between VCR.py which lets you record HTTP requests (with the secret info stripped out, some magic to decompress gzip-encoded stuff and some manual editing of the result file to remove LShift-specific info) and the mock library, we can in fact get to 100% test coverage (I’m skipping the “main” routine, which is cheating a little…).

This also means I finally have my first module on PyPI which is nice. In the spirit of testing more other items, I also ran clonedigger against the source to prod me into de-duplicating the previously very copy/paste test code, which for a semi-defunct 8 year old project that’s still on Sourceforge works very well. Now all I need to do is integrate this into the Chef config…

Previously: Yet Another Jukebox Next: Herder: Automagic scheduling of a convention