class Agents::Elks46SmsAgent

Public Instance Methods

default_options() click to toggle source

There is a form_configurable for extra options, this can be used instead of the default options. It's located in app/concerns/form_configurable.rb

# File lib/huginn_elks46_sms_agent/elks46_sms_agent.rb, line 24
def default_options
  {
    'api_username' => 'u6xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'api_password' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'from' => 'Huginn',
    'to' => ['+46700000000'],
    'message' => 'Hello this message from your friend Huginn.',
  }
end
receive(incoming_events) click to toggle source
# File lib/huginn_elks46_sms_agent/elks46_sms_agent.rb, line 56
def receive(incoming_events)
  incoming_events.each do |event|
      interpolate_with event do
        send_sms(interpolated)
      end
  end
end
send_sms(payload) click to toggle source
# File lib/huginn_elks46_sms_agent/elks46_sms_agent.rb, line 64
def send_sms(payload)
  payload['to'].each do |to_recipient|
    uri = URI('https://api.46elks.com/a1/sms')
    req = Net::HTTP::Post.new(uri)
    req.basic_auth payload['api_username'], payload['api_password']
    req.set_form_data(
      :from => payload['from'],
      :to => to_recipient,
      :message => payload['message']
    )

    res = Net::HTTP.start(
        uri.host,
        uri.port,
        :use_ssl => uri.scheme == 'https') do |http|
      http.request req
    end

    unless res.is_a?(Net::HTTPSuccess)
      error("Error: #{res.body}")
    end
  end
end
validate_options() click to toggle source
# File lib/huginn_elks46_sms_agent/elks46_sms_agent.rb, line 34
def validate_options
  unless options['api_username'].present?
    errors.add(:base, '`api_username` is required.')
  end
  unless options['api_password'].present?
    errors.add(:base, '`api_password` is required.')
  end
  unless interpolated['from'].present?
    errors.add(:base, '`from` is required.')
  end
  unless interpolated['to'].present?
    errors.add(:base, '`to` is required.')
  end
  unless interpolated['message'].present?
    errors.add(:base, '`message` is required.')
  end
end
working?() click to toggle source
# File lib/huginn_elks46_sms_agent/elks46_sms_agent.rb, line 52
def working?
  !recent_error_logs?
end