Sending One Time Password via SMS Using Rails3 and SMSAPI


Hi friends,

My need was send one time password via e-mail/sms ,who registered with their E-mail/mobile no.

Lets see “How to send SMS using Rails3 application and smsAPI”

At first we need a smsapi to send SMS via Rails3 application. There is a site, which provides FREE API to send SMS via programming. So, here we need to register with your Domain and mail-id. They also provide codes for various languages 🙂

EX code for RUBY:

require 'net/http'
require 'uri'
Net::HTTP.get_print URI.parse('http://s1.freesmsapi.com/messages/send?skey=8c0185641435ac8a43cf10230a1417ac&message=YOUR_MESSAGE&senderid=YOUR_SENDERID&recipient=MOBILE_NUMBER')

We just modify the code, what ever our application needs,

require 'net/http'
require 'uri'
pass = "Hi+This+is+ur+one+time+passwd:" + rand(999999).to_s
mobile = ARGV.shift
url = "http://s1.freesmsapi.com/messages/send?skey=9600152989freesmsaPI&message=#{pass}&senderid=slash&recipient=#{mobile}"
Net::HTTP.get_print URI.parse(url)
Then create a new rails project using,
  $ rails new sms
Go to project directory using,
  $ cd sms
Create scoffold using,
  $ rails generate scaffold name:string phone:string
Now, we have to edit the user controller
  $ gedit /app/controller/users_controller.rb
# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])

smspasswd

respond_to do |format|
if @user.save
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
format.xml  { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
Now, we have to write a small def function into that “usres_controller.rb” file
# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])

smspasswd  #Note this Function

respond_to do |format|
if @user.save
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
format.xml  { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

def smspasswd
name = @user.name
pass = "Hi+#{name},+This+is+ur+one+time+passwd:" + rand(999999).to_s
phone = @user.phone
url = "http://s1.freesmsapi.com/messages/send?skey=8c0185641435ac8a43cf10230a1417ac&message=#{pass}&senderid=slash&recipient=#{phone}"
Net::HTTP.get_print URI.parse(url)

end
That’s all
Sample Application: http://send-sms.heroku.com
Download from GitHub link: https://github.com/thasuresh/sms
Note:
      But, This “freesmsapi” does not work with the numbers who activated DND support. 😦
Advertisements

7 thoughts on “Sending One Time Password via SMS Using Rails3 and SMSAPI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s