Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Appointment Reminders with Python and Flask

Download the Code

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...
      
      
      
      
      .env.example

      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...
          
          
          
          
          application.py

          Our core application code

          application.py

          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...
              
              
              
              
              models/appointment.py

              The Appointment model

              models/appointment.py

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

              Loading Code Sample...
                  
                  
                  
                  
                  views/appointment.py

                  Scheduling new reminders

                  views/appointment.py

                  We'll look at that task next.

                  Create the send_sms_reminder task

                  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.

                  Loading Code Sample...
                      
                      
                      
                      
                      tasks.py

                      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...
                          
                          
                          
                          
                          tasks.py

                          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 Agustin Camino David Prothero Kat King

                          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 browsing the Twilio tag on Stack Overflow.

                          Loading Code Sample...