Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sending SMS using Twilio: Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4
    • 0
    • 730
    Comment on it

    Send SMS using Twilio: Rails

    Twilio is a SMS gateway service that lets you send and receive SMS using your application. For testing purpose, it allows you to send SMS for free to few numbers. To start integrating twilio, you first need to sign up for twilio account from http://twilio.com/. The sign up form will look like this.

    After sign up it asks you to verify your mobile number and once you verify your mobile number by just entering the PIN [sent to your registered number]. Your account comes into active state. In the next step twilio provides you a unique twilio number which is required for sending any kind of messages from your twilio account. You can purchase multiple twilio numbers from the same account.
    In your account section it also gives you API credentials, that are ACCOUNT SID and AUTH TOKEN, which are required for api calls.




    To use twilio with Ruby on Rails, we use twilio-ruby gem. To use it, first add twilio-ruby to your Gemfile.

     

    	gem 'twilio-ruby', '~> 4.11.1'
    

    Now Run the bundle and require it to your application.rb.

    	require 'twilio-ruby'
    

    To send send SMS in ruby using twilio you first need to configure the Twilio Client. You can do it in initializer or you can configure it dynamically when the send message call comes.

    	## API Keys from Twilio Account
    	account_sid = 'ACaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    	auth_token = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
    
    	## To configure Client
    	Twilio.configure do |config|
    	  config.account_sid = account_sid
    	  config.auth_token = auth_token
    	end
    
    	## and then you can create a new client without parameters
    	@client = Twilio::REST::Client.new
    

    Now to send SMS write the below code:

    	@client.messages.create(
    		from: '+16854685454', ## Your Twilio Number
    		to: '+19789898787',   ## Receiver's Mumber
    		body: 'Hi This is from Twilio',
    		media_url: 'http://media-url' ## Optional in case of MMS
    	)
    


    Hope you liked it. For more blogs like this. Click here.

    Sending SMS using Twilio: Rails

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: