Skip to content

Instantly share code, notes, and snippets.

@presskey
Forked from ytspar/sending_messages.rb
Created March 2, 2012 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save presskey/1954621 to your computer and use it in GitHub Desktop.
Save presskey/1954621 to your computer and use it in GitHub Desktop.
Recess Mobile - sending a text message (SMS)
require 'uri'
require 'net/https'
require 'json'
API_URL = "http://api.recessmobile.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