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?

Masked Phone Numbers with Ruby and Rails

Download the Code

This Ruby on Rails sample application is modeled after the amazing rental experience created by AirBnB, but with more Klingons.

Host users can offer rental properties which other guest users can reserve. The guest and the host can then anonymously communicate via a disposable Twilio phone number created just for a reservation. In this tutorial, we'll show you the key bits of code to make this work.

To run this sample app yourself, download the code and follow the instructions on GitHub.

Read how Lyft uses masked phone numbers to let customers securely contact drivers

Let's get started!

Create a reservation

The first step in connecting a guest and host is creating a reservation. Here, we handle a form submission for a new reservation which contains the guest's name and phone number.

Loading Code Sample...
      
      
      
      
      app/controllers/reservations_controller.rb

      Reservation Creation Method

      app/controllers/reservations_controller.rb

      Part of our reservation system is receiving reservation requests from potential renters. However, these reservations need to be confirmed. Let's see how we would handle this step.

      Confirm the Reservation

      Confirm the Reservation

      Before the reservation is finalized, the host needs to confirm that the property is still available. Learn how to automate this process in our first AirTNG tutorial, Workflow Automation.

      Once the reservation is confirmed, we need to create a Twilio number that the guest and host can use to communicate in the provision_phone_number method.

      Loading Code Sample...
          
          
          
          
          app/models/reservation.rb

          Confirm Reservation Method

          app/models/reservation.rb

          Once the reservation is confirmed, we need to purchase a Twilio number that the guest and host can use to communicate.

          Purchase a Twilio Number

          Purchase a Twilio Number

          Here we use a Twilio REST API Client to search for and buy a new phone number to associate with the reservation. When we buy the number, we designate a Twilio application that will handle webhook requests when the new number receives an incoming call or text.

          We then save the new phone number on our Reservation model, so when our app receives calls or texts to this number, we'll know which reservation the call or text belongs to.

          Loading Code Sample...
              
              
              
              
              app/models/reservation.rb

              Provision Phone Number Method

              app/models/reservation.rb

              Now that each reservation has a Twilio Phone Number, we can see how the application will look up reservations as guest or host calls come in.

              Find a reservation when a guest or host calls

              Find a reservation when a guest or host calls

              In our controller, we create a filter which gets executed every time Twilio asks our application how to handle an incoming call or text. This filter finds and stores the correct reservation (the one associated with the anonymous number) as an instance variable that will be used as we connect the guest and host via voice or SMS.

              Loading Code Sample...
                  
                  
                  
                  
                  app/controllers/reservations_controller.rb

                  Find a Reservation

                  app/controllers/reservations_controller.rb

                  Next, let's see how to connect the guest and the host via SMS.

                  Connect the Guest and the Host via SMS

                  Connect the Guest and the Host via SMS

                  Our Twilio application should be configured to send HTTP requests to this controller method on any incoming text message. Our app responds with TwiML to tell Twilio what to do in response to the message.

                  If the initial message sent to the anonymous number was made by the host, we forward it on to the guest. But if the message was sent by the guest, we forward it to the host.

                  Loading Code Sample...
                      
                      
                      
                      
                      app/controllers/reservations_controller.rb

                      Connect Via SMS

                      app/controllers/reservations_controller.rb

                      Let's see how to connect the guest and the host via phone call next.

                      Connect the Guest and Host via Phone Call

                      Connect the Guest and Host via Phone Call

                      Our Twilio application will send HTTP requests to this method on any incoming voice call. Our app responds with TwiML instructions that tell Twilio to Play an introductory MP3 audio file and then Dial either the guest or host, depending on who initiated the call.

                      Loading Code Sample...
                          
                          
                          
                          
                          app/controllers/reservations_controller.rb

                          Connect Via Phone Call

                          app/controllers/reservations_controller.rb

                          That's it! We've just implemented anonymous communications that allow your customers to connect while protecting their privacy with the help of the Twilio Ruby Helper Library.

                          Where to Next?

                          Where to Next?

                          If you're a Ruby developer working with Twilio, you might want to check out these other tutorials.

                          Part 1 of this Tutorial: Workflow Automation

                          Increase your rate of response by automating the workflows that are key to your business.

                          Appointment Reminders

                          Send your customers a text message when they have an upcoming appointment - this tutorial shows you how to do it from a background job.

                          Did this help?

                          Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Tweet @twilio to let us know what you think.

                          Jarod Reyes Jose Oliveros Agustin Camino David Prothero Hector Ortega Andrew Baker  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...