Making an SMS Birthday Card with Python and Flask

My friend Sonya turned 30 this past weekend and I wanted to whip up something heartfelt and crafty to herald the glorious conclusion of her twenties.  Given that my competency with glue guns and popsicle sticks is lower than Lee Evans’ current self-esteem, I was left with two options:

  1. Buy something on Etsy and pass it off as my own.
  2. Cut some code.

Since Sonya possesses one of the most finely-tuned baloney filters in New York, I felt the latter was the safest choice.  But what to write given my busy schedule as friendly neighborhood developer evangelist and worse predilection for fanciful side projects than Ben Gibbard?

Her husband Alex and I rapped for a few minutes on what we could code quickly and came up with the terrifically telephonic “Reasons Sonya Is Awesome Hotline” – a number the birthday gal could SMS anytime and receive a random reason why she is awesome.  In a couple dozen minutes of hacking and testing we would have a handcrafted heartwarmer that she could keep and use regardless of any new phone she chooses to by in the future.  While he set hurriedly to collect reasons from her friends and family, I opened a terminal and started going to town.

Cloud-based Tools for Quick Craftwork

Speed was key, so I needed to rock this Twilio app with a quickness.  Flask is my usual go-to for speedy development the Python web microframework sports a Werkzeug foundation leaving plenty of easy hosting options, simple RESTful request dispatching, and a built-in development server for easy debugging.

The framework choice needed to be paired with a no-muss-no-fuss hosting option.  Zero configuration with uberfast deployment were top requirements, making Heroku a perfect choice.  They added support for my favorite language last fall and their step-by-step Python setup guide only takes a few minutes to complete.  With that complete, deploying Sonya’s birthday app was as simple as a git push – all that remained was to kick out some code.

Flexing with Flask

Flask’s simplicity meant I could keep the entire app in a single python file, which I opened as app.py.  First step, write a little boiler plate to define the Flask app:

Next, I define a list of reasons the birthday app recipient is awesome (dutifully collected by my partner-in-crime Alex).

After that, I created a TwiML application we can use for our hotline’s SMS Request URL that responds with a random reason why Sonya is awesome inside a SMS verb.

For a little flourish, I go ahead and add a HELP keyword so our birthday gal is not completely confused by a text from an unknown number telling her why she is awesome.

And just like that, Alex and I had crafted a birthday hit.  To make the gag even better, I ended up tacking on a Twilio Voice app and even a little website for the project.

For the full code for this Twilio-powered birthday surprise, you can check out the code here. You can also step through each part of this blog post with this gist.