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 C# and ASP.NET Core MVC 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 use case to work we have to handle authentication. We will rely on ASP.NET Core Identity for this purpose.
Identity User already includes a
phone_number that will be required to later send SMS notifications.
Next let's take a look at the Vacation Property model.
Our rental application will obviously require listing properties.
VacationProperty belongs to the
User who created it (we'll call this user the host from this point on) and contains only two properties: a
Description and an
VacationProperty can have many
Next, let's see what our Reservation model looks like.
Reservation model is at the center of the workflow for this application.
It is responsible for keeping track of:
VacationPropertyit is associated with to have access. Through this property the user will have access to the host phone number indirectly.
PhoneNumberof the guest.
Messagesent to the host on reservation.
Statusof the reservation.
Now that our models are ready, let's have a look at the controller that will create reservations.
The reservation creation form holds only a single field: the message that will be sent to the host when one of her properties is reserved. The rest of the information needed to create a reservation is taken from the
A reservation is created with a default status
ReservationStatus.Pending. That way when the host replies with an
reject response the application knows which reservation to update.
Next, let's see how we will send SMS notifications to the vacation rental host.
When a reservation is created we want to notify the owner of the property that someone is interested.
Next we just have to wait for the host to send an SMS response accepting or rejecting the reservation. Then we can notify the guest and host that the reservation information has been updated.
Now's let's peek at how we're handling the host's responses.
Sms/Handle controller handles our incoming Twilio request and does four things:
- Check for the guest's pending reservation
- Update the status of the reservation
- Respond to the host
- Send notification to the guest
Let's have closer look at how Twilio webhooks are configured to enable incoming requests to our application.
In the Twilio console, you must setup the sms web hook to call your application's end point in the route
One way to expose your development machine to the outside world is using ngrok. The url for the sms web hook on your number would look like this:
An incoming request from Twilio comes with some helpful parameters, such as a
from phone number and the message
We'll use the
from parameter to look for the host and check if he/she has any pending reservations. If he/she does, we'll use the message body to check for 'accept' and 'reject'.
In the last step, we'll use Twilio's TwiML as a response to Twilio to send an SMS message to the guest.
Now that we know how to expose a webhook to handle Twilio requests, let's see how we generate the TwiML needed.
After updating the reservation status, we must notify the host that he/she has successfully confirmed or rejected the reservation. We also have to return a friendly error message if there are no pending reservations.
If the reservation is confirmed or rejected we send an additional SMS to the guest to deliver the news.
We use the verb Message from TwiML to instruct Twilio's server that it should send the SMS messages.
Congratulations! You've just learned how to automate your workflow with Twilio SMS.
Next, lets see what else we can do with the Twilio C# SDK.
If you're a .NET developer working with Twilio you know we've got a lot of great content here on the Docs site. Here are just a couple ideas for your next tutorial:
Easily route callers to the right people and information with an IVR (interactive voice response) system.
Instantly collect structured data from your users with a survey conducted over a voice call or SMS text messages.
Thanks for checking out this tutorial! Tweet to us @twilio with what you're building!