Menu

Expand
Rate this page:

Workflow Automation with Python and Flask

One of the more abstract concepts you'll handle when building your business is what the workflow will look like.

At its core, setting up a standardized workflow is about enabling your service providers (agents, hosts, customer service reps, administrators, and the rest of the gang) to better serve your customers.

To illustrate a very real-world example, today we'll build a Python and Flask webapp for finding and booking vacation properties — tentatively called Airtng.

Here's how it'll work:

  1. A host creates a vacation property listing
  2. A guest requests a reservation for a property
  3. The host receives an SMS notifying them of the reservation request. The host can either Accept or Reject the reservation
  4. The guest is notified whether a request was rejected or accepted

Learn how Airbnb used Twilio SMS to streamline the rental experience for 60M+ travelers around the world.

Workflow Building Blocks

We'll be using the Twilio REST API to send our users messages at important junctures. Here's a bit more on our API:

Loading Code Sample...
        
        
        airtng_flask/__init__.py

        Load the application configuration

        airtng_flask/__init__.py

        Let's get started! Click the below button to begin.

        Automate A Workflow

        User and Session Management

        For this workflow to work, we need to have Users created in our application, and allow them to log into Airtng.

        Our User model stores a user's basic information including their phone number. We'll use that to send them SMS notifications later.

        Loading Code Sample...
              
              
              airtng_flask/models/user.py

              The Airtng User model

              airtng_flask/models/user.py

              Next, let's look at how we define the VacationProperty model.

              Next

              The VacationProperty Model

              In order to build a vacation rentals company we need a way to create the property listings.

              The VacationProperty model belongs to the User who created it (we'll call this user the host moving forward) and contains only two properties: a description and an image_url.

              We also include a couple database relationship fields to help us link vacation properties to their hosts as well as to any reservations our users make.

              Loading Code Sample...
                    
                    
                    airtng_flask/models/vacation_property.py

                    VacationProperty model

                    airtng_flask/models/vacation_property.py

                    Next we'll take a look at how to model a reservation.

                    Next

                    The Reservation Model

                    The Reservation model is at the center of the workflow for this application. It is responsible for keeping track of:

                    • The guest who performed the reservation
                    • The vacation_property the guest is requesting (and associated host)
                    • The status of the reservation: pending, confirmed, or rejected
                    Loading Code Sample...
                          
                          
                          airtng_flask/models/reservation.py

                          The Reservation model

                          airtng_flask/models/reservation.py

                          Now that we have our models, let's see how a user would create a reservation.

                          Next

                          Creating a Reservation

                          The reservation creation form holds only one field, the message that will be sent to the host user when reserving one of her properties.

                          The rest of the information necessary to create a reservation is taken from the user that is logged into the system and the relationship between a property and its owning host.

                          A reservation is created with a default status pending, so when the host replies with an accept or reject response, the system knows which reservation to update.

                          Loading Code Sample...
                                
                                
                                airtng_flask/views.py

                                Routes for the Airtng workflow

                                airtng_flask/views.py

                                Now that we have seen how we will initiate a reservation, let's look at how to notify the host.

                                Show Me How to Notify the Host

                                Notify the Host via SMS

                                When a reservation is created for a property, we want to notify the host of the reservation request.

                                We use Twilio's Rest API to send a SMS message to the host, using a Twilio phone number.

                                Now we just have to wait for the host to send an SMS response accepting or rejecting the reservation. At that point we can notify the user and host and update the reservation information accordingly.

                                Loading Code Sample...
                                      
                                      
                                      airtng_flask/models/reservation.py

                                      Notify the user and the host

                                      airtng_flask/models/reservation.py

                                      Next, let's see how to handle incoming messages from Twilio webhooks.

                                      Hook 'Em

                                      Handle Incoming Twilio Requests

                                      This method handles the Twilio request triggered by the host's SMS and does three things:

                                      1. Checks for a pending reservation from a user
                                      2. Updates the status of the reservation
                                      3. Responds to the host and sends a notification to the user

                                      Setting Up Incoming Twilio Requests

                                      In the Twilio console, you should change the 'A Message Comes In' webhook to call your application's endpoint in the route /confirm:

                                      SMS Webhook

                                      One way to expose your machine to the world during development is using ngrok. Your URL for the SMS web hook on your phone number should look something like this:

                                      http://<subdomain>.ngrok.io/confirm
                                      

                                      An incoming request from Twilio comes with some helpful parameters. These include the From phone number and the message Body.

                                      We'll use the From parameter to look up the host and check if he or she has any pending reservations. If she does, we'll use the message body to check for the message 'accepted' or 'rejected'. Finally, we update the reservation status and use the SmsNotifier abstraction to send an SMS to the guest telling them the host accepted or rejected their reservation request.

                                      TwiML Response

                                      In our response to Twilio, we'll use Twilio's TwiML Markup Language to command Twilio to send an SMS notification message to the host.

                                      Loading Code Sample...
                                            
                                            
                                            airtng_flask/views.py

                                            Confirm or reject a pending reservation request

                                            airtng_flask/views.py

                                            Congratulations!

                                            You've just learned how to automate your workflow with Twilio Programmable SMS. In the next pane, we'll look at some other features Twilio makes it easy to add.

                                            What Else Can I Build?

                                            Where to Next?

                                            To improve upon this you could add anonymous communications so that the host and guest could communicate through a shared Twilio phone number: Call Masking with Python and Flask.

                                            You might also enjoy these other tutorials:

                                            IVR Phone Tree

                                            Create a seamless customer service experience by building an IVR (Interactive Voice Response) Phone Tree for your company.

                                            Click To Call

                                            Convert web traffic into phone calls with the click of a button.

                                            Did this help?

                                            Thanks for checking out this tutorial! If you have any feedback to share with us, please hit us up on Twitter and let us know what you're building!

                                            Andrew Baker Maylon Pedroso David Prothero Kat King Jose Oliveros Paul Kamp Stephanie Marchante Carolina Lopez Brianna DelValle
                                            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!

                                                  thanks-feedback-gif