Rails 3 and Twilio to test the phone

I am building an application in Rails 3 using twilio to verify the existence of a business. Basically, when you create a new business, I randomly generate a 6-digit number, and then I call the business phone number with this confirmation code, and the user must enter it back into the system to complete the registration process. I am having trouble finding relevant examples of how to do this. I found this one , but it seems terribly outdated and does not work with Rails 3, it seems. The documentation for twilio-rb gem is also confusing.

Does anyone know of any examples or have any code samples that could point me in the right direction?

+4
source share
3 answers

As I said in a comment on your question, I am the author of the twilio-rb gem you mentioned. At the top of my head, I would use a verification resource to which you send the phone number.

POST /verifications.voice { telephone_number: '+12125551234' } 

In the create action, use Twilio::Call.create to create a new call with Twilio

 def create @verification = Verification.new params[:verification] if @verification.save Twilio::Call.create to: @verification.telephone_number, from: YOUR_CALLER_ID, url: verification_url(@verification, format: :voice) # 201 created and return verification code etc else # Handle errors end end 

You will also want to save any API errors that twilio-rb may raise. The URL refers to the show action of the validation resource instance. Then Twilio dials the phone number indicated on the phone, and when the call is connected, it asks for a URL, for example. GET /verifications/1.voice , so you will need a view view that requests a verification code and collects numbers using <Gather> verb :

 res.gather num_digits: 4, action: twilio_hack_verification_url(@verification, :format => :voice), method: 'POST' do |form| form.say 'Please enter the your 4 digit verification code' end 

Since Twilio does not currently implement the PUT verb, you must add a member to your resource

 resources :verifications do member { post 'twilio_hack' } end 

Then in your controller, update the object using user input:

 def twilio_hack @verification = Verification.find(params[:id]).tap do |v| v.user_input params['Digits'] v.save end if @verification.confirmed? # handle success else # handle failure end end 

Finally, in your model you will need a code that generates a verification code and checks to see if it is validated.

 class Verification < ActiveRecord::Base before_save -> { self[:confirmed] = true if user_input == verification_code }, if: user_input before_create -> { self[:verification_code] = rand.to_s[2..5] } end 

This is all untested from head to head, which we thought about 2 minutes, but you need to start.

+6
source

If you want to check out the business:

  • Generate a verification code.
  • Use the Twilio REST API to initiate an outgoing call by passing the URL for the callback to the controller, which will handle the validation logic. The docs in Twilio are here and an example is here .
  • This means that you need to pass the control code to the controller via the callback URL. Use an unscientific route with a bound parameter. See here .

Write a controller that processes the call and processes the validation:

  • Emit TwiML, which challenges the user to enter a verification code. I found using Nokogiri to make TwiML the easiest. (See the phone_greeting method in this simple application. I wrote: here .)
  • If this is correct, mark the business as verified, congratulate the user and hang up.
  • If not, loop.

We hope that there is enough information to point you in the right direction.

+2
source

Do you consider using Twilio Outgoing Caller ID to help solve this problem?

When you call Twilio over REST to add a new caller ID to your account, Twilio will return a 6-digit confirmation code (ValidationCode property) to display in the user interface, and then Twilio will automatically call the number and request for the code. When the user checks the phone number, their number will be added to the caller IDs. You can then request Twilio for your phone number via REST (PhoneNumber parameter) to verify that the check was successful.

See documentation here:

Add Caller ID: http://www.twilio.com/docs/api/rest/outgoing-caller-ids#list-post

Find Caller ID: http://www.twilio.com/docs/api/rest/outgoing-caller-ids#list

+1
source

All Articles