Cloud Zone is brought to you in partnership with:

Eric is the Editorial Manager at DZone, Inc. Feel free to contact him at egenesky@dzone.com Eric has posted 804 posts at DZone. You can read more from them at their website. View Full User Profile

Leveraging Windows Azure and Twilio to Support SMS in the Cloud

12.13.2012
| 2840 views |
  • submit to reddit

 Introduction

  1. Leveraging SMS messaging in a cloud context is a powerful capability. There are a wide variety of applications that can be built upon the techniques I illustrate. Imagine that you have a cloud service that responds to SMS messages. Here are some examples:
    • Imagine you are presenting at a conference and you want to collect email addresses from the audience. This is the use case I will demonstrate.
    • You could SMS the message "TIME, JAPAN" and the cloud would send back the answer
    • The list goes on. Use your imagination.
  2. My solution leverages twilio from .net. I found the twilio guidance on doing this to be challenging. Myself, and my colleague, found it to be fairly challenging to implement. It took many hours of experimentation.
skmogwrg Prerequisites
  1. Sign up for an Azure account
  2. Sign up for Twilio Account
    • Twilio allows you to re-route SMS messages to your Azure hosted Account and then from Azure back to the phone.
    • You can sign up here: http://www.twilio.com/
There are some high level steps to follow:
  1. Sign up for Azure as discussed previously
  2. Create an Azure Cloud Service at the Azure Portal
    • You will end up with a URL that is the endpoint that will receive an SMS message forwarded by Twilio.
    • For example, that endpoint might look like this:
  3. Write our Azure Application that will be deployed to a MS data center.
  4. Deploy your app to a MS data center
  5. Sign up for a Twilio account
  6. You are done.
  7. You can now write apps that receive SMS messages and return responses from a cloud application.
Signing up for an Azure account
  1. Signing up for an Azure account means you need to follow this link:
  2. It is a no obligation. Yes, you have to supply a credit card. Amazon does this too. It helps guard against fraud.

jzc0cphi Create an Azure Cloud Service at the Azure Portal

txvrzc3g
  1. You can notice that I have clicked on NEW. The service I created can be seen in the list above. Note “ReceiveAndSendSms” is the name that I personally defined for me cloud service. You will necessarily need to have a different name. It becomes the endpoint, http://receiveandsendsms.cloudapp.net
  2. If you don’t know http://www.windowsazure.com/en-us/manage/services/cloud-services/how-to-create-and-deploy-a-cloud-service/
  3. Once you create your service, it will have an endpoint where SMS messages will be forwarded by Twilio.
  4. That endpoint, in my case is, http://receiveandsendsms.cloudapp.net. Yours will differ.
  5. You will need to use this endpoint at the Twilio portal. Twilio will forward SMS messages to this endpoint. The cloud app that you write will be able to read this endpoint.
Write an Azure Application that will be deployed to a MS data center.
  1. Here is a little tutorial that is similar to mine.
  2. http://msdn.microsoft.com/en-us/library/windowsazure/ee405487.aspx
  3. Start Visual Studio 2010/2012
  4. Select File/New Project
  5. Choose the template “Cloud / Windows Azure Cloud Service.”

n3n0ffan
  1. You will indicate a “Project Name” and “Location.”
  2. You will add a Web Role to the project

q0t2uaae
  1. Your finished solution will look similar to this:

cyw4etuo
  1. We now need to add some code that will execute when the SMS message is received
  2. Right mouse-click on the “Web Role” and choose “Add / New Item.” You will add a “Generic Handler.”


wbewc0j1

  1. You can keep the default name of Handler1.ashx, if you wish.

syey54xd

  1. There will be a method with the following signature: public void ProcessRequest(HttpContext context) {}
  2. This is where we will add our code to receive and process the SMS message.
  3. I will also add a method to perform the insert into the database
  4. Here is the two finished functions in Handler1.ashx.cs
public void ProcessRequest(HttpContext context)
{
    // Search for the word "Body=" and pluck out the value, which is the sms msg that was sent
    string srchfor = "Body=";
    int mylen = srchfor.Length;
    // Read the sms message sent
    System.IO.StreamReader reader = new System.IO.StreamReader(HttpContext.Current.Request.InputStream);
    string requestFromPost = reader.ReadToEnd();
    // Find the "Body=" string
    int offset = requestFromPost.IndexOf(srchfor);
    int end = requestFromPost.Substring(offset).IndexOf('&') + offset;
    // Pluck out the value
    string myval = requestFromPost.Substring(offset + mylen, end - (offset + mylen));

    InsertEmail(myval);

    // Write the upper case version back to the client
    context.Response.Clear();
    context.Response.ContentType = "text/xml";
    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
    string twiMLResponse = null;
    twiMLResponse = "Your email has been added " + myval + "";
    context.Response.Write(twiMLResponse);
    context.Response.End();

}
public void InsertEmail(string email)
{
    try
    {
        string connString = "Server=tcp:fj869qmhla.database.windows.net;" +
            "Database=SMSEmail; User ID=bruno@fj869qmhla;" +
            "Password=xyz;Trusted_Connection=False;Encrypt=False;";

        using (SqlConnection connection = new SqlConnection(connString))
        {
            connection.Open();
            SqlCommand cmdInsert = new SqlCommand(
                    string.Format("Insert Into [dbo].[Email]" +
                    "(email) Values" + "('{0}')", email), connection);
            cmdInsert.ExecuteNonQuery();
        }
    }
    catch (SqlException ex)
    {
        Exception error = new Exception("Failure on Insert!", ex);
        throw error;
    }

}

Deploy to the cloud

  1. There are many ways to deploy to the cloud. You can learn more here:
  2. http://msdn.microsoft.com/en-us/library/ff683672.aspx
  3. I am using the “Publish” method. You could also create a package, go to the portal, and upload it.

ggjnsenu The database
  1. The database is hosted in the cloud as well. I am using SQL Database. You can see I have created a database called SMSEmail. I have create a table called, “email”
  2. For more help, see this link, Getting Started with Windows Azure SQL Database (http://www.windowsazure.com/en-us/manage/services/sql-databases/getting-started-w-sql-databases/)

aosvghyw
  1. I created a very simple database with one table as follows:

y3pjvy52
Twilio Setup
  1. The next step is to configure twilio to point to our cloud service. You can sign up for a free twilio account at their website. There is some important information here.

q2deed5m We are done
  1. We just need to send a text message to 707-999-2736. Whatever his texted, gets saved into a database in the cloud. Cool stuff.




Published at DZone with permission of its author, Eric Genesky. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)