Class: TwilioClient
- Inherits:
-
Object
- Object
- TwilioClient
- Includes:
- Singleton
- Defined in:
- app/services/twilio_client.rb
Overview
Service object: twilio client.
Delegated Instance Attributes collapse
-
#client ⇒ Object
readonly
Alias for Instance#client.
Instance Attribute Summary collapse
-
#default_sender ⇒ Object
readonly
Returns the value of attribute default_sender.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Delegated Instance Attributes collapse
-
#download_recording(recording_sid, dual_channel: true) ⇒ String
Download a recording as WAV (required when call recording encryption is enabled) SIP trunk recordings are natively stereo (2 channels) - preserved in WAV format.
-
#list_numbers ⇒ Object
Alias for Instance#list_numbers.
-
#list_recordings(date_created_after: nil, limit: 100) ⇒ Array<Hash>
List recordings from Twilio.
-
#lookup_line_type(number) ⇒ Symbol
Twilio Lookups v2 Line Type Intelligence.
-
#send_message(to:, body:, from: nil, media_urls: nil) ⇒ Object
============================================================================ SMS Methods ============================================================================.
Instance Method Summary collapse
-
#delete_recording(recording_sid) ⇒ Object
Delete a recording from Twilio (after successful import).
-
#get_call(call_sid) ⇒ Hash
Get call details for a recording (to extract phone numbers).
-
#initialize(options = {}) ⇒ TwilioClient
constructor
A new instance of TwilioClient.
Constructor Details
#initialize(options = {}) ⇒ TwilioClient
Returns a new instance of TwilioClient.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'app/services/twilio_client.rb', line 16 def initialize( = {}) @logger = [:logger] || Rails.logger account_sid = [:account_sid] || Heatwave::Configuration.fetch(:twilio, :account_sid) auth_token = [:auth_token] || Heatwave::Configuration.fetch(:twilio, :auth_token) @account_sid = account_sid @auth_token = auth_token @sms_status_callback_url = [:sms_status_callback_url] @sms_status_callback_url ||= "#{Heatwave::Configuration.fetch(:twilio, :sms_base_wehook_url)}/update_status" @default_sender = [:default_sender] || '+18008755285' @client = Twilio::REST::Client.new account_sid, auth_token end |
Instance Attribute Details
#client ⇒ Object (readonly)
Alias for Instance#client
10 11 12 |
# File 'app/services/twilio_client.rb', line 10 def client @client end |
#default_sender ⇒ Object (readonly)
Returns the value of attribute default_sender.
10 11 12 |
# File 'app/services/twilio_client.rb', line 10 def default_sender @default_sender end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'app/services/twilio_client.rb', line 10 def logger @logger end |
Instance Method Details
#delete_recording(recording_sid) ⇒ Object
Delete a recording from Twilio (after successful import)
182 183 184 185 186 187 188 189 |
# File 'app/services/twilio_client.rb', line 182 def delete_recording(recording_sid) client.recordings(recording_sid).delete logger.info "[TwilioClient] Deleted recording #{recording_sid}" true rescue Twilio::REST::RestError => e logger.error "[TwilioClient] Failed to delete recording #{recording_sid}: #{e.}" false end |
#download_recording(recording_sid, dual_channel: true) ⇒ String
Download a recording as WAV (required when call recording encryption is enabled)
SIP trunk recordings are natively stereo (2 channels) - preserved in WAV format
Note: Twilio error 16109 - When call recording encryption is enabled,
recordings can ONLY be downloaded in WAV format, not MP3.
See: https://www.twilio.com/docs/errors/16109
150 |
# File 'app/services/twilio_client.rb', line 150 delegate :client, :send_message, :list_numbers, :list_recordings, :download_recording, :lookup_line_type, to: :instance |
#get_call(call_sid) ⇒ Hash
Get call details for a recording (to extract phone numbers)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/services/twilio_client.rb', line 121 def get_call(call_sid) call = client.calls(call_sid).fetch { sid: call.sid, from: call.from, to: call.to, direction: call.direction, duration: call.duration.to_i, start_time: call.start_time, end_time: call.end_time, status: call.status, # SIP trunk info if available trunk_sid: call.trunk_sid } rescue Twilio::REST::RestError => e logger.error "[TwilioClient] Failed to fetch call #{call_sid}: #{e.}" nil end |
#list_numbers ⇒ Object
Alias for Instance#list_numbers
13 |
# File 'app/services/twilio_client.rb', line 13 delegate :client, :send_message, :list_numbers, :list_recordings, :download_recording, :lookup_line_type, to: :instance |
#list_recordings(date_created_after: nil, limit: 100) ⇒ Array<Hash>
List recordings from Twilio
96 |
# File 'app/services/twilio_client.rb', line 96 delegate :client, :send_message, :list_numbers, :list_recordings, :download_recording, :lookup_line_type, to: :instance |
#lookup_line_type(number) ⇒ Symbol
Logs only the last 4 digits of the number to avoid PII leakage to
centralized log aggregators.
Twilio Lookups v2 Line Type Intelligence.
Returns the carrier-reported line type for an E.164 number, or :unknown
when Twilio can't classify or the lookup fails. Billed per call
(~$0.005 USD as of 2026), so callers should cache by number and re-verify
infrequently.
Logic Details
Twilio's type field is camelCase (fixedVoip, nonFixedVoip,
tollFree, sharedCost); this method normalizes to underscored Ruby
symbols.
Twilio::REST::TwilioError is the parent of both RestError (HTTP 4xx/5xx
responses) and the network/timeout wrappers, so rescuing it preserves the
"always returns :unknown on failure" contract for both flavors of failure.
76 |
# File 'app/services/twilio_client.rb', line 76 delegate :client, :send_message, :list_numbers, :list_recordings, :download_recording, :lookup_line_type, to: :instance |
#send_message(to:, body:, from: nil, media_urls: nil) ⇒ Object
============================================================================
SMS Methods
32 |
# File 'app/services/twilio_client.rb', line 32 delegate :client, :send_message, :list_numbers, :list_recordings, :download_recording, :lookup_line_type, to: :instance |