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 Java and Servlets 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
We'll be using the Twilio REST API to send our users messages at important junctures. Here's a bit more on our API:
Ready to go? Boldly click the button right after this sentence.
For this workflow to work, we need to have a user model and allow logins.
Next, let's look at the Vacation Property model.
In order to build a true vacation rental company we'll need a way to create a property rental listing.
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
The model has two associations implemented: there are many reservations in it and many users can make those reservations.
Next, let's look at the all-important 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:
Next up, we'll show how our new application will create a new reservation.
The reservation creation form holds only one field: the message that will be sent to the host when reserving one of her properties.
The rest of the information necessary to create a reservation is taken from the logged in user and the relationship between a property and its owner. Our base generic
Repository is in charge of handling entity insertion.
A reservation is created with a default status
pending. This lets our application easily react to a host rejecting or accepting a reservation request.
Now let's look at how the system will notify a
host when a new reservation request is submitted.
When a reservation is created for a property we want to notify the owner that someone has made a reservation.
We use an abstraction called
SmsNotifier which under the hood uses another abstraction called
Sender. Here is where we use Twilio's Rest API to send an SMS message to the host using your Twilio phone number. That's right - it's as simple as that to send a SMS with Twilio.
The next step shows how to handle a host accepting or rejecting a request - let's continue.
Let's take a closer look at the
ReservationConfirmation servlet. This servlet handles our incoming Twilio request and does three things:
- Check for a pending reservation from the incoming user.
- Update the status of the reservation.
- Respond to the host and send notification to the guest.
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 to use 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, including the
From phone number and the message
We'll use the
From parameter to lookup the host and check if he/she has any pending reservations. If he/she does, we'll use the message body to check for an 'accept' or 'reject'.
Finally, we update the reservation status and use the
SmsNotifier abstraction to send an SMS to the guest with the news.
Next up, let's see how we will respond to Twilio.
Finally, we'll use Twilio's TwiML as a response to Twilio's server instructing it to send SMS notification message to the host.
We'll be using the
Message verb to instruct Twilio's server that it should send the message.
Congrats! You've just learned how to automate your workflow with Twilio SMS.
Next, let's look at some other interesting features you might want to add to your application.
Like Twilio? Like Java? You're in the right place. Here are a couple other excellent tutorials:
Build a server notification system that will alert all administrators via SMS when a server outage occurs.
Convert web traffic into phone calls with the click of a button.
Thanks for checking this tutorial out! Tweet to us @twilio with what you're building!