Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

RailsConf2009 Integrating SMS with your Rails App


Published on

Learn how to integrate SMS with email gateways, GSM Modems, and Service Providers. Discuss working with service providers, obtaining a short code and some tips for integration including sending bulk SMSes. Concluded with a demo SMS on Rails available on

Published in: Technology, Business

RailsConf2009 Integrating SMS with your Rails App

  1. Integrating SMS Messaging with your Rails Application <ul><li>Blythe Dunham </li></ul><ul><li>[email_address] </li></ul><ul><li>spongecell .com / snowgiraffe .com </li></ul>
  2. Why SMS? <ul><li>Oldest most reliable means to communicate with mobile users </li></ul><ul><li>Cell phone agnostic </li></ul><ul><li>Everybody is doing it </li></ul><ul><li>Great way to receive emergency messages at the bar </li></ul>SERVER IS DOWN SPARKS ARE UP
  3. Popular Applications to Send SMS <ul><li>Standard Email </li></ul><ul><ul><li>yournumber@gatewayaddr </li></ul></ul><ul><ul><li>Find your provider gateway </li></ul></ul><ul><li>Instant Message </li></ul><ul><ul><li>AOL (US only) </li></ul></ul><ul><ul><li>Skype (small fee) </li></ul></ul><ul><li>Other </li></ul><ul><ul><li>Teleflip RIP </li></ul></ul><ul><ul><li>remindblast .com - Send SMS reminders to be awesome or do laundry </li></ul></ul><ul><ul><li>Facebook SMS app </li></ul></ul><ul><ul><li>Cell providers web page (ex. ) </li></ul></ul>
  4. Overview: One Way Messaging <ul><li>Mobile Terminated (MT) or Outbound </li></ul>Black Box of Your application & gateway & other Party at A1A Beach front Ave Mobile Terminated (MT)
  5. Overview: Two Way Messaging Black Box of Your application & gateway & other Mobile Originated (MO) or Inbound ROCK MO Respond with &quot;ROCK&quot; for Free party tix MT
  6. Simplest approach: SMS as email
  7. Pros and Cons of sending SMS through email <ul><li>The Good </li></ul><ul><li>Free to application (end-user with phone might pay) </li></ul><ul><li>Easy to integrate. Only email setup required. </li></ul><ul><li>Excellent for use with known numbers </li></ul><ul><ul><li>Emergency Messages with plugins like Exception Notification </li></ul></ul>
  8. Pros and Cons of sending SMS through email <ul><li>The Tricky </li></ul><ul><li>User carrier must be known </li></ul><ul><ul><li>additional user input </li></ul></ul><ul><ul><li>storage considerations </li></ul></ul><ul><li>No delivery confirmation </li></ul><ul><li>Supported by most US but not all cell phone carriers </li></ul><ul><li>No two way messaging </li></ul>
  9. Giant Black Box Explained: SMSCs, SMS Gateways, Service Providers <ul><li>Before exploring the other available SMS options, lets look at how SMS works </li></ul>
  11. GSM Modem Super App GSM Modem (Cell Phone) SMSC AT Commands
  12. GSM Modem APIs and Applications <ul><li>Kannel in C (open source) supports an API </li></ul><ul><li>RubyGSM - Written by Unicef for RapidSMS to inexpensively send text messages </li></ul>
  13. GSM Modem LoDown <ul><li>Advantages </li></ul><ul><li>Cheap </li></ul><ul><li>Can use almost anywhere </li></ul><ul><li>Two way messaging support </li></ul><ul><li>Disadvantages </li></ul><ul><li>Difficult to setup </li></ul><ul><li>Slow </li></ul><ul><li>Doesn’t scale (only 6 to 10 messages per second) </li></ul><ul><li>Must connect modem to a server </li></ul>
  14. Talk directly to all SMS Centers SMSC SMSC SMSC Awesome App
  15. Mission Impossible: Coding for each SMSC <ul><li>Large number of SMS Centers </li></ul><ul><li>Each use different and proprietary communication protocols </li></ul>Don’t do this unless you are me!
  16. SMS Gateway translates SMSC protocols SMSC SMSC SMSC Rockstar App SMS Gateway
  17. SMS Gateways <ul><li>Free Open Source Gateways </li></ul><ul><li>Kannel - written in C supports many SMSCs and also acts as a WAP gateway </li></ul><ul><li>Must setup/configure on a server yourself </li></ul>
  18. Pay the middleman! <ul><li>Charge for sending messages </li></ul><ul><ul><li>~ 5 cents per message </li></ul></ul><ul><ul><li>Bulk discounts </li></ul></ul><ul><li>Provide convenient APIs </li></ul><ul><li>Most provide two way messaging </li></ul><ul><li>Can help obtain short code </li></ul>
  19. SMS Gateway: Sending Outbound (MT) <ul><li>Typical approach for Outbound messages is simple: </li></ul><ul><li>Create an account </li></ul><ul><li>Buy credits (or messages) </li></ul><ul><li>Start sending messages through the API </li></ul>
  20. Ruby Outbound (MT) message with HTTPS <ul><li>http =, 443 ) </li></ul><ul><li>http.use_ssl = true </li></ul><ul><li>http.enable_post_connection_check = true </li></ul><ul><li>http.start {response = http.request_get(url_string)} </li></ul>
  21. Choosing an API (if you have a choice) <ul><li>HTTP/S (GET & POST) </li></ul><ul><ul><li>By far the easiest to implement </li></ul></ul><ul><ul><li>Great for sending < 10k messages a month </li></ul></ul><ul><li>SMPP </li></ul><ul><ul><li>Best for bulk messages (over 10k per month) </li></ul></ul><ul><ul><li>Leaves Connection open </li></ul></ul><ul><li>STMP - send through mail </li></ul><ul><li>Other options (not so hot) </li></ul><ul><li>FTP - Bulk option for legacy systems </li></ul><ul><li>XML (use HTTP if available) </li></ul><ul><li>SOAP (use HTTP if available) </li></ul><ul><li>Com Object (Seriously? What wah?) </li></ul>
  22. SMS Gateway: Two way messaging and MOs <ul><li>Typically enable Outbound messaging </li></ul><ul><li>Obtain or rent a short code </li></ul><ul><li>Respond to Inbound MOs </li></ul><ul><ul><li>Use a provider campaign management tool ($$$) </li></ul></ul><ul><ul><li>Parse yourself and write some regexp! </li></ul></ul>/^OPT(s*[-_]?s*)OUT|STOP|END|QUIT| UNSUBSCRIBE|EXIT|HALT| FUCK(s*[-_]?s*)OFF|REMOVE|CANCEL)/i
  23. Two way messaging: Pull <ul><li>Setup a cron task to pull new messages every few seconds. </li></ul>
  24. Two way messaging: Push <ul><li>Provide a callback URL that service provider invokes for each new message </li></ul>
  25. Do I need a short code? <ul><li>If your awesome app is: </li></ul><ul><li>A giant spam machine </li></ul><ul><li>Implementing two way messaging </li></ul><ul><li>(Recommended) Sending > 5000 messages per month </li></ul>
  26. Shared (Rented) Versus Dedicated Short Codes <ul><li>Shared short code </li></ul><ul><li>shared with others </li></ul><ul><li>requires keyword like &quot;RUBY&quot; </li></ul><ul><li>Dedicated short code </li></ul><ul><li>all to yourself </li></ul><ul><li>Takes around 2 months to obtain </li></ul><ul><li>$1000 for a selected code or $500 for a random code per month </li></ul>
  27. Chewing on a Cactus <ul><li>Choose a short code (selected or random) </li></ul><ul><li>Apply by sending registration data for 3, 6 or 12 months </li></ul><ul><li>Receive approval email from CSCA </li></ul><ul><li>Pay all up front $500 per month for Random, $1000/mo for Selected </li></ul><ul><li>Apply for approval from each wireless service provider </li></ul><ul><li>Sign a contract with each provider </li></ul><ul><li>Implement and Test each connection with each provider </li></ul>Sms gateway service providers(connection aggregators) will help with this process or you can go directly through the CSCA in the US
  28. Opt out concerns <ul><li>Handled by most providers </li></ul><ul><li>Implement yourself </li></ul><ul><ul><li>Tracking </li></ul></ul><ul><ul><li>Opting out different senders </li></ul></ul>
  29. Choosing a Service Provider <ul><li>Cost: how many sms messages will you send per month? </li></ul><ul><ul><li>Credit vs. sms </li></ul></ul><ul><ul><li>Hidden costs </li></ul></ul><ul><ul><li>Monthly fee </li></ul></ul><ul><li>Coverage: does the provider cover all needed areas? </li></ul><ul><li>Implementation Time: is the API you want to use easy to integrate? </li></ul><ul><li>Opt-out: is it handled? </li></ul><ul><li>Two way messaging: is it supported? Do you need it? </li></ul><ul><li>Help with short code cactus eating experience? </li></ul>
  30. Developer Integration Concerns
  31. Save message information <ul><li>Do you need to save a history or audit trail for reporting? </li></ul><ul><li>Lots of messages to send? </li></ul><ul><li>Can't send in real time (before controller action times out?) </li></ul>
  32. Inside the SMS ORM: Check out SMS ON RAILS Draft Message Delivery By Date PhoneNumber Number WhiteList? DoNotSend? Outbound Status Time Sent Service Provider UID PhoneCarrier Name Email gateway Sender/User Username Password Favorite beer
  33. Outbound Status: Produce and Consume <ul><li>Web action creates an Outbound record with status NOT_PROCESSED </li></ul><ul><li>Cron task processes messages with status NOT_PROCESSED and marks them SUCCESS or FAIL </li></ul>Controller Action Producer Cron/Background Task Consumer NOT_PROCESSED SUCCESS
  34. Locking and Delivering: Outbound Status <ul><li>No transactions; no take backs </li></ul><ul><li>When delivering a message, try locking Outbound ORM object </li></ul>NOT_PROCESSED PROCESSING SUCCESS FAILED
  35. Bulk Message Delivery <ul><li>Use batch API call to send multiple phone numbers the same msg </li></ul><ul><li>For huge deliveries, use SMPP to maintain an open connection </li></ul><ul><li>Set delivery date and send to provider in advance </li></ul><ul><li>Lock multiple records with your database (MySQL FOR UPDATE) </li></ul><ul><li>Use Multiple servers </li></ul>
  36. Bulk Message Delivery Tips <ul><li>Reduce contention for competing servers storing data </li></ul><ul><ul><li>Assign a group of records (ID mod X srvrs) to each server </li></ul></ul><ul><ul><li>(non dedicated servers) put tasks on staggered timers (ex. every 2 min) that execute for a shorter duration(ex. 45 seconds) </li></ul></ul><ul><li>Identify fatal errors and stop the task </li></ul><ul><ul><li>Fatal, stop task: Ex. No response from service provider </li></ul></ul><ul><ul><li>Single message error: Mark failed and continue processing </li></ul></ul><ul><li>Cron to Recover messages in 'PROCESSING' state with updated_at field > duration of consumer task (ex. 10 min) </li></ul><ul><li>Delivery status background task to request delivery information </li></ul><ul><li>Set priority on messages </li></ul>
  37. Sanitize Phone Numbers <ul><li>PhoneNumber .create!(:number => '(206)-555-1212 ' ) </li></ul><ul><li>PhoneNumber .find_by_number( '12065551212' ) == nil </li></ul>Your database without data integrity
  38. Sanitize Phone Numbers <ul><li>Sanitize all phone numbers before search or insert or update </li></ul><ul><li>Store with country code (for USA auto add a 1 for 10 digit numbers not inputted with a + or 0) </li></ul>My database runs on flowers
  39. Outbound Attributes: White List and Opt Out <ul><li>Save safe numbers on a white list and enable checking to </li></ul><ul><li>prevent accidents (test mode) </li></ul><ul><li>restrict access </li></ul><ul><li>Store Opt-Out Information </li></ul><ul><li>Prevent paying for a message to a recipient provider opted out </li></ul><ul><li>Can make a distinction between opt out, opt in, and bounce, abuse </li></ul>smsi
  40. SMS Gateway Service Providers <ul><li>Short List of Providers </li></ul><ul><ul><li>http://www. redoxygen .com </li></ul></ul><ul><ul><li>http://www. redrabbitsms .com/ </li></ul></ul><ul><ul><li>http:// clickatell .com </li></ul></ul><ul><ul><li>http:// quios .com </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><li>Long list and comparison table of providers: </li></ul><ul><li> http://www. developershome .com/ sms / smsGatewayProvComp .asp </li></ul>
  41. Useful References <ul><li>General SMS: http://www. developershome .com/ sms / </li></ul><ul><li>Provider Gateway addresses: http://en. wikipedia .org/ wiki /SMS_gateway </li></ul><ul><li>Sms On Rails Engine: http:// github .com/ blythedunham / smsonrails </li></ul><ul><li>Clickatell Gem: </li></ul><ul><li>Exception Notification Plugin: </li></ul><ul><li> http:// github .com/rails/exception_notification/tree/master </li></ul><ul><li>Kannel: http://www. kannel .org/ </li></ul><ul><li>RapidSms </li></ul><ul><li>RubyGSM RubyGSM </li></ul><ul><li>More Mobile Tools: </li></ul><ul><li>Common Short Code Admin(CSCA) http://www. usshortcodes .com/ </li></ul>
  42. SMS On Rails Engine: SMS in minutes <ul><li>script/plugin install git :// github .com/ blythedunham / smsonrails </li></ul>Demo <ul><li>Support for: </li></ul><ul><li>Email Gateway Integration with phone carrier email domains </li></ul><ul><li>Clickatell HTTPS Integration </li></ul><ul><li>ORM/DB tables and migrations to store messages </li></ul><ul><li>RESTful admin views to show message history </li></ul><ul><li>Safe Locking and delivery </li></ul><ul><li>Safely store phone numbers </li></ul><ul><li>Dynamic parameter substitution </li></ul><ul><li>Not included: (yet) </li></ul><ul><li>Crons tasks or bulk message delivery </li></ul><ul><li>Stale message recovery </li></ul><ul><li>Pretty UI with rounded corners customized to your application </li></ul><ul><li>Inbound Message support </li></ul><ul><li>Please log tickets on github! </li></ul>
  43. Questions? Yo Dawg, I heard you like Rails Apps so I put an SMS Rails App in your Rails App so you can SMS yourself to be awesome
  44. Photos <ul><li>Trash can http://www. flickr .com/photos/ knittingskwerlgurl /396165625/ </li></ul><ul><li>Flowers http://www. flickr .com/photos/ blondepowers /2722511769/ </li></ul><ul><li>Timepiece: http://www. flickr .com/photos/ lebron /223223725/ </li></ul><ul><li>Bear http://www. flickr .com/photos/ guenterleitenbauer /697065143/ </li></ul><ul><li>Email http://www. flickr .com/photos/ charuca /2448685976/ </li></ul><ul><li>Golden gate: </li></ul><ul><li>Beetles: </li></ul><ul><li>Computer Cat: </li></ul><ul><li>Giant: </li></ul>