Click here to Skip to main content
15,881,248 members
Articles / Web Development / ASP.NET

Public SMS Message Repository

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
3 Jun 2015CPOL4 min read 20.4K   201   20   6
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)


Written By
Engineer Robotic Assistance Devices / AITX
Canada Canada
Yvan Rodrigues has 30 years of experience in information systems and software development for the industry. He is Senior Concept Designer at Robotic Assistance Devices

He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).

Yvan draws on experience as owner of Red Cell Innovation Inc., Mabel's Labels Inc. as Manager of Systems and Development, the University of Waterloo as Information Systems Manager, and OTTO Motors as Senior Systems Engineer and Senior Concept Designer.

Yvan is currently focused on design of embedded systems.

Comments and Discussions

 
QuestionThis is the message response when trying to download project Pin
DanRGleason4-Jun-15 10:00
professionalDanRGleason4-Jun-15 10:00 
/KB/applications/996504/RedCell.Web.SmsRepository.zip appears to be missing on our servers. D'oh.
AnswerRe: This is the message response when trying to download project Pin
bigsquid4-Jun-15 10:19
bigsquid4-Jun-15 10:19 
AnswerRe: This is the message response when trying to download project Pin
Yvan Rodrigues8-Jun-15 6:09
professionalYvan Rodrigues8-Jun-15 6:09 
AnswerRe: This is the message response when trying to download project Pin
Yvan Rodrigues8-Jun-15 6:09
professionalYvan Rodrigues8-Jun-15 6:09 
Questionsource code file link broken Pin
Tridip Bhattacharjee3-Jun-15 23:32
professionalTridip Bhattacharjee3-Jun-15 23:32 
AnswerRe: source code file link broken Pin
Yvan Rodrigues8-Jun-15 6:09
professionalYvan Rodrigues8-Jun-15 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.