Updated: Aug. 18, 2017
During my time at Hackbright Academy, I built a full-stack web application called PAWS Finder. Recently, I started working on a new feature that allows users like my mother-in-law Harriet, to get alerts. She needed this feature so she can get updates from the Camp Cocker Rescue shelter about the status of Owen, the Cocker Spaniel.
In this blog, I will walk through how I used the Twilo API to build this alert feature.
Install Dependencies and Twilio Setup
In my github README, you will need to installation various software and dependencies. Assuming you’ve done this step you are ready to move on to Twilio.
Set up your account. Fortunately, Twilio has many tutorials on this topic.
The Flask Application
Before I go further, here’s how the alert feature will work. When Harriet signed up for an account, she chose to receive shelter alerts. PAWS Finder calls the Petfinder API on a weekly basis and any updates to Harriet’s saved data on PostgreSQL will trigger a Twilio alert.
Create the Flask Routes
Assuming you are familiar with Flask and have installed the web framework, it’s time to create the application and routes. The app file, which I called, paws_sms.py, is where I imported all the necessary modules, wrote os.environ variables to access my credentials (API keys, application keys) and created the Twilio Client object. Here are the routes.
- The /send-alert route will get triggered once any new information is recorded in the database. In lines 37 to 41, I set up the alert message using the Twilio Client object. For testing purposes, line 43, displays a message in a web page, ensuring that the alert was sent.
- The /sms route allows users like Harriet, to respond to the message.
Since I am running my application on my local dev environment, I needed a way to receive POST requests. Enter Ngrok! Check out this tutorial on downloading and getting ngrok to work.
With ngrok up and running, save the ngrok forwarding URL to your Twilio phone number-active number dashboard within the section Messaging – A MESSAGE COMES IN Webhook. Make sure to add your Flask route name that will be responding to the sms at the end of the ngrok URL. In this example, that route is /sms.
Run The App
Let’s give our application a go! Start your python server.
Let’s first check the /send-alert route using the ngrok forwarding URL to make sure this route is up and running.
Now check your phone. You should see the alert, similar to the one below.
Harriet just responded to this alert. She is looking forward to bringing Cody home!
Now that alerts are working, I will incorporate it into my PAWS Finder application and set up the trigger capabilities. This step requires some work on the database as well as addition of a python time module. Stay tuned for updates!
Questions or comments? Contact me via Twitter @LittonG or github, username glitton. Email works too firstname.lastname@example.org.