railsconf2009 integrating sms with your rails app

44
Integrating SMS Messaging with your Rails Application Blythe Dunham [email protected] spongecell .com / snowgiraffe .com

Upload: blythe-dunham

Post on 22-Jan-2015

8.772 views

Category:

Technology


1 download

DESCRIPTION

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 http://github.com/blythedunham/smsonrails

TRANSCRIPT

Page 1: RailsConf2009 Integrating SMS with your Rails App

Integrating SMS Messaging with your Rails Application

Blythe [email protected]

spongecell.com / snowgiraffe.com

Page 2: RailsConf2009 Integrating SMS with your Rails App

Why SMS?Oldest most reliable means to communicate with mobile users

Cell phone agnostic

Everybody is doing it

Great way to receive emergency messages at the bar

QuickTime™ and a decompressor

are needed to see this picture.

SERVER IS DOWNSPARKS ARE UP

Page 3: RailsConf2009 Integrating SMS with your Rails App

Popular Applications to Send SMSStandard Email

yournumber@gatewayaddr Example:[email protected]

Find your provider gateway

Instant Message

AOL (US only)

Skype (small fee)

Other

Teleflip RIP

remindblast.com - Send SMS reminders to be awesome or do laundry

Facebook SMS app

Cell providers web page (ex. http://www.t-mobile.com)

Page 4: RailsConf2009 Integrating SMS with your Rails App

Overview: One Way MessagingMobile Terminated (MT) or Outbound

Black Box ofYour application &

gateway & other

Party at A1A Beach front Ave

Mobile Terminated

(MT)

Page 5: RailsConf2009 Integrating SMS with your Rails App

Overview: Two Way Messaging

Black Box ofYour application &

gateway & other

ROCK

MO

Respond with "ROCK" for Free party tix

MT

Mobile Originated (MO) or Inbound

Page 6: RailsConf2009 Integrating SMS with your Rails App

Simplest approach: SMS as email

Page 7: RailsConf2009 Integrating SMS with your Rails App

Pros and Cons of sending SMS through email

The Good

• Free to application (end-user with phone might pay)

• Easy to integrate. Only email setup required.

• Excellent for use with known numbers

• Emergency Messages with plugins like Exception Notification

Page 8: RailsConf2009 Integrating SMS with your Rails App

Pros and Cons of sending SMS through email

The Tricky

• User carrier must be known

• additional user input

• storage considerations

• No delivery confirmation

• Supported by most US but not all cell phone carriers

• No two way messaging

Page 9: RailsConf2009 Integrating SMS with your Rails App

Giant Black Box Explained: SMSCs, SMS Gateways, Service Providers

Before exploring the other available SMS options, lets look at how SMS works

Page 10: RailsConf2009 Integrating SMS with your Rails App

SMS Centers (SMSC)

SMSC SMSCSMSC

Page 11: RailsConf2009 Integrating SMS with your Rails App

GSM Modem

SuperApp

GSMModem

(Cell Phone)

SMSC

AT Commands

Page 12: RailsConf2009 Integrating SMS with your Rails App

GSM Modem APIs and Applications• Kannel in C (open source) supports an API

• RubyGSM - Written by Unicef for RapidSMS to inexpensively send text messages

Page 13: RailsConf2009 Integrating SMS with your Rails App

GSM Modem LoDownAdvantages

Cheap

Can use almost anywhere

Two way messaging support

Disadvantages

Difficult to setup

Slow

Doesn’t scale (only 6 to 10 messages per second)

Must connect modem to a server

Page 14: RailsConf2009 Integrating SMS with your Rails App

Talk directly to all SMS Centers

SMSC SMSCSMSC

AwesomeApp

Page 15: RailsConf2009 Integrating SMS with your Rails App

Mission Impossible: Coding for each SMSCLarge number of SMS Centers

Each use different and proprietary communication protocols

Don’t do this unless you are me!

Page 16: RailsConf2009 Integrating SMS with your Rails App

SMS Gateway translates SMSC protocols

SMSC SMSCSMSC

RockstarApp

SMS Gateway

Page 17: RailsConf2009 Integrating SMS with your Rails App

SMS GatewaysFree Open Source Gateways

Kannel - written in C supports many SMSCs and also acts as a WAP gateway

Must setup/configure on a server yourself

Page 18: RailsConf2009 Integrating SMS with your Rails App

Pay the middleman!• Charge for sending messages

• ~ 5 cents per message

• Bulk discounts

• Provide convenient APIs

• Most provide two way messaging

• Can help obtain short code

Page 19: RailsConf2009 Integrating SMS with your Rails App

SMS Gateway: Sending Outbound (MT)Typical approach for Outbound messages is simple:

• Create an account

• Buy credits (or messages)

• Start sending messages through the API

Page 20: RailsConf2009 Integrating SMS with your Rails App

Ruby Outbound (MT) message with HTTPS

http = Net::HTTP.new(CLICKATELL_BASE_URL, 443)

http.use_ssl = true

http.enable_post_connection_check = true

http.start {response = http.request_get(url_string)}

Page 21: RailsConf2009 Integrating SMS with your Rails App

Choosing an API (if you have a choice)• HTTP/S (GET & POST)

- By far the easiest to implement- Great for sending < 10k messages a month

• SMPP- Best for bulk messages (over 10k per month)- Leaves Connection open

• STMP - send through mail

Other options (not so hot)• FTP - Bulk option for legacy systems• XML (use HTTP if available)• SOAP (use HTTP if available)• Com Object (Seriously? What wah?)

Page 22: RailsConf2009 Integrating SMS with your Rails App

SMS Gateway: Two way messaging and MOs1. Typically enable Outbound messaging

2. Obtain or rent a short code

3. Respond to Inbound MOs

1. Use a provider campaign management tool ($$$)

2. Parse yourself and write some regexp!

/^OPT(\s*[-_]?\s*)OUT|STOP|END|QUIT|UNSUBSCRIBE|EXIT|HALT|FUCK(\s*[-_]?\s*)OFF|REMOVE|CANCEL)/i

Page 23: RailsConf2009 Integrating SMS with your Rails App

Two way messaging: PullSetup a cron task to pull new messages every few seconds.

Page 24: RailsConf2009 Integrating SMS with your Rails App

Two way messaging: PushProvide a callback URL that service provider invokes for each new

message

Page 25: RailsConf2009 Integrating SMS with your Rails App

Do I need a short code?

If your awesome app is:

• A giant spam machine• Implementing two way messaging• (Recommended) Sending > 5000

messages per month

Page 26: RailsConf2009 Integrating SMS with your Rails App

Shared (Rented) Versus Dedicated Short Codes

Shared short code shared with othersrequires keyword like "RUBY"

Dedicated short codeall to yourselfTakes around 2 months to obtain$1000 for a selected code or $500 for a random code per month

Page 27: RailsConf2009 Integrating SMS with your Rails App

Chewing on a Cactus

1. Choose a short code (selected or random)2. Apply by sending registration data for 3, 6 or 12 months3. Receive approval email from CSCA4. Pay all up front $500 per month for Random, $1000/mo for Selected5. Apply for approval from each wireless service provider6. Sign a contract with each provider7. Implement and Test each connection with each provider

Sms gateway service providers(connection aggregators) will help with this process or you can go directly through the CSCA in the US

Page 28: RailsConf2009 Integrating SMS with your Rails App

Opt out concerns• Handled by most providers

• Implement yourself

- Tracking

- Opting out different senders

Page 29: RailsConf2009 Integrating SMS with your Rails App

Choosing a Service ProviderCost: how many sms messages will you send per month?

Credit vs. sms

Hidden costs

Monthly fee

Coverage: does the provider cover all needed areas?

Implementation Time: is the API you want to use easy to integrate?

Opt-out: is it handled?

Two way messaging: is it supported? Do you need it?

Help with short code cactus eating experience?

Page 30: RailsConf2009 Integrating SMS with your Rails App

Developer Integration Concerns

Page 31: RailsConf2009 Integrating SMS with your Rails App

Save message informationDo you need to save a history or audit trail for reporting?

Lots of messages to send?

Can't send in real time (before controller action times out?)

Page 32: RailsConf2009 Integrating SMS with your Rails App

Inside the SMS ORM: Check out SMS ON RAILS

DraftMessage

Delivery By Date

PhoneNumberNumber

WhiteList?DoNotSend?

PhoneCarrierName

Email gateway

Sender/UserUsernamePassword

Favorite beer

OutboundStatus

Time SentService Provider UID

Page 33: RailsConf2009 Integrating SMS with your Rails App

Outbound Status: Produce and Consume• Web action creates an Outbound record with status

NOT_PROCESSED

• Cron task processes messages with status NOT_PROCESSED and marks them SUCCESS or FAIL

ControllerAction

Producer

Cron/Background Task

Consumer

NOT_PROCESSED

SUCCESS

Page 34: RailsConf2009 Integrating SMS with your Rails App

Locking and Delivering: Outbound StatusNo transactions; no take backs

When delivering a message, try locking Outbound ORM object

NOT_PROCESSED PROCESSING

SUCCESS

