-
-
Save ytspar/f5ed09d09b06300e836b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'uri' | |
require 'net/https' | |
require 'json' | |
API_URL = "https://sl.carouselsms.com/api" | |
API_KEY = "[API KEY GOES HERE]" | |
# Simple ServiceLayer gateway class | |
class ServiceLayer | |
class Error < StandardError; end | |
def self.api_call(command, data = nil) | |
data = (data || {}).merge(:api_key => API_KEY) | |
res = Net::HTTP.post_form(URI.parse(API_URL + "/" + command.to_s), data) | |
# Raise an error | |
raise ServiceLayer::Error.new(res.body) if res.code == 500 | |
parse_response res.body | |
end | |
def self.parse_response(body) | |
return JSON.parse(body) | |
rescue => e | |
return body | |
end | |
end | |
# Sending a message | |
message_ids = ServiceLayer.api_call(:send_message, | |
:phone_number => '[PHONE NUMBER GOES HERE]', | |
:body => 'Hey', | |
:tag => 'tag1') | |
puts message_ids.inspect # => { "message_ids": "685320" } | |
# Sending multiple messages | |
message_ids = ServiceLayer.api_call(:send_messages, | |
:template => 'Hello {{name}}. You have {{credits}} credits left.', | |
:recipients => { | |
'555555555555' => { :name => 'Aleks', :credits => '100' }, | |
'5555555555' => { :name => 'John', :credits => '0' } | |
}.to_json, | |
:tag => 'tag2') | |
puts message_ids.inspect # => { "message_ids" => "685322,685323" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment