iCoreTech Research Labs Just a placeholder

20Aug/090

Send SMS with Ruby on Rails

SMSOnce upon a time embedding the chance to send SMS withing an app was daunting, no more.

We are going to use Ruby on Rails Edge (git version, but should work on stable too) and the excellent SmsOnRails plugin, along with Clickatell SMS provider.

Open your project with your favourite text editor and edit config/environment.rb by adding gem requirement:

1
2
config.gem 'blythedunham-sms_on_rails', :lib => 'sms_on_rails', :source  => 'http://gems.github.com'
config.gem 'clickatell' # if using Clickatell

Install the gems:

sudo rake gems:install

Or if you're not going to use gems, install as plugin:

script/plugin install git://github.com/blythedunham/smsonrails.git

The plugin itself is complete with generators, for both models and migrations, I will not show them here (even because rails edge still cannot handle gem generators as of this writing), instead I will show you how to send sms quickly and easily.

We are going to use one specific provider for sending our sms, it's Clickatell.
Therefore go to their website and open an account to use Clickatell Central API.
Account starts with a balance of 10 SMS, on which you cannot add custom text, but once you buy a proper package those limitations wears off (of course).

After you open up your account, proceed in buying sms at your liking, after that click on My Connections.

From here you can add a new HTTP connection by clicking Add Connection select box - HTTP.
You can leave the fields blank in the form that will appear on page reload, but for extra security you better edit them soonish.

When you add a new connection, it will get assigned a API ID, most likely it's numeric.

Back to our plugin configuration, keep in mind that SmsOnRails also inherits settings from ActionMailer, so it can send also emails instead of SMS.

Create a new file called sms.rb in config/initializers directory and write this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SmsOnRails::ServiceProviders::Clickatell.config =
{
   :api_id => 'numeric_api_id',
   :user_name => 'your_username',
   :password => 'your_password'
}
 
 
SmsOnRails::ServiceProviders::EmailGateway.config =
{
   :sender => 'no-reply@domain.com',
   :subject => 'Default Subject Text'
   #:bcc => nil,
   #:mailer_klass => nil
}
 
 
SmsOnRails::ServiceProviders::Base.set_default_service_provider :clickatell

Replace api_id, user_name and password with your data.
As you can see we set our provider as Clickatell.

There are different methods for sending a SMS, and service providers are singletons and can be accessed by their instance.

Therefore:

1
2
3
phone_number = '392848676093485693' # not a real number
message_text = 'hi'
SmsOnRails::ServiceProviders::Clickatell.instance.send_message(phone_number, message_text)

That's it. Easy as pie.
SmsOnRails is a robust plugin, pings the clickatell service to keep the connection alive.
I have yet to find out a way to show a custom sender ID, but for now, it's fine and works well.

Advanced configurations can be seen at the github project page.

Related posts:

  1. Rails vs. Tarantula
  2. jGrowl on Rails (with i18n)
  3. My Rails .gitignore
  4. Ruby ordered hash tip with Rails
  5. Install MySQL and MySQL Ruby gem on Snow Leopard, 64 bit

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.

Additional comments powered by BackType