Module: Twilio::Rails::Client
Overview
An abstraction over top of the Twilio::REST
API client. Used to send SMS messages and start calls, as well as
return an initialized client if needed.
Instance Method Summary collapse
-
#client ⇒ Twilio::REST::Client
Twilio client initialized with
account_sid
andauth_token
from the config. -
#send_message(message:, to:, from:) ⇒ String
Do not call this directly, instead see SMS::SendOperation.
-
#send_messages(messages:, to:, from:) ⇒ Array<String>
Do not call this directly, instead see SMS::SendOperation.
-
#start_call(url:, to:, from:, answering_machine_detection: true) ⇒ String
Do not call this directly, instead see Phone::StartCallOperation.
Instance Method Details
#client ⇒ Twilio::REST::Client
Returns Twilio client initialized with account_sid
and auth_token
from the config.
10 11 12 13 14 15 |
# File 'lib/twilio/rails/client.rb', line 10 def client @twilio_client ||= Twilio::REST::Client.new( Twilio::Rails.config.account_sid, Twilio::Rails.config.auth_token, ) end |
#send_message(message:, to:, from:) ⇒ String
Do not call this directly, instead see SMS::SendOperation. Send an SMS message to and from the given phone numbers using the Twilio REST API directly. This does not store or manage any interactions in the database.
25 26 27 28 29 30 31 32 33 |
# File 'lib/twilio/rails/client.rb', line 25 def (message:, to:, from:) Twilio::Rails.config.logger.tagged(self) { |l| l.info("[send_message] to=#{ to } from=#{ from } body='#{ }'") } client..create( from: from, to: to, body: , status_callback: "#{ Twilio::Rails.config.host }#{ ::Twilio::Rails::Engine.routes.url_helpers.sms_status_path(format: :xml) }", ).sid end |
#send_messages(messages:, to:, from:) ⇒ Array<String>
Do not call this directly, instead see SMS::SendOperation. Sends multiple SMS messages to and from the given phone numbers using the Twilio REST API directly. This does not store or manage any interactions in the database. If a message is blank it will be ignored.
43 44 45 46 |
# File 'lib/twilio/rails/client.rb', line 43 def (messages:, to:, from:) Twilio::Rails.config.logger.tagged(self) { |l| l.info("[send_messages] to blank messages") } if .blank? .map { |m| (message: m, to: to, from: from) } end |
#start_call(url:, to:, from:, answering_machine_detection: true) ⇒ String
Do not call this directly, instead see Phone::StartCallOperation. Starts a phone call to and from the given phone numbers using the Twilio REST API directly. This does not store or manage any interactions in the database. The URL should come from the Phone::Tree that is being used to start the call.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/twilio/rails/client.rb', line 57 def start_call(url:, to:, from:, answering_machine_detection: true) Twilio::Rails.config.logger.tagged(self) { |l| l.info("[start_call] to=#{ to } from=#{ from } url=#{ url } answering_machine_detection=#{ !!answering_machine_detection }") } client.calls.create( from: from, to: to, url: url, machine_detection: ( answering_machine_detection ? "Enable" : "Disable" ), async_amd: true, async_amd_status_callback: "#{ Twilio::Rails.config.host }#{ ::Twilio::Rails::Engine.routes.url_helpers.phone_status_path(format: :xml, async_amd: "true") }", async_amd_status_callback_method: "POST", status_callback: "#{ Twilio::Rails.config.host }#{ ::Twilio::Rails::Engine.routes.url_helpers.phone_status_path(format: :xml) }", status_callback_method: "POST", status_callback_event: ["completed", "no-answer"], # timeout: 30, ).sid end |