All-in-One Messaging Toolbox for Ruby on Rails Developers

25 Jan 2017
17
Comments

All-in-One Messaging Toolbox for Ruby on Rails Developers

Give your web and mobile apps the power to exchange messages of any variety, from chat to SMS. You get global reach, delivery intelligence and the highest reliability over both IP and carrier networks, all with minimal development effort.

Here Twilio, is a cloud communications platform. We can able to use it in a various ways. It is specially available for the developers to get interact with the communication resources like., SMS, MMS, voice or video calls.

Twilio platform is available not only for ruby language but also for many kinds of programming languages like., C#.Net, Java, Python, PHP, Node, Android etc. The availability of the service is really good and easy for the developers perspective which bring something good for the developing application.

Using Twilio we can integrate SMS, MMS, or Voice and Video service in our developing application. It is just a couple of integration steps through which we can use the described services easily.

So, Lets start with the some programming language through which we can use it easily. The following steps with the description implements simple messaging service in our ruby on rails web application easily.

STEP 1.

For implementing SMS service into our rails application by twilio we have to register freely onto https://www.twilio.com. Just after registration we will get the the attributes which we will use into our rails application and that are., TWILIO_AUTH_TOKEN, TWILIO_ACCOUNT_SID, TWILIO_NUMBER.

STEP 2.

Create a simple Rails application using “rails new ” .

STEP 3. gem install twilio-ruby

STEP 4. For the use of SMS service create a controller “Messaging_using_twilio”.

STEP 5. Create a method send_sms into the controller as well as we have to create a view which show only two fields, that are., Message text box and Send button.

STEP 6. Into the send_sms method we need to set three parameters which are we got from twilio account for the service use, that are., twilio_account_sid, twilio_auth_token, twilio_number.

STEP 7. Just below the parameters initialization we will create rest object of the twilio or instantiate Twilio::REST::Client To access Twilio REST resources. The Twilio::REST::Client needs your Twilio credentials(twilio_account_sid, twilio_auth_token, twilio_number). It will return twilio REST resources.

STEP 8. Then just after that we can call the method messages.create() which require :from, :to and :body parameters. The :from is our twilio contact number, :to is our registered contact number (or any contact number if :from number is a paid/ purchased) and :body is nothing but our string message.

STEP 9. The twilio-ruby library will throw a Twilio::REST::RequestError. 400-level errors are normal during API operation (“Invalid number”, “Cannot deliver SMS to that number”, for example) and should be handled appropriately. Through out this way you can easily implement sms service using twilio trial account.

Example

def send_sms

ENV['TWILIO_ACCOUNT_SID'] = "AC48df98e647aeaasdasAASD232bbc26863"

ENV['TWILIO_AUTH_TOKEN'] = "5819a96aed8298f232130f212137b5"

ENV['TWILIO_NUMBER'] = "+11159878489"

begin

client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'],

ENV['TWILIO_AUTH_TOKEN']

response = client.messages.create(

from: ENV['TWILIO_NUMBER'],

to: params[:phone_no],

body: "Welcome to Twilio SMS service.")

render json: {:response => response}, status: :ok

rescue Twilio::REST::RequestError => e

render json: {:errmsg => e.message}, status: :ok

end

end



Leave a comment:

Recent Blogs
Contact Us