Created
January 31, 2011 19:11
-
-
Save adamloving/804603 to your computer and use it in GitHub Desktop.
Simple Twilio Ruby Client Class
This file contains 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 '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