Skip to content

Instantly share code, notes, and snippets.

@etsai
Last active January 4, 2016 21:39
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 etsai/8682643 to your computer and use it in GitHub Desktop.
Save etsai/8682643 to your computer and use it in GitHub Desktop.
Basic script of the Twilio Live Demo to introduce Twilio's API
#Add Ruby Library
require "twilio-ruby"
#Connect to my account
@client = Twilio::REST::Client.new 'AccountSid', 'AuthToken'
#Assiging that object so it easier to access my Account
@account = @client.account
#Get a list of all available US phone numbers
@account.available_phone_numbers.get('US').local.list({:area_code => '415'})
#Will assign n to the first number of that list
n = _[0]
#Let's purchase that phone number
@account.incoming_phone_numbers.create(:phone_number =>n.phone_number)
#Assign the phone number you just bought to variable 'my_number'
my_number = _
#Configuring Twilio phone number to a Voice application
my_number.update({:voice_url => 'http://twimlbin.com/...'})
#Or Configure to SMS application
my_number.update({:sms_url => 'http://twimlbin.com/...'})
#Display phone number for people to call
my_number.friendly_name
#Ask audience to call or text
#Here's a list of all the people who have called my number
@account.calls.list({:to => my_number.phone_number}).each do |x|
puts x.from
end
#Send SMS messages to all callers
@account.calls.list({:to => my_number.phone_number}).each do |x|
@account.sms.messages.create(:from => my_number.phone_number, :to =>x.from, :body => 'Thanks!')
end
#SMS Body
Thanks for watching the Twilio demo!
#Variation: Can also call all incoming callers and place them in a conference room to make those phones ring.
@account.calls.list({:to =>my_number.phone_number}).each do |x|
@account.calls.create({:from=>my_number.phone_number, :to=>x.from, :url => 'http://twimlbin.com/...'})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment