Appointment Reminders with Python and Flask
Ahoy! We now recommend you build your SMS appointment reminders with Twilio's built in Message Scheduling functionality. Head on over to the Message Resource documentation to learn more about scheduling SMS messages!
This web application shows how you can use Twilio to send your customers a text message reminding them of upcoming appointments.
We use Flask to build out the web application that supports our user interface, and Celery to send the reminder text messages to our customers at the right time.
In this tutorial, we'll point out the key bits of code that make this application work. Check out the project README on GitHub to see how to run the code yourself.
Check out how Yelp uses SMS to confirm restaurant reservations for diners.
Let's get started! Click the button below to get started.
Configure the application to use Twilio
Before we can use the Twilio API to send reminder text messages, we need to configure our account credentials. These can be found on your Twilio Console. You'll also need an SMS-enabled phone number - you can find or purchase a new one to use here.
We put these environment variables in a .env
file and use autoenv to apply them every time we work on the project. More information on how to configure this application can be found in the project README.
Now that the configuration is taken care of. We'll move on to the application structure.
The application structure
The Application
object is the heart of any Flask app. Our's initializes the app, sets the URLs, and pulls in all our environment variables.
The celery
method is boilerplate to configure Celery using settings and context from our Flask application. Our app uses Redis as a broker for Celery. But you can also use any of the other available Celery brokers.
To get Celery to run locally on your machine, follow the instructions in the README.
With our Application
ready, let's create an Appointment model
.
The Appointment model
Our Appointment model is pretty simple. The name
and phone_number
fields tell us who to send the reminder to. The time
, timezone
, and delta
fields tell us when to send the reminder.
We use SQLAlchemy to power our model and give us a nice ORM interface to use it with.
We added an extra method, get_notification_time
, to help us determine the right time to send our reminders. The handy arrow library makes this kind of time arithmatic easy.
Next we will use this model to create a new Appointment
and schedule a reminder.
Scheduling new reminders
This view handles creating new appointments and scheduling new reminders. It accepts POST data sent to the /appointment
URL.
We use WTForms to validate the form data using a class called NewAppointmentForm
that we defined in forms/new_appointment.py
.
After that we use arrow to convert the time zone of the appointment's time to UTC time.
We then save our new Appointment
object and schedule the reminder using a Celery task we defined called send_sms_reminder
.
We'll look at that task next.
Set up a Twilio API client
Our tasks.py
module contains the definition for our send_sms_reminder
task. At the top of this module we use the twilio-python library to create a new instance of Client
.
We'll use this client
object to send a text message using the Twilio API in our send_sms_reminder
function.
Let's look at send_sms_reminder
now.
Sending a reminder
This is the send_sms_reminder
function we called in our appointment.create
view. Our function starts with an appointment_id
parameter, which we use to retrieve an Appointment
object from the database - a Celery best practice.
To compose the body of our text message, we use arrow again to convert the UTC time stored in our appointment to the local time zone of our customer.
After that, sending the message itself is a simple call to client.messages.create()
. We use our customer's phone number as the to
argument and our Twilio number as the from_
argument.
That's it! Our Flask application is all set to send out reminders for upcoming appointments.
Where to next?
We hope you found this sample application useful.
If you're a Python developer working with Twilio and Flask, you might enjoy these other tutorials:
Put a button on your web page that connects visitors to live support or sales people via telephone.
Improve the security of your Flask app's login functionality by adding two-factor authentication via text message.
Did this help?
Thanks for checking out this tutorial! If you have any feedback to share with us, please reach out on Twitter... we'd love to hear your thoughts, and know what you're building!
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.