Class: Telephony::Providers::TwilioProvider
- Inherits:
-
Object
- Object
- Telephony::Providers::TwilioProvider
show all
- Includes:
- NumberHelper
- Defined in:
- lib/telephony/providers/twilio_provider.rb
Constant Summary
collapse
- RING_TIMEOUT_IN_SECONDS =
60
- RING_TRANSFER_TIMEOUT_IN_SECONDS =
15
- REQUEST_TIMEOUT_IN_SECONDS =
6
Instance Attribute Summary collapse
Instance Method Summary
collapse
#extract_area_code, #normalize_number
Constructor Details
Returns a new instance of TwilioProvider.
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/telephony/providers/twilio_provider.rb', line 18
def initialize(config)
self.account_sid = config[:account_sid]
self.auth_token = config[:auth_token]
self.outbound_caller_id = config[:outbound_caller_id]
self.callback_root = config[:callback_root]
self.voice_url = config[:voice_url]
self.sms_application_sid = config[:sms_application_sid]
self.twilio_provider_url = "#{callback_root}/providers/twilio/calls"
@incoming_phone_numbers_cache = {}
end
|
Instance Attribute Details
#account_sid ⇒ Object
Returns the value of attribute account_sid.
10
11
12
|
# File 'lib/telephony/providers/twilio_provider.rb', line 10
def account_sid
@account_sid
end
|
#auth_token ⇒ Object
Returns the value of attribute auth_token.
10
11
12
|
# File 'lib/telephony/providers/twilio_provider.rb', line 10
def auth_token
@auth_token
end
|
#callback_root ⇒ Object
Returns the value of attribute callback_root.
10
11
12
|
# File 'lib/telephony/providers/twilio_provider.rb', line 10
def callback_root
@callback_root
end
|
#outbound_caller_id ⇒ Object
Returns the value of attribute outbound_caller_id.
10
11
12
|
# File 'lib/telephony/providers/twilio_provider.rb', line 10
def outbound_caller_id
@outbound_caller_id
end
|
#sms_application_sid ⇒ Object
Returns the value of attribute sms_application_sid.
10
11
12
|
# File 'lib/telephony/providers/twilio_provider.rb', line 10
def sms_application_sid
@sms_application_sid
end
|
#twilio_provider_url ⇒ Object
Returns the value of attribute twilio_provider_url.
10
11
12
|
# File 'lib/telephony/providers/twilio_provider.rb', line 10
def twilio_provider_url
@twilio_provider_url
end
|
#voice_url ⇒ Object
Returns the value of attribute voice_url.
10
11
12
|
# File 'lib/telephony/providers/twilio_provider.rb', line 10
def voice_url
@voice_url
end
|
Instance Method Details
#buy_number_for_area_code(area_code) ⇒ Object
76
77
78
79
80
81
82
83
|
# File 'lib/telephony/providers/twilio_provider.rb', line 76
def buy_number_for_area_code(area_code)
number = buy_number(area_code)
@incoming_phone_numbers_cache[area_code] = number
number
rescue Exception => error
Rails.logger.info "Failed attempt to buy a phone number in area code '#{area_code}': #{error.message}"
nil
end
|
#call(call_id, destination, caller_id) ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/telephony/providers/twilio_provider.rb', line 29
def call(call_id, destination, caller_id)
create_call number: destination,
caller_id: caller_id,
callback_url: "#{twilio_provider_url}/#{call_id}/connect",
call_id: call_id,
timeout: RING_TIMEOUT_IN_SECONDS
end
|
#call_ended?(sid) ⇒ Boolean
89
90
91
92
93
94
95
96
97
|
# File 'lib/telephony/providers/twilio_provider.rb', line 89
def call_ended?(sid)
call = client.account.calls.find sid
call.status.in?(%w(completed failed busy no-answer))
rescue ::Twilio::REST::RequestError, NoMethodError
false
rescue => error
log_error(error, "Error when verifying if call ended")
false
end
|
#caller_id_for(area_code) ⇒ Object
69
70
71
72
73
74
|
# File 'lib/telephony/providers/twilio_provider.rb', line 69
def caller_id_for(area_code)
incoming_phone_number_from_cache(area_code) ||
fetch_existing_incoming_phone_numbers(area_code) ||
buy_number_for_area_code(area_code) ||
normalize_number(outbound_caller_id)
end
|
#client ⇒ Object
99
100
101
|
# File 'lib/telephony/providers/twilio_provider.rb', line 99
def client
@client ||= ::Twilio::REST::Client.new account_sid, auth_token, timeout: REQUEST_TIMEOUT_IN_SECONDS
end
|
#dial_into_conference(call_id, destination, caller_id) ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/telephony/providers/twilio_provider.rb', line 53
def dial_into_conference(call_id, destination, caller_id)
create_call number: destination,
caller_id: caller_id,
callback_url: "#{twilio_provider_url}/#{call_id}/join_conference",
call_id: call_id,
timeout: RING_TRANSFER_TIMEOUT_IN_SECONDS
end
|
#hangup(sid) ⇒ Object
61
62
63
64
65
66
67
|
# File 'lib/telephony/providers/twilio_provider.rb', line 61
def hangup(sid)
call = client.account.calls.find sid
call.hangup
rescue => error
log_error(error, "Error during hangup for call sid: '#{sid}'")
false
end
|
#redirect_to_conference(call_id, sid) ⇒ Object
45
46
47
|
# File 'lib/telephony/providers/twilio_provider.rb', line 45
def redirect_to_conference(call_id, sid)
redirect_or_raise sid, "#{twilio_provider_url}/#{call_id}/join_conference"
end
|
#redirect_to_dial(call_id, sid) ⇒ Object
37
38
39
|
# File 'lib/telephony/providers/twilio_provider.rb', line 37
def redirect_to_dial(call_id, sid)
redirect_or_raise sid, "#{twilio_provider_url}/#{call_id}/dial"
end
|
#redirect_to_hold(call_id, sid) ⇒ Object
49
50
51
|
# File 'lib/telephony/providers/twilio_provider.rb', line 49
def redirect_to_hold(call_id, sid)
redirect_or_raise sid, "#{twilio_provider_url}/#{call_id}/complete_hold"
end
|
#redirect_to_inbound_connect(csr_id, sid) ⇒ Object
41
42
43
|
# File 'lib/telephony/providers/twilio_provider.rb', line 41
def redirect_to_inbound_connect(csr_id, sid)
redirect_or_raise sid, "#{callback_root}/providers/twilio/inbound_calls/connect?csr_id=#{csr_id}"
end
|
#uncallable_number ⇒ Object
85
86
87
|
# File 'lib/telephony/providers/twilio_provider.rb', line 85
def uncallable_number
'this-number-is-not-whitelisted'
end
|