MusicBot is a Slack integration that facilitiates control of music playback through a Sonos system. This Python/Flask project interfaces with the Sonos hardware through the SoCo Python library, and with Spotify’s API. The system is configured to work with Slack as an Outgoing webhook, which listens to all conversations on a set channel.

Users can add tracks to the Sonos playlist by typing ‘add ‘ in the designated channel. The search query is routed to Spotify’s API, and the top five search results are returned to the user, who then selects the desired track by typing a number 1 through 5. MusicBot will also continue to randomly add previously-selected tracks, as well as tracks from a seed playlist during the day, ensuring there is always music playing.

Under the hood, when a user adds a track, it is added as the next track to the Sonos device. It is also recorded in a `playlist` table of a MySQL database. This table is used by MusicBot for selecting random tracks. There is a 2nd Python app which runs in tandem with the Slack integration, which polls the Sonos device’s queue at regular intervals, and adds a track if there is nothing left on the list.

Users also have the ability to veto tracks (a track with enough vetoes will be skipped, and will be less likely to be randomly chosen again), upvote tracks (which increases their likelihood to be selected again), see information about current and upcoming tracks, and control the volume. Admin users can also start/stop playback, skip tracks, and clear down the queue.

This project was completed during my internship at 50onRed, and is still in daily use in the office. It was my first experience writing Python code, and working with the flask framework. Since project completion, I have continued to work on the project: refactoring code for cleanliness, write unit tests, and adding features. You can read about my process of developing this act here.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn