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

Returns Twilio client initialized with ‘account_sid` and `auth_token` from the config.

Returns:

  • (Twilio::REST::Client)

    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.,
    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.

Parameters:

  • message (String)

    the message to send.

  • to (String)

    the phone number to send the message to.

  • from (String)

    the phone number to send the message from.

Returns:

  • (String)

    the SID returned from Twilio for the sent SMS message.



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

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.

Parameters:

  • messages (Array<String>)

    the messages to send.

  • to (String)

    the phone number to send the messages to.

  • from (String)

    the phone number to send the messages from.

Returns:

  • (Array<String>)

    the SIDs returned from Twilio for the sent SMS messages.



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

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.

Parameters:

  • url (String)

    the URL to use for the Twilio REST API call, probably from the Phone::Tree.

  • to (String)

    the phone number to make the call to.

  • from (String)

    the phone number to make the call from.

  • answering_machine_detection (true, false) (defaults to: true)

    whether or not to enable answering machine detection.

Returns:

  • (String)

    the SID returned from Twilio for the started 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