Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Public SMS Message Repository

5.00/5 (4 votes)
3 Jun 2015CPOL4 min read 21.1K   201  
Store and retrieve messages in a central repository with SMS using Twilio and ASP.NET.

Introduction

This project enables an individual use a mobile phone to store a short message "in the cloud", that can be picked up by others using their mobile phone.

This project will demonstrate how to:

  • use the Twilio TwiML API to respond to SMS messages;
  • use XML as a data store;
  • have some political fun along the way.

Try it (for free).

Before we get started, see the application in action. Using your mobile phone, SMS (text) bush to +1 530 PROJECT (as in Code Project, or +15307765328).

Note: The demo does not allow addition/change/deletion of messages.

Background

My softball team had a problem. If a game was cancelled or rescheduled due to rain (or hail), it was difficult to relay the message to all the players quickly. They would drive to the ball park, only to learn about the cancellation.

This project, written in 2012, and rewritten for this article, is a solution. It works like this:

  1. The team captain sets a message by sending a message to the application:
    set rainout bingo July 5 @ 10:00 Today's games are cancelled.
  2. The application responds:
    rainout set: July 5 @ 10:00 Today's games are cancelled.
  3. Players can check the status by sending a machine to the application:
    rainout
  4. The application responds:
    July 5 @ 10:00 Today's games are cancelled.

How it Works

Twilio is a telephony gateway. It bridges data and voice between the internet, POTS, and 3GPP networks. By setting up an account with Twilio, calls and messages be be directed to and from an assigned number.

This project uses a simple API, called TwiML. It's use is very simple:

  1. A phone number is associated with a web application by specifying the URI of the application.
  2. When the number receives a message, an HTTP POST is made to the specified URI as application/x-www-form-urlencoded, containing data and metadata.
  3. The application responds to the request with an XML document (TwiML), containing instructions on how to respond.
  4. The response is relayed back to the requestor.

Inside the Code

In this example I have used IIS/ASP.NET to act as the HTTP server and scripting interface. Incoming requests are handled by overriding the Render method of the Default Page class. If a form is POSTed with a Body parameter, the Controller is invoked.

Controller

Examples of valid request messages are:

  • bush
  • set greeting bingo Thanks for stopping by!
  • greeting
  • delete greeting bingo

ParseTwilioRequest uses a regular expression to split the message into its components.

C#
// Parse the twilio request.
string command = string.Empty, message = null, password = null, key = null;
var matches = Regex.Match(request, "^(?<command>[^ ]+) (?<key>[^ ]+) (?<password>[^ ]+)(?: (?<message>.*))?$|^(?<command>[^ ]+)(?: (?<values>.*))?$");

The possible message components are:

command
A verb/instruction. This is the only required component. Valid commands are any stored key, hello, ping, set, delete, and bush.
password
If performing a set or delete command, a password is required. The password is specified by Repository.Password.
key
If performing a set or delete command, the key of a key/message pair. The key must match a pattern, specified by Repository.KeyPattern.
message
If performing a set or delete command, the message of a key/message pair.

If no components are present, or the command is invalid, a default message is returned.

Repository

The application uses and XML file as a data repository, abstracted by the Repository class. It subclasses XDocument, and provides properties and methods to manipulate its data.

XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<repository password="bingo" pattern="[a-zA-Z0-9]{3,10}">
  <default>Reply with [hello|bush|{0}]. +1530PROJECT brought to you by Red Cell Innovation Inc.</default>
  <message key="foo">bar</message>
  <message key="rainout">July 5 @ 1000 Today's games are cancelled.</message>
</repository>

Bushisms

Just for some fun, a bush request invokes Bushisms.GetRandom() for a random Dubya quote.

Deployment

  1. Copy the provided files to your web server.
  2. Open a Twilio account (you'll receive some free credit).
  3. Order a telephone number.
  4. In the telephone number configuration, assign the URI of your application the to the SMS callback.
  5. Test, and use Twilio's alerts panel to debug.

Once your credit expires, this project will cost:

  • about USD 1.00 per month for a telephone number;
  • about USD 0.0075 (¾¢) per message sent or received.

Security Considerations

For my use, security was not a major concern. Keep in mind:

  • The password, used to set and delete messages is stored in plain text in the repository.xml file, so are the messages.
  • To secure the application, it should be configured on a server that uses SSL/TLS (HTTPS) to encrypt the transport of messages between Twilio and the web server.
  • By design, messages can be retrieved from the store using only a keyword.

Contribute

How could you use this application? What other use cases can you imagine? Add your response below.

Conclusion

This project demonstrates how to build a simple SMS application without the use any external libraries or dependencies.

I am not affiliated with Twilio. This article is not an endorsement for Twilio. Twilio is one telephony gateway, and there are many others. Each may offer different services, at different costs.

Note: Offering a working demo for this article has real costs. The cost of this demonstration is sponsored by Red Cell Innovation Inc. If you require development of a telephony or mobile application, please consider us.

History

  • May 31, 2015 – First publication

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)