Skip to content

Instantly share code, notes, and snippets.

@rahims
Created May 4, 2011 06:42
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 rahims/954842 to your computer and use it in GitHub Desktop.
Save rahims/954842 to your computer and use it in GitHub Desktop.
Code to send an SMS using the Twilio API in Ruby. Written by Andrés Corvetto (not me, Rahim Sonawalla). Full blog post here: http://www.twilio.com/blog/2011/05/andres-corvetto-wins-our-twiliohardware-developer-contest.html
require 'twiliolib'
# Twilio REST API version
API_VERSION = '2010-04-01'
# Twilio AccountSid and AuthToken
ACCOUNT_SID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCOUNT_TOKEN = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
FROM_ID = 'xxx-xxx-xxx';
CALLER_ID = 'yyy-yyy-yyy';
account = Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN)
body= 'Intruder alert! http://10.10.10.10/alert/'+ARGV[0]
puts 'Sending SMS:' + body
d = {
'From' => FROM_ID,
'To' => CALLER_ID,
'Body' => body,
}
resp = account.request("/#{API_VERSION}/Accounts/#{ACCOUNT_SID}/SMS/Messages", 'POST', d)
@kevinburke
Copy link

Hi, Unfortunately this code interface has been deprecated with version 3 of the twilio-ruby gem. The good news is the new interface is much simpler.

Here is a working example using the new interface:

    require 'rubygems' # not necessary with ruby 1.9 but included for completeness
    require 'twilio-ruby'

    # put your own credentials here
    account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'

    # set up a client to talk to the Twilio REST API
    @client = Twilio::REST::Client.new account_sid, auth_token

    @client.account.sms.messages.create(
      :from => '+14159341234',
      :to => '+16105557069',
      :body => 'Intruder alert! http://10.10.10.10/alert/' + ARGV[0]
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment