Class: CommandTower::Messaging::Execution::Adapters::Sms::TwilioHttpClient

Inherits:
Object
  • Object
show all
Defined in:
app/services/command_tower/messaging/execution/adapters/sms/twilio_http_client.rb

Overview

Narrowly scoped Twilio Messages HTTP client for Messaging notification SMS. Not shared with Identity OTP transport.

Constant Summary collapse

API_BASE =
"https://api.twilio.com/2010-04-01"

Instance Method Summary collapse

Constructor Details

#initialize(account_sid:, auth_token:) ⇒ TwilioHttpClient

Returns a new instance of TwilioHttpClient.



17
18
19
20
# File 'app/services/command_tower/messaging/execution/adapters/sms/twilio_http_client.rb', line 17

def initialize(account_sid:, auth_token:)
  @account_sid = 
  @auth_token = auth_token
end

Instance Method Details

#create_message(to:, body:, from: nil, messaging_service_sid: nil) ⇒ Object

Returns a hash: { ok:, status_code:, body:, sid:, provider_status:, error_code: }



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/command_tower/messaging/execution/adapters/sms/twilio_http_client.rb', line 23

def create_message(to:, body:, from: nil, messaging_service_sid: nil)
  uri = URI("#{API_BASE}/Accounts/#{@account_sid}/Messages.json")
  request = Net::HTTP::Post.new(uri)
  request.basic_auth(@account_sid, @auth_token)

  form = { "To" => to, "Body" => body }
  if messaging_service_sid.present?
    form["MessagingServiceSid"] = messaging_service_sid
  else
    form["From"] = from
  end
  request.set_form_data(form)

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, open_timeout: 10, read_timeout: 15) do |http|
    http.request(request)
  end

  parse_response(response)
end