Integration of twilio into rails 4

After some trial and error and much fumbling through online documentation, I managed to get the text notification functionality integrated into my Rails app.
Here is a quick summary of the steps I took to get the Twilio API working. I hope that this document will help other developers navigate through the setup process much quicker than I did.

Step 1 – Create a Twilio Dev Account

You will need a Twilio test account to test your application. There is no charge for the test account and you get a free number to validate your voice or texting application.
The account setup process is very straightforward and is explained very well in this video. The speaker does a thorough job walking you through the setup.

You can choose the area code for your test number (and avoid long distance charges). You can even choose a number based on a set of letters.
You will get an API SID and authorization token to configure your account in your Rails application. I used the Figaro gem to protect the data. Figaro places your credentials inside the application.yml file and keeps that file from being posted on GitHub.
You can also control the Request URL and set the http GET and POST settings for the number. This is done inside your Twilio account under Numbers -> Twilio Numbers

Step 2 – Texting Out With Twilio From the App

 Texting from twilio is the basic and the easiest task to complete. To do this you
 need to first create a form in your web which can take some text and a number which should be registered for twilio trial account.
 
2.1 Create a form :
<%= form_for @twilio, url: send_text_message_path, method: :post do |f|%>
    <%= f.label 'text_message' %>
    <%= f.text_field :message %>
    <%= f.submit 'Send'%>
<% end %>

2.2 Create a controller to handle for form get and post :
class TwiliosController < ApplicationController  skip_before_filter :verfiy_authenticity_token  before_filter :create_twilio_credentials
  def send_text_message    @twilio_client.account.sms.messages.create(
        :from => 'twilio_number',
        :to => 'number_you_have_registered',
        :body => params[:twilio_mob][:message]
    )
    redirect_to :back  end
  def write_text
    @twilio =TwilioMob.new
  end
  private  def create_twilio_credentials    @twilio_client = Twilio::REST::Client.new("sid", "token")
  endend

2.3 Create routes :

get 'write_text',to: 'twilios#write_text', as: :twilio_text

post 'send_text_message',to: 'twilios#send_text_message', as: :send_text_message


Now you are ready to go for your text message submission I have just used body because I don't need to send the message to anyone else


 Now when you submit your text it will be on your mobile.

Thanks for reading this blog...









Comments

Popular posts from this blog

Installing Wowza Streaming Engine on ubuntu

Highcharts with grouped categories

Completely Uninstall and Install Rubymine on ubuntu