When your team is out of the games, you have to somehow kill the time.
Alas, Italy is not taking part in the World Cup 2018 tournament.
As an Italian which follows football only when the national team is playing, this is a nightmare becoming real. Since I was a child, I waited for the World Cup to come every 4 years to support Italy and losing my voice screaming at every goal.
Not having to follow the matches, though, gave me some spare time. And if I can’t participate to this global event as a supported, I still can do it as a developer.
That’s why I came up with the idea of creating a Slack Bot for the World Cup 2018.
To spice up the task a bit, I decided to give me 24h only and see what I could come up with.
The goal (pun intended) of the bot was simple.
It had to be able to notify on a Slack channel about details of every current match. In particular, about start, end and half times, goals, yellow and red cards, substitutions.
You can see the result of my small experiment here. You can easily clone and install it on your own Slack account.
In the rest of the post, I’ll explain how the bot was designed and developed.
The base idea
The bot is a simple Go application that does the following:
- fetches information about today matches from worldcup.sfg.io (thank you, Eric S)
- figures out if there’s a live match;
- figures out what happened in the live match since the last time we fetched;
- extracts the latest match highlights;
- shouts the highlights in Slack.
The application is dockerized for an ready-to-go development experience and the compilation of the executable. Make + Docker is a killer combination for an easy yet powerful automation of your development tasks.
The application is also configurable in terms of polling time, bot appearance and slack channel.
The application design
The design of the bot revolves around 3 main components:
- Slack Bot
- World Cup domain model
Each of the components is reflected in its own Go package.
When the application is started, a Scheduler is created to take control of the operations.
At a configurable interval, the Scheduler executes the following steps:
- get the current match
- get the latest highlights of the current match
- send those highlights to the slack bot
The Slack bot is a very simple abstraction over the amazing golang Slack API.
It has a single method
Say(something string)to send a message to the channel configured at construction time.
World Cup domain model
All the definitions of teams, matches, events and highlights belongs here.
It also defines the repository for fetching the current match. Under the hood, the repository is calling the /today worldcup.sfg.io API and figures out whether there’s a live match ongoing.
It always amazes me how easy and fast you can build and deploy a backend application with Go. As an example, I find
<-time.After(duration) a very elegant one-liner solution for implementing a ticking mechanism.
The 24h constraint was an effective method that prevented me from adding feature after feature upfront and never release something functional.
I must say I had a lot of fun building this bot. Nevertheless, my hope is that 4 years from now I’ll be able to spend time supporting my national team rather than building another World Cup bot again 🙂