Rate this page:

Send Appointment Reminders with Python and Flask

Ahoy! We now recommend you build your appointment reminders with Twilio's built in Message Scheduling functionality. Head on over to the Message Scheduling documentation to learn more about scheduling 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.

Let's 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.

Loading Code Sample...

        Configure the environment variables


        Now that the configuration is taken care of. We'll move on to the application structure.

        Let's look at 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.

        Loading Code Sample...

              Our core application code


              With our Application ready, let's create an Appointment model.

              Let's create some appointments

              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.

              Loading Code Sample...

                    The Appointment model


                    Next we will use this model to create a new Appointment and schedule a reminder.

                    Scheduling new reminders

                    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/

                    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.

                    Loading Code Sample...

                          Scheduling new reminders


                          We'll look at that task next.

                          Create the send_sms_reminder task

                          Set up a Twilio API client

                          Our 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.

                          Loading Code Sample...

                                Set up a Twilio API client


                                Let's look at send_sms_reminder now.

                                Send reminder messages with the Twilio API

                                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.

                                Loading Code Sample...

                                      Perform the actual task of sending a SMS


                                      That's it! Our Flask application is all set to send out reminders for upcoming appointments.

                                      Where to next?

                                      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:

                                      Click to Call

                                      Put a button on your web page that connects visitors to live support or sales people via telephone.

                                      Two-Factor Authentication

                                      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!

                                      Andrew Baker David Prothero Kat King Gonzalo Fernandez de Aguirre Stephanie Marchante Brianna DelValle Diego Villavicencio
                                      Rate this page:

                                      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.

                                      Loading Code Sample...

                                            Thank you for your feedback!

                                            Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

                                            Sending your feedback...
                                            🎉 Thank you for your feedback!
                                            Something went wrong. Please try again.

                                            Thanks for your feedback!