Skip to content

Instantly share code, notes, and snippets.

@lambtron
Last active December 19, 2015 04:38
Show Gist options
  • Save lambtron/5898201 to your computer and use it in GitHub Desktop.
Save lambtron/5898201 to your computer and use it in GitHub Desktop.
Twilio Click to call in the inbox controller
require 'twilio-ruby'
class InboxController < ApplicationController
BASE_URL = 'http://www.andyjiang.com/'
# /click-to-call-request
def click_to_call(calling_to)
# debugging purposes.
twilio_number = '4154444444'
calling_to = digits_only(calling_to)
agent_phone = '5555555555'
caller_id = agent_phone
# When we call the agent (andy + our apartment), we set the caller ID to the party we're eventually
# calling. Why? So you could call them back from normal cellphone later.
begin
@client = Twilio::REST::Client.new ACCOUNT_SID, AUTH_TOKEN
@client.account.calls.create(
:from => twilio_number,
:to => agent_phone,
:url => BASE_URL + '/click-to-call-callscreen?calling_to=' + calling_to + '&caller_id=' + caller_id
)
puts "click to call initiated."
rescue
400
end
render 'app/views/inbox/sfapartments.html', :formats => [:html]
end
# /click-to-call-callscreen
# This handler receives an HTTP callback from Twilio once the agent has picked up
# their phone. It first verifies that they're ready to receive the call (to avoid
# voicemail) and then defers to the final twiml URL.
def click_to_call_callscreen
@post_to = BASE_URL + '/click-to-call-twiml?calling_to=' + params['calling_to'] + '&caller_id=' + params['caller_id']
render :action => 'clicktocallcallscreen.xml.builder', :layout => false
end
# /click-to-call-twiml
# This handler is fired if the callscreen detected a digit
def click_to_call_twiml
@calling_to = params['calling_to']
@caller_id = params['caller_id']
render :action => 'clicktocall.xml.builder', :layout => false
end
def digits_only(string)
return string.gsub(/[^[0-9]]/, "")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment