Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 115 lines (82 sloc) 3.967 kB
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
1 # examples version 3
2
3 @account_sid = 'AC043dcf9844e04758bc3a36a84c29761'
4 @auth_token = '62ea81de3a5b414154eb263595357c69'
14ce815 @andrewmbenton more examples
andrewmbenton authored
5 # set up a client
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
6 @client = Twilio::REST::Client.new(@account_sid, @auth_token)
7
8 ################ ACCOUNTS ################
9
f4dbd64 @andrewmbenton examples cleanup
andrewmbenton authored
10 # shortcut to grab your account object (account_sid is inferred from the client's auth credentials)
11 @account = @client.account
12
13 # list your (sub)accounts
14 @client.accounts.list
15
14ce815 @andrewmbenton more examples
andrewmbenton authored
16 # grab an account instance resource if you know the sid
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
17 @account = @client.accounts.get(@account_sid)
18 # http round trip happens here
19 puts @account.friendly_name
20
14ce815 @andrewmbenton more examples
andrewmbenton authored
21 # update an account's friendly name
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
22 @client.accounts.get(@account_sid).update(:friendly_name => 'A Fabulous Friendly Name')
23
24 ################ CALLS ################
25
14ce815 @andrewmbenton more examples
andrewmbenton authored
26 # print a list of calls (all parameters optional)
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
27 @account.calls.list({:page => 0, :page_size => 1000, :start_time => '2010-09-01'}).each do |call|
28 puts call.sid
29 end
30
14ce815 @andrewmbenton more examples
andrewmbenton authored
31 # get a particular call and list its recording urls
f4dbd64 @andrewmbenton examples cleanup
andrewmbenton authored
32 @account.calls.get('CAXXXXXXX').recordings.list.each do {|r| puts r.wav}
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
33
14ce815 @andrewmbenton more examples
andrewmbenton authored
34 # make a new outgoing call. returns a call object just like calls.get
957832c @jpf s/myapp.com/example.com/g
jpf authored
35 @call = @account.calls.create({:from => '+14159341234', :to => '+18004567890', :url => 'http://example.com/call-handler'})
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
36
14ce815 @andrewmbenton more examples
andrewmbenton authored
37 # cancel the call if not already in progress
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
38 @account.calls.get(@call.sid).update({:status => 'canceled'})
14ce815 @andrewmbenton more examples
andrewmbenton authored
39 # or equivalently
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
40 @call.update({:status => 'canceled'})
41 # or simply
42 @call.cancel
43
14ce815 @andrewmbenton more examples
andrewmbenton authored
44 # redirect and then terminate a call
957832c @jpf s/myapp.com/example.com/g
jpf authored
45 @account.calls.get('CA386025c9bf5d6052a1d1ea42b4d16662').update({:url => 'http://example.com/call-redirect'})
14ce815 @andrewmbenton more examples
andrewmbenton authored
46 @account.calls.get('CA386025c9bf5d6052a1d1ea42b4d16662').update({:status => 'completed'})
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
47 # or, use the aliases...
957832c @jpf s/myapp.com/example.com/g
jpf authored
48 @call.redirect_to('http://example.com/call-redirect')
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
49 @call.hangup
50
51 ################ SMS MESSAGES ################
52
14ce815 @andrewmbenton more examples
andrewmbenton authored
53 # print a list of sms messages
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
54 @account.sms.messages.list({:date_sent => '2010-09-01'}).each do |sms|
55 puts sms.body
56 end
57
14ce815 @andrewmbenton more examples
andrewmbenton authored
58 # print a particular sms message
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
59 puts @account.sms.messages.get('SMXXXXXXXX').body
60
61 # send an sms
62 @account.sms.messages.create(:from => '+14159341234', :to => '+16105557069', :body => 'Hey there!')
63
64 ################ PHONE NUMBERS ################
65
f4dbd64 @andrewmbenton examples cleanup
andrewmbenton authored
66 # get a list of supported country codes
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
67 @account.available_phone_numbers.list
68
14ce815 @andrewmbenton more examples
andrewmbenton authored
69 # print some available numbers
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
70 @numbers = @account.available_phone_numbers.get('US').local.list({:contains => 'AWESOME'})
71 @numbers.each {|num| puts num.phone_number}
72
14ce815 @andrewmbenton more examples
andrewmbenton authored
73 # buy the first one
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
74 @account.incoming_phone_numbers.create(:phone_number => @numbers[0].phone_number)
75
14ce815 @andrewmbenton more examples
andrewmbenton authored
76 # update an existing phone number's voice url
957832c @jpf s/myapp.com/example.com/g
jpf authored
77 @account.incoming_phone_numbers.get('PNdba508c5616a7f5e141789f44f022cc3').update({:voice_url => 'http://example.com/voice'})
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
78
79 ################ CONFERENCES ################
80
14ce815 @andrewmbenton more examples
andrewmbenton authored
81 # get a particular conference's participants object and stash it
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
82 @participants = @account.conferences.get('CFbbe46ff1274e283f7e3ac1df0072ab39').participants
83
14ce815 @andrewmbenton more examples
andrewmbenton authored
84 # list participants
c8a763e @andrewmbenton new twilio-ruby finally :)
andrewmbenton authored
85 @participants.list.each do {|p| puts p.sid}
86
87 # update a conference participant
88 @participants.get('CA386025c9bf5d6052a1d1ea42b4d16662').update({:muted => 'true'})
89 # or an easier way
90 @participants.get('CA386025c9bf5d6052a1d1ea42b4d16662').mute
91
92 # and, since we're lazy loading, this would only incur one http request
93 @account.conferences.get('CFbbe46ff1274e283f7e3ac1df0072ab39').participants.get('CA386025c9bf5d6052a1d1ea42b4d16662').update({:muted => 'true'})
8fe2d14 Finished queue and member functionality and added some examples in ex…
Oscar Sanchez authored
94
95 ################ QUEUES ###################
96
32c8852 changed examples.rb to show creating a new queue
Oscar Sanchez authored
97 # create a new queue
def834b @andrewmbenton minor queue documentation tweaks
andrewmbenton authored
98 @queue = @account.queues.create(:friendly_name => 'MyQueue', :max_size => 50)
32c8852 changed examples.rb to show creating a new queue
Oscar Sanchez authored
99
8fe2d14 Finished queue and member functionality and added some examples in ex…
Oscar Sanchez authored
100 # get a list of queues for this account
101 @queues = @account.queues.list
102
103 # get a particular queue and its members
104 @queue = @account.queues.get("QQb6765b0458714964970a73dcaf55efd1")
105 @members = @queue.members
106
107 #list members
108 @members.list.each do |m|
60da7bb @andrewmbenton a slightly more interesting example
andrewmbenton authored
109 puts m.wait_time
8fe2d14 Finished queue and member functionality and added some examples in ex…
Oscar Sanchez authored
110 end
111
112 # dequeue a particular user and run twiml at a specific url
113 @member = @members.get('CA386025c9bf5d6052a1d1ea42b4d16662')
114 @member.dequeue('http://myapp.com/deque')
Something went wrong with that request. Please try again.