Django-twilio to the Rescue

Amanda Casari recently gave a remote presentation (via Zoom) at Cleveland PyLadies on the topic of Feature Engineering for count and text data. Since she graciously offered a copy of her book to give away at the meetup, I wanted to put together a fancy little Twilio app to allow livestream viewers to enter.

I don’t know if I’ve come to expect an unreasonable level of documentation or what, but I was disappointed that the Twilio documentation only really gave useful information for hooking to Flask apps. While I’m not new to Flask, I’m much more fluent in the Django framework. As I was on a time crunch, I decided I was spending too much time looking up Flask particulars, so I ultimately stuck with Django for this particular project.

Handling the names that participants would be texting was my first goal, which was the minimum viable functionality. But, in typical Utterberg fashion, I decided to complicate things by having my app also generate a confirmation text response. I find it frustrating when you can’t be 100% confident that you entered the right digits, so a confirmation text seemed like it’d be worth the effort. The database structure was simple, as all I needed the app to do on intake was store the phone number and text body (participant name, per giveaway instructions) in a single model. Getting the webhook to play nicely on a data type level turned out to be the main nightmare. I must have wrestled with manual solutions for almost an hour!

Before django-twilio
Before django-twilio: Django did not play well with Twilio out of the box.

However, as this helpful article from Paul Hallett points out, the django-twilio package works in Django debug mode and formats the HttpResponse object in the way that Twilio expects! I had basic functionality with about 3 hours to spare.

Of course, opening the door to auto-generating texts led to me adding a congratulation text to the winner and “aw shucks, not this time” texts to all other entrants upon the official contest drawing. Luckily, a simple “for” loop in my random selection view took care of that pretty easily. Each participant received a text with a link to the GitHub repo for the book – and everyone except the winner also got a link to purchase the book itself.

Code for this project is available at https://github.com/mUtterberg/book_giveaway – though please do remember that I ran this locally via Ngrok due to time constraints. If I run a giveaway on this script again, I’ll be setting up legitimate hosting for it first.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.