Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now

Menu

Expand
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?

Workflow Automation with Java and Servlets

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:

  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:

Ready to go? Boldly click the button right after this sentence.

Boldly Move to the Next Step

User and Session Management

For this workflow to work, we need to have a user model and allow logins.

        
        
        
        
        src/main/java/org/twilio/airtng/models/User.java

        User and Session Management

        src/main/java/org/twilio/airtng/models/User.java

        Next, let's look at the Vacation Property model.

        Next

        Vacation Property Model

        In order to build a true vacation rental company we'll need a way to create a property rental listing.

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

        The model has two associations implemented: there are many reservations in it and many users can make those reservations.

              
              
              
              
              src/main/java/org/twilio/airtng/models/VacationProperty.java

              Vacation Property Model

              src/main/java/org/twilio/airtng/models/VacationProperty.java

              Next, let's look at the all-important Reservation model.

              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
                    
                    
                    
                    
                    src/main/java/org/twilio/airtng/models/Reservation.java

                    The Reservation Model

                    src/main/java/org/twilio/airtng/models/Reservation.java

                    Next up, we'll show how our new application will create a new reservation.

                    Next

                    Create a 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.

                          
                          
                          
                          
                          src/main/java/org/twilio/airtng/servlets/ReservationServlet.java

                          Create a new reservation

                          src/main/java/org/twilio/airtng/servlets/ReservationServlet.java

                          Now let's look at how the system will notify a host when a new reservation request is submitted.

                          Next

                          Notify the Host

                          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.

                                
                                
                                
                                
                                src/main/java/org/twilio/airtng/lib/sms/Sender.java

                                Notify the host

                                src/main/java/org/twilio/airtng/lib/sms/Sender.java

                                The next step shows how to handle a host accepting or rejecting a request - let's continue.

                                Next

                                Handle Incoming Messages

                                Let's take a closer look at the ReservationConfirmation servlet. This servlet handles our incoming Twilio request and does three things:

                                1. Check for a pending reservation from the incoming user.
                                2. Update the status of the reservation.
                                3. Respond to the host and send notification to the guest.
                                Next

                                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 /reservation-confirmation:

                                SMS Webhook

                                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:

                                http://<subdomain>.ngrok.io/reservation-confirmation

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

                                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.

                                      
                                      
                                      
                                      
                                      Webhook for handling Host's decision

                                      Handle incoming messages

                                      Webhook for handling Host's decision

                                      Next up, let's see how we will respond to Twilio.

                                      Next

                                      TwiML Response

                                      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.

                                            
                                            
                                            
                                            
                                            src/main/java/org/twilio/airtng/servlets/ReservationConfirmationServlet.java

                                            Build a TwiML response

                                            src/main/java/org/twilio/airtng/servlets/ReservationConfirmationServlet.java

                                            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.

                                            Next

                                            Where to Next?

                                            Like Twilio? Like Java? You're in the right place. Here are a couple other excellent tutorials:

                                            SMS and MMS notifications

                                            Build a server notification system that will alert all administrators via SMS when a server outage occurs.

                                            Click To Call

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

                                            Did this help?

                                            Thanks for checking this tutorial out! Tweet to us @twilio with what you're building!

                                            Agustin Camino David Prothero Kat King Samuel Mendes Paul Kamp Andrew Baker
                                            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 browsing the Twilio tag on Stack Overflow.