Search code examples
ruby-on-railsruby-on-rails-3twilioverificationphone-call

Rails 3 and Twilio to do phone verification


I am building an app in Rails 3, using twilio to verify a businesses existance. Basically, when you create a new buisiness I randomly generate a 6 digit number and then call the business phone number with this verification code and the user needs to enter it back in the system to finish the signup process. I am having trouble finding any relevant examples as to how to get this set up. I've found this, but it seems horribly outdated and doesn't work with Rails 3 seemingly. The documentation for the twilio-rb gem is confusing as well.

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


Solution

  • As I said in the comment on your question itself, I am the author of the twilio-rb gem you mention. Off the top of my head, I would implement a verifications resource that you post a telephone number to.

    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 rescue any API errors that twilio-rb might raise. The url refers to the show action of the verification resource instance. Twilio will then dial the supplied telephone number, and when the call is connected will request the url, e.g. GET /verifications/1.voice so you'll need a show view that asks for the verification code and collects the digits with the <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 currently does not implement the PUT verb, you'll to add a member to your resource

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

    Then in your controller update the object with the 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'll need code that generates the verification code, and verifies if it is confirmed

    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 and off the top of my head with about 2 minutes thought, but it should get you started.