How to Click To Call Back With Tropo and Ruby on Rails

In this post, we’re going to build on our previous work on number masking. Let’s say you’ve created a mask for your number and someone called that mask. Now you want to call that person back. How do you do so without revealing your actual number?

Here’s how. Once again, we’ll be focusing on the Tropo back-end side of things.

 

Steps:

  1. Create a model for saving call history.
  2. Create a view that shows this history and allows you to click on a number, triggering call_back
  3. Have Tropo call you and add you to a unique conference
  4. Have Tropo call the other person and add them to the same conference

 

Reminder: number is your actual phone number, while a mask is the phone number that is made publicly available. Calls to the mask get transferred to your actual number, keeping it anonymous.

 

Create a model for saving call history and a view for triggering the call back

We want to keep track of all calls made to a mask. To achieve this, we create an inbound_calls model that stores the number of the person calling and the time that the person called. An inbound_call belongs to a mask, which allows us to also keep track of the mask that was called and the number associated with that mask. Remember from our previous blog that a mask belongs to a number. 

Here is example code that stores an inbound call before making the transfer.

def transfer

    from = params[:session][:from][:id]
    to = params[:session][:to][:id]
    timestamp = params[:session][:timestamp]

    mask = Mask.find_by mask: Phonelib.parse(to).e164
    number = mask.number.phone_number

    inbound_call = InboundCall.new(from: from, mask: mask, time: timestamp)
    if !inbound_call.save 
        puts "Creating inbound call failed: #{inbound_call.errors.to_json}"
    end

    t = Tropo::Generator.new  
    t.transfer(:to => number, :from => from)

    render :json => (t.response)
end

 

Note that Tropo provides the timestamp of the call in the session timestamp parameter. Here is an example view for triggering the call backs.

inbound_call

 

 

Have Tropo call and add people to a conference

def call_back(from, mask, number)
    conference_id = from.to_s + mask.to_s + rand(1000).to_s
    tell_tropo_to_invite_to_conference(conference_id, from, mask)
    tell_tropo_to_invite_to_conference(conference_id, number, mask)
end
def tell_tropo_to_invite_to_conference(conference_id, to, from) 
    token = APP_CONFIG['TROPO_CONFERENCE_INVITE_APP_TOKEN'] 
    tropo_params = {action: "create", conference_id: conference_id, to: to, from: from, token: token} 
    
    response = tell_tropo(tropo_params) 
    if response.code === '200' 
        puts "successfully invited #{to}" 
    else 
        puts "error: #{response.body}" 
    end 
end 

def invite_to_conference 
    from = params[:session][:parameters][:from] 
    to = params[:session][:parameters][:to] 
    conference_id = params[:session][:parameters][:conference_id] 
    puts "Inviting #{to} to conference #{conference_id} with callerid #{from}" 

    t = Tropo::Generator.new 
    t.call(to: to, from: from) 
    t.conference({ name: conference_id, id: conference_id}) 

    render json: t.response 
end 

def tell_tropo(tropo_params) 
    uri = URI.parse('https://api.tropo.com/1.0/sessions')   
    uri.query = URI.encode_www_form( tropo_params ) 
    http = Net::HTTP.new(uri.host, uri.port) 
    http.use_ssl = (uri.scheme == "https") 
    request = Net::HTTP::Get.new(uri.request_uri) 
    return http.request(request) 
end


These methods are in the controller. The call_back method starts the whole process. The from is the number of the person we’re calling back. The mask is the mask that the person called. And the number is the number associated with the mask, i.e., your number. We will use the conference_id to invite people to the conference. We create a unique conference_id each time the call_back method is ran, or else every one will be added to the same conference.

The tell_tropo_to_invite_to_conference method tells Tropo to start the Tropo application that we created for inviting people to conferences. The script link we provided in that app points to our invite_to_conference method. The invite_to_conference method calls in a person to the conference_id provided. We pass the conference_id and other parameters to the invite_to_conference method by including the parameters as part of the query. We then access these parameters in the invite_to_conference method via params[:session][:parameters]. Notice that when calling the other person, the callerId is set to the mask, keeping your number anonymous 🙂

The tell_tropo method simply makes a GET request to Tropo with the query parameters that are passed in.

Here are the Tropo docs for making a call, setting up a conference, and passing parameters:
https://www.tropo.com/docs/scripting/call
https://www.tropo.com/docs/webapi/quickstarts/controlling-calls/creating-conference-call
https://www.tropo.com/docs/scripting/quickstarts/making-call/passing-parameters

 

And that’s it! You can now call back people without revealing your phone number.