FAILED

Page 35: RailsConf2009 Integrating SMS with your Rails App

Bulk Message Delivery • Use batch API call to send multiple phone numbers the same msg

• For huge deliveries, use SMPP to maintain an open connection

• Set delivery date and send to provider in advance

• Lock multiple records with your database (MySQL FOR UPDATE)

• Use Multiple servers

Page 36: RailsConf2009 Integrating SMS with your Rails App

Bulk Message Delivery Tips• Reduce contention for competing servers storing data

• Assign a group of records (ID mod X srvrs) to each server

• (non dedicated servers) put tasks on staggered timers (ex. every 2 min) that execute for a shorter duration(ex. 45 seconds)

• Identify fatal errors and stop the task

• Fatal, stop task: Ex. No response from service provider

• Single message error: Mark failed and continue processing

• Cron to Recover messages in 'PROCESSING' state with updated_at field > duration of consumer task (ex. 10 min)

• Delivery status background task to request delivery information

• Set priority on messages

Page 37: RailsConf2009 Integrating SMS with your Rails App

Sanitize Phone NumbersPhoneNumber.create!(:number => '(206)-555-1212 ')

PhoneNumber.find_by_number('12065551212') == nil

Your database without data integrity

Page 38: RailsConf2009 Integrating SMS with your Rails App

Sanitize Phone NumbersSanitize all phone numbers before

search or insert or update

Store with country code (for USA auto add a 1 for 10 digit numbers not inputted with a + or 0)

My database runs on flowers

Page 39: RailsConf2009 Integrating SMS with your Rails App

Outbound Attributes: White List and Opt OutSave safe numbers on a white list and enable checking to

• prevent accidents (test mode)

• restrict access

Store Opt-Out Information

• Prevent paying for a message to a recipient provider opted out

• Can make a distinction between opt out, opt in, and bounce, abuse

smsi

Page 40: RailsConf2009 Integrating SMS with your Rails App

SMS Gateway Service Providers

Short List of Providers

http://www.redoxygen.com

http://www.redrabbitsms.com/

http://clickatell.com

http://quios.com

http://messagepub.com/

http://www.openmarMeket.com/products-and-services/message-and-content-delivery.html

http://www.mobilemarketing.net

http://www.textmarks.com/

http://www.mozes.com/

Long list and comparison table of providers:

http://www.developershome.com/sms/smsGatewayProvComp.asp

Page 41: RailsConf2009 Integrating SMS with your Rails App

Useful ReferencesGeneral SMS: http://www.developershome.com/sms/

Provider Gateway addresses: http://en.wikipedia.org/wiki/SMS_gateway

Sms On Rails Engine: http://github.com/blythedunham/smsonrails

Clickatell Gem: http://clickatell.rubyforge.org/

Exception Notification Plugin:

http://github.com/rails/exception_notification/tree/master

Kannel: http://www.kannel.org/

RapidSms http://mobileactive.org/tagging/rapid-sms

RubyGSM http://mobilehacking.org/index.php/RubyGSM

More Mobile Tools: http://mobilehacking.org/index.php/Main_Page

Common Short Code Admin(CSCA) http://www.usshortcodes.com/

Page 42: RailsConf2009 Integrating SMS with your Rails App

DemoSMS On Rails Engine: SMS in minutesscript/plugin install git://github.com/blythedunham/smsonrails

Support for:

• Email Gateway Integration with phone carrier email domains

• Clickatell HTTPS Integration

• ORM/DB tables and migrations to store messages

• RESTful admin views to show message history

• Safe Locking and delivery

• Safely store phone numbers

• Dynamic parameter substitution

Not included: (yet)

• Crons tasks or bulk message delivery

• Stale message recovery

• Pretty UI with rounded corners customized to your application

• Inbound Message support

Please log tickets on github!

Page 43: RailsConf2009 Integrating SMS with your Rails App

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

Page 44: RailsConf2009 Integrating SMS with your Rails App

PhotosTrash can http://www.flickr.com/photos/knittingskwerlgurl/396165625/

Flowers http://www.flickr.com/photos/blondepowers/2722511769/

Timepiece: http://www.flickr.com/photos/lebron/223223725/

Bear http://www.flickr.com/photos/guenterleitenbauer/697065143/

Email http://www.flickr.com/photos/charuca/2448685976/

Golden gate: http://www.flickr.com/photos/h2orange/63606734/

Beetles: http://www.flickr.com/photos/lhtang2006/3490285407/

Computer Cat: http://www.flickr.com/photos/sammrowe/3491788169/

Giant: http://www.flickr.com/photos/andreatallone/2841979961/