Class: SignalApi::DeliverSms

Inherits:
SignalHttpApi show all
Includes:
ApiMock
Defined in:
lib/signal_api/deliver_sms.rb,
lib/signal_api/mocks/deliver_sms.rb

Overview

Deliver a SMS message using Signal’s messaging API

Instance Method Summary collapse

Methods included from ApiMock

included

Constructor Details

#initialize(username, password) ⇒ DeliverSms

Create an instance of this class, with your messaging API credentials. These credentials are separate from the api_key that is used by the other APIs, and can be found in the API campaign configuration.



11
12
13
14
15
16
17
18
# File 'lib/signal_api/deliver_sms.rb', line 11

def initialize(username, password)
  @username = username
  @password = password

  if @username.nil? || @password.nil?
    raise InvalidParameterException.new("username and password must be provided")
  end
end

Instance Method Details

#deliver(mobile_phone, message) ⇒ String

Deliver a SMS message to a mobile phone. Messages exceeding the 160 character limit will be split into multiple messages.

Parameters:

  • mobile_phone (String)

    The mobile phone to send the message to

  • message (String)

    The message to send

Returns:

  • (String)

    The unique message ID



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/signal_api/deliver_sms.rb', line 27

def deliver(mobile_phone, message)
  sanitized_mobile_phone = Phone.sanitize(mobile_phone)
  unless Phone.valid?(sanitized_mobile_phone)
    raise InvalidParameterException.new("An invalid mobile phone was specified: #{mobile_phone}")
  end

  if message.nil? || message.strip.empty?
    raise InvalidParameterException.new("A message must be provided")
  end

  SignalApi.logger.info "Delivering the following message to #{sanitized_mobile_phone}: #{message}"
  self.class.with_retries do
    response = self.class.post('/messages/send',
                               :basic_auth => { :username => @username, :password => @password },
                               :query => { :mobile_phone => sanitized_mobile_phone, :message => message })

    if response.code == 200
      response.parsed_response =~ /^Message ID: (.*)$/
      $1
    else
      self.class.handle_api_failure(response)
    end
  end
end

#deliver_additional_infoObject



9
10
11
# File 'lib/signal_api/mocks/deliver_sms.rb', line 9

def deliver_additional_info
  { :user_name => @username }
end