This web application shows how you can use Twilio to send your customers a text message reminding them of upcoming appointments.
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.
Let's get started! Click the button below to get started.
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.
Application object is the heart of any Flask app. Our's initializes the app, sets the URLs, and pulls in all our environment variables.
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.
Application ready, let's create an
Our Appointment model is pretty simple. The
phone_number fields tell us who to send the reminder to. The
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.
This view handles creating new appointments and scheduling new reminders. It accepts POST data sent to the
We use WTForms to validate the form data using a class called
NewAppointmentForm that we defined in
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
We'll look at that task next.
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
We'll use this
client object to send a text message using the Twilio API in our
Let's look at
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
That's it! Our Flask application is all set to send out reminders for upcoming appointments.
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.
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!