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:
- A host creates a vacation property listing
- A guest requests a reservation for a property
- The host receives an SMS notifying them of the reservation request. The host can either Accept or Reject the reservation
- 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:
Let's get started! Click the below button to begin.
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.
User model stores a user's basic information including their phone number. We'll use that to send them SMS notifications later.
Next, let's look at how we define the VacationProperty model.
The VacationProperty Model
In order to build a vacation rentals company we need a way to create the property listings.
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
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.
Next we'll take a look at how to model a reservation.
The Reservation Model
Reservation model is at the center of the workflow for this application. It is responsible for keeping track of:
guestwho performed the reservation
vacation_propertythe guest is requesting (and associated host)
statusof the reservation:
Now that we have our models, let's see how a user would create a reservation.
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
reject response, the system knows which
reservation to update.
Now that we have seen how we will initiate a reservation, let's look at 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.
Next, let's see how to handle incoming messages from Twilio webhooks.
Handle Incoming Twilio Requests
This method handles the Twilio request triggered by the host's SMS and does three things:
- Checks for a pending reservation from a user
- Updates the status of the reservation
- 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
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:
An incoming request from Twilio comes with some helpful parameters. These include the
From phone number and the message
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.
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.
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.
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:
Create a seamless customer service experience by building an IVR (Interactive Voice Response) Phone Tree for your company.
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!
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.