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

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

25 Jan 2017
0 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 the minimal development effort.

Here Twilio, is a cloud communications platform. We can able to use it in various ways. It is especially 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 from the developer's perspective which brings 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, Let's start with some programming language through which we can use it easily. The following steps with the description implement 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 attributes that we will use in our rails application, and that is., 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 shows only two fields, that are., Message text box and Sends 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 is., twilio_account_sid, twilio_auth_token, twilio_number.

STEP 7. Just below the initialization of the parameters we will create the rest objects 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: the 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. In this way, you can easily implement an SMS service using a 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: