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. On 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.
The first step in connecting a guest and a host is creating a reservation.
We handle here a submission form for a new reservation. After we save the reservation to the database, we send the host an SMS message asking them to accept or reject the reservation.
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.
Once the reservation is confirmed, we need to purchase a Twilio number that the guest and host can use to communicate.
Here we use the Twilio Python helper library to search for and buy a new phone number to associate with the reservation. We start by searching for a number with a local area code - if we can't find one, we take any available phone number in that country.
We then save the new phone number on our
Reservation model. Therefore when our app receives calls or messages to this number we know which reservation the call or text belongs to.
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.
When someone messages or calls one of the Twilio numbers (that we purchased for a reservation) Twilio makes a request to the URL you set in the TwiML app. That request will contain some helpful metadata:
incoming_phone_numbernumber that originally called or sent an SMS.
anonymous_phone_numberTwilio number that triggered this request.
In our code we use the
To parameter sent by Twilio to find a reservation that has the number we bought stored in it, as this is the number both hosts and guests will call and send SMSs to.
Next, let's see how to connect the guest and the host via SMS.
Our TwiML 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 sent by the host, we forward it on to the guest. Likewise, if the original message was sent by the guest, we forward it to the host.
We wrote a helper function called
gather_outgoing_phone_number to help us determine which party to forward the message to.
Let's see how to connect the guest and the host via phone call next.
That's it! We've just implemented anonymous communications that allow your customers to connect while protecting their privacy.
If you're a Python developer working with Twilio you might want to check out these other tutorials:
Create a seamless customer service experience by building an IVR Phone Tree for your company.
Measure the effectiveness of different marketing campaigns by assigning a unique phone number to different advertisements and track which ones have the best call rates while getting some data about the callers themselves.
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.