Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created January 31, 2011 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/804603 to your computer and use it in GitHub Desktop.
Save adamloving/804603 to your computer and use it in GitHub Desktop.
Simple Twilio Ruby Client Class
require 'twiliolib'
ACCOUNT_SID = 'big-hex-number'
ACCOUNT_TOKEN = 'another-big-hex-number'
# version of the Twilio REST API to use
API_VERSION = '2010-04-01'
# base URL of this application
BASE_URL = "http://myapp.com"
CALLER_ID = 'your-twilio-phone-number'
class Sms
def self.normalize_number(n)
"+1" + n.delete(' (\-)') if n
end
def self.send(to_phone_number, body)
d = {
'From' => CALLER_ID,
'To' => Sms.normalize_number(to_phone_number),
'Body' => body
}
begin
account = Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN)
url = "/#{API_VERSION}/Accounts/#{ACCOUNT_SID}/SMS/Messages"
puts url
puts d.inspect
resp = account.request(url, 'POST', d)
resp.error! unless resp.kind_of? Net::HTTPSuccess
return true
rescue StandardError => bang
# todo: log it somewhere!
puts bang.inspect
return false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment