Permalink
Browse files

It's a thing

  • Loading branch information...
0 parents commit 930bda6e9bcabbf905357933472b18d5bbafa998 @nakajima committed Jul 30, 2010
Showing with 525 additions and 0 deletions.
  1. +22 −0 LICENSE
  2. +25 −0 README.md
  3. +32 −0 bin/sms
  4. +27 −0 lib/sms.rb
  5. 0 lib/sms/.gitignore
  6. +392 −0 lib/sms/twiliolib.rb
  7. +27 −0 sms-rb.gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+(c) Copyright 2010 Pat Nakajima
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
+# sms-rb
+
+Send text messages with Twilio (http://www.twilio.com). Easily.
+
+## USAGE
+
+Set the following ENV variables:
+
+* `TWILIO_ID` - Your Twilio API id token.
+* `TWILIO_SECRET` - Your Twilio API secret token.
+* `TWILIO_PHONE` (optional) - One of your Twilio phone numbers.
+
+Then send some texts!
+
+If you've set `ENV['TWILIO_PHONE']`:
+
+ SMS.text "Hello!", :to => "5558675309"
+
+If you haven't:
+
+ SMS.text "Hello!", :to => "5558675309", :from => "5555555555"
+
+Calling `SMS.text` returns true or false depending on whether or not
+it was a success. You can also call `SMS.text!`, which will raise an
+error if the text does not get sent successfully.
32 bin/sms
@@ -0,0 +1,32 @@
+#!/usr/bin/env ruby
+
+require File.join(File.dirname(__FILE__), '..', 'lib', 'sms')
+require 'optparse'
+
+from = to = nil
+OptionParser.new do |opts|
+ opts.banner = 'Usage: sms "some message" [options]'
+ opts.separator "Specific options:"
+ opts.on('-t', '--to [PHONE NUMBER]', "Recipient's number") { |v| to ||= v }
+ opts.on('-f', '--from [PHONE NUMBER]', "Your Twilio number") { |v| from ||= v }
+end.parse!
+
+ENV['TWILIO_ID'] || abort("Please set ENV['TWILIO_ID'] to your Twilio API id.")
+ENV['TWILIO_SECRET'] || abort("Please set ENV['TWILIO_SECRET] to your Twilio API secret key.")
+
+if from.nil? and ENV['TWILIO_PHONE'].nil?
+ abort <<-ERR
+ Twilio number blank.
+ Please set ENV['TWILIO_PHONE'] or pass the --from option.
+ ERR
+end
+
+if to.nil?
+ abort 'Recipient number blank.'
+end
+
+begin
+ SMS.text! ARGV.join(' '), :from => from, :to => to
+rescue => e
+ abort('Failure: ' + e.message)
+end
@@ -0,0 +1,27 @@
+require 'rubygems'
+require File.join(File.dirname(__FILE__), 'sms', 'twiliolib')
+
+module SMS
+ TWILIO_SEND = "/2008-08-01/Accounts/#{ENV['TWILIO_ID']}/SMS/Messages"
+
+ def self.text(message, options={})
+ twilio = Twilio::RestAccount.new ENV['TWILIO_ID'], ENV['TWILIO_SECRET']
+ res = twilio.request TWILIO_SEND, 'POST',
+ 'To' => options[:to],
+ 'From' => options[:from] || ENV['TWILIO_PHONE'],
+ 'Body' => message
+ if res.code == 201
+ true
+ else
+ if options[:raise]
+ raise(res.body.match(/<Message>(Message body is required)<\/Message>/)[1])
+ else
+ false
+ end
+ end
+ end
+
+ def self.text!(message, options={})
+ text(message, options.merge(:raise => true))
+ end
+end
No changes.
Oops, something went wrong.

0 comments on commit 930bda6

Please sign in to comment.