My group of friends created an ESPN fantasy football league back when we were freshman in college. It started as a way to keep in touch and became a tradition that we all look forward to each year. Winning our league gives you ultimate bragging rights, and a nice prize of more than $1000. Needless to say, we take our league very seriously, and after several years of participating in the league, I decided we needed to step it up a notch. I started learning python in 2018 and was ready to use it, so I thought up a cool project that involved our leagues GroupMe chat. The code, or bot as I'll refer to it, can be triggered via text command by any user in the GroupMe. The bot then responds with the requested data in a readable format. I made this open source as well so anyone with an ESPN league that also uses GroupMe can set it up for their league. Scroll down to learn more about the project.

2019

Python, API, Heroku, ESPN, GroupMe,

Github description of the project

The main concept of this bot is to provide league data, current and past, to our GroupMe when requested. Any user in the group can type one of the commands seen in the image below which will trigger the bot to respond. For example, I might be bragging about how good my team is. By typing in "$current-ranks" in our GroupMe the bot will post the current ranks, showcasing my team ranked near the top. More-so, I might want to reference that my team was even better in prior years. I could type "2020-pf" to show that my team scored the most points that specific year. You get the idea.  

Examples of commands for the bot

I included a "$help" command that would give information about the bot and display what commands could be used. The code below shows how this function works. The output that displays the available commands is in a list, with each line being in its own list; a list of lists. Then while looping, the strings are formatted to ensure each command is correctly broken into a new line so it is easily readable for the GroupMe users.

The code used to display the available commands

The logic of the bot was pretty simple. First, listen for specific text strings from the GroupMe. Second, check that the command is from a valid user in the group to ensure bots aren't triggering each other. Third, grab the data from ESPN and format it nicely for GroupMe. Finally, send the message into the GroupMe for everyone to see. You can see below where my friend Shane starts off this process by using a command for the league ranks from 2017. In a matter of seconds the bot returns the results.

The bot responding to a request to display the rankings from 2017

You can see below where the bot checks for the specific string of text, when it sees that Shane posted "$2017-ranks" it will then execute the reply functionality, which calls a function as its parameter to gather the ESPN data using the provided year from the GroupMe user.

The bot checks to see which year to retrieve data from for the provided command

That function then makes an API request to ESPN's shrouded API that famously shut down in 2014. Luckily some generous developers around the world have documented the existing API structure so that ESPN users like me can still access our data for our own use. Below shows all of the magic behind the scenes, which isn't really magic at all. Getting the request back, looping to grab the specific data we want, and formatting it into a readable post for the GroupMe. 

Function for historical rankings that takes the year as a parameter

I wrote this bot with the intent of hosting it on Heroku, where it resides currently and actively provides my fantasy league all the stats we need for arguing in our GroupMe! If you're interested in getting an instance of this bot setup for your own league, check out the Github repo. Feel free to reach out to me for help or suggestions as always!

You may also like

Back to Top