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:
- 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.
- The application responds:
rainout set: July 5 @ 10:00 Today's games are cancelled.
- Players can check the status by sending a machine to the application:
rainout
- 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:
- A phone number is associated with a web application by specifying the URI of the application.
- 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. - The application responds to the request with an XML document (TwiML), containing instructions on how to respond.
- 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.
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.
="1.0"="utf-8"="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
- Copy the provided files to your web server.
- Open a Twilio account (you'll receive some free credit).
- Order a telephone number.
- In the telephone number configuration, assign the URI of your application the to the SMS callback.
- 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