Module: Twilio::Rails::Client

Extended by:
Client
Included in:
Client
Defined in:
lib/twilio/rails/client.rb

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

Instance Method Details

#clientTwilio::REST::Client



11
12
13
14
15
16
# File 'lib/twilio/rails/client.rb', line 11

def client
  @twilio_client ||= Twilio::REST::Client.new(
    Twilio::Rails.config.,
    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.



26
27
28
29
30
31
32
33
34
# File 'lib/twilio/rails/client.rb', line 26

def send_message(message:, to:, from:)
  Twilio::Rails.config.logger.tagged(self) { |l| l.info("[send_message] to=#{to} from=#{from} body='#{message}'") }
  client.messages.create(
    from: from,
    to: to,
    body: message,
    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.



44
45
46
47
# File 'lib/twilio/rails/client.rb', line 44

def send_messages(messages:, to:, from:)
  Twilio::Rails.config.logger.tagged(self) { |l| l.info("[send_messages] to blank messages") } if messages.blank?
  messages.map { |m| send_message(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.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/twilio/rails/client.rb', line 58

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