Employee Directory

Use Twilio to accept SMS messages and turn them into queries against a SQL database. This example functions as an Employee Directory where a mobile phone user can send a text message with a partial string of a person's name and it will return their picture and contact information (e-mail address and phone number).

Start Tutorial

What does this thing do?

This ASP.NET application is an Employee Directory that can be used from any SMS client to find an employee's contact information. To build this app, we will need to:

  • Create an Employee model
  • Receive an incoming SMS
  • Search for an Employee by name
  • Respond with an outgoing MMS

Let's get started!

See Also:

Create an Employee model

The first thing we need is a database of employees. We will be using Entity Framework (EF) Code First for this. That means we can use a POCO (Plain Old CLR Object) to represent our employees.

Our employee entity has a few fields for contact information, including their name, phone number, and a public URL containing an image of them.

Now, let's build a service for finding employees.

See Also:

Search for an Employee by name

The EmployeeDirectoryService class allows us to search the database for employees either by name or by their unique database identifier. When searching by name, we'll return a list of employees whose name might match a search query, or just one if we find an exact match. If we know the ID of the employee we're looking for, we can return it right away.

We'll use this search functionality when responding to an SMS from a user, which we'll look at next.

Receive an incoming SMS

When your number receives an SMS message, Twilio will send an HTTP POST request to our application. This will be handled by the Lookup action in EmployeeController.

We check for numeric input (more on that later) or perform a lookup for the desired employee. The results are packaged up as a TwiML response and sent back to Twilio and, in turn, the original sender of the SMS.

Let's take a closer look at building the response.

See Also:

Responding with single match for an Employee name

Let's say the service finds a single employee matching the text message. In this case, we simply write out a response that contains the employee's contact information, including a photo, making our response a MMS message.

A single matching employee isn't the only scenario, however.

See Also:

What if no Employee or multiple employees match?

If we don't find any employees, we can simply return a "Not found" message.

What about multiple matches? For this case, we want to return a list of the matching employees' names along with an incrementing number the end user can use to make their selection. For example, if someone searched for "David" they might get something like:

We found: 1-David Prothero, 2-David Smith
 - Reply with # of desired person

Let's see how we build that menu of choices next.

Returning a list of possible matches

For the message text returned to the user, we build a numbered menu of possible matches.

Our app needs to remember — between SMS messages from the user — the mapping of the 1, 2, 3 selection numbers to the actual unique ID's of employees. You will notice we are placing them in a cookie, which Twilio will send back with every HTTP request to our application.

Now let's look at how to handle the user's response to this menu of choices.

See Also:

Returning Employee's contact information by number choice

When we receive an SMS message, we check whether:

  • The body of the text input is, in fact, a number.
  • A cookie exists with the mapping of numbers to ID's.
  • The mappings contain the number sent to us by the user.

If any of those checks fail, then we'll simply proceed with our typical name lookup.

However, if those conditions are all met, we return a the single employee that matches their selection.

Only thing left to do is celebrate.


Where to next?

Take a look at the code on GitHub to run the application yourself. There you will find the complete Visual Studio solution and instructions for getting up and running.

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

Two-Factor Authentication

Appointment Reminders

Did this help?

Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Contact the Twilio Developer Education Team to let us know what you think.