webficient-twilio 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +24 -6
- data/VERSION.yml +1 -1
- data/lib/twilio/verb.rb +32 -25
- data/test/fixtures/yml/verb_responses.yml +1 -1
- data/test/twilio/verb_test.rb +31 -31
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -1,23 +1,41 @@
|
|
1
|
-
=
|
1
|
+
= Twilio Gem
|
2
2
|
|
3
|
-
|
3
|
+
The Twilio gem provides two major pieces of functionality: (1) a Ruby wrapper for the Twilio REST API and (2) response handlers based on the Twilio Markup XML (TwiML).
|
4
4
|
|
5
|
-
|
5
|
+
See http://www.twilio.com/docs/index for Twilio's API documentation.
|
6
|
+
|
7
|
+
For an overview of the Twilio Gem and a sample use case, check out http://www.webficient.com/2009/06/22/hello-this-is-your-rails-app-calling-you.
|
8
|
+
|
9
|
+
== Calling the Twilio REST API
|
6
10
|
|
7
11
|
First create a connection object:
|
8
12
|
|
9
13
|
c = Twilio::Connection.new('my_twilio_sid', 'my_auth_token')
|
10
14
|
|
11
15
|
Now instantiate other objects by passing in the connection:
|
12
|
-
|
13
|
-
a = Twilio::Account.new(c)
|
14
|
-
a.update_name('sparky')
|
15
16
|
|
16
17
|
call = Twilio::Call.new(c)
|
17
18
|
call.make('1234567890', '9876543210', 'http://mysite.com/connected_call')
|
18
19
|
|
19
20
|
recording = Twilio::Recording.new(c)
|
20
21
|
recording.list
|
22
|
+
|
23
|
+
== Responding to Twilio
|
24
|
+
|
25
|
+
When Twilio calls your application URL, your response must use the Twilio Markup XML (http://www.twilio.com/docs/api_reference/TwiML/). The Twilio gem makes this very easy
|
26
|
+
by providing a Twilio Verb class.
|
27
|
+
|
28
|
+
For example, in a Ruby on Rails application, you could do the following inside a controller class:
|
29
|
+
|
30
|
+
Twilio::Verb.dial('415-123-4567')
|
31
|
+
|
32
|
+
and you can nest multiple verbs inside a block:
|
33
|
+
|
34
|
+
verb = Twilio::Verb.new { |v|
|
35
|
+
v.say("The time is #{Time.now}")
|
36
|
+
v.hangup
|
37
|
+
}
|
38
|
+
verb.response
|
21
39
|
|
22
40
|
== Copyright
|
23
41
|
|
data/VERSION.yml
CHANGED
data/lib/twilio/verb.rb
CHANGED
@@ -3,11 +3,11 @@ module Twilio
|
|
3
3
|
# There are 5 primary verbs (say, play, gather, record, dial) and 3 secondary (hangup, pause, redirect).
|
4
4
|
# Verbs can be chained and, in some cases, nested.
|
5
5
|
#
|
6
|
-
# If your response consists of a single verb, you can
|
6
|
+
# If your response consists of a single verb, you can call a Verb class method:
|
7
7
|
#
|
8
|
-
# Twilio::Verb.
|
8
|
+
# Twilio::Verb.say('The time is 9:35 PM.')
|
9
9
|
#
|
10
|
-
# But if you need to chain several verbs together, just wrap them in
|
10
|
+
# But if you need to chain several verbs together, just wrap them in an instance block and call the 'response' attribute:
|
11
11
|
#
|
12
12
|
# verb = Twilio::Verb.new { |v|
|
13
13
|
# v.dial('415-123-4567')
|
@@ -17,6 +17,13 @@ module Twilio
|
|
17
17
|
class Verb
|
18
18
|
|
19
19
|
attr_reader :response
|
20
|
+
|
21
|
+
class << self
|
22
|
+
def method_missing(method_id, *args) #:nodoc:
|
23
|
+
v = Verb.new
|
24
|
+
v.send(method_id, *args)
|
25
|
+
end
|
26
|
+
end
|
20
27
|
|
21
28
|
def initialize(&block)
|
22
29
|
@xml = Builder::XmlMarkup.new
|
@@ -32,24 +39,24 @@ module Twilio
|
|
32
39
|
# Say is useful for dynamic text that is difficult to prerecord.
|
33
40
|
#
|
34
41
|
# Examples:
|
35
|
-
# Twilio::Verb.
|
36
|
-
# Twilio::Verb.
|
42
|
+
# Twilio::Verb.say('The time is 9:35 PM.')
|
43
|
+
# Twilio::Verb.say('The time is 9:35 PM.', :loop => 3)
|
37
44
|
#
|
38
45
|
# With numbers, 12345 will be spoken as "twelve thousand three hundred forty five" while
|
39
46
|
# 1 2 3 4 5 will be spoken as "one two three four five."
|
40
47
|
#
|
41
|
-
# Twilio::Verb.
|
42
|
-
# Twilio::Verb.
|
48
|
+
# Twilio::Verb.say('Your PIN is 1234', :loop => 4)
|
49
|
+
# Twilio::Verb.say('Your PIN is 1 2 3 4', :loop => 4)
|
43
50
|
#
|
44
51
|
# If you need a longer pause between each loop, instead of explicitly calling the Pause
|
45
52
|
# verb within a block, you can set the convenient pause option:
|
46
53
|
#
|
47
|
-
# Twilio::Verb.
|
54
|
+
# Twilio::Verb.say('Your PIN is 1 2 3 4', :loop => 4, :pause => true)
|
48
55
|
#
|
49
56
|
# Options (see http://www.twilio.com/docs/api_reference/TwiML/say) are passed in as a hash:
|
50
57
|
#
|
51
|
-
# Twilio::Verb.
|
52
|
-
# Twilio::Verb.
|
58
|
+
# Twilio::Verb.say('The time is 9:35 PM.', :voice => 'woman')
|
59
|
+
# Twilio::Verb.say('The time is 9:35 PM.', {:voice => 'woman', :language => 'es'})
|
53
60
|
def say(*args)
|
54
61
|
options = {:voice => 'man', :language => 'en', :loop => 1}
|
55
62
|
args.each do |arg|
|
@@ -76,13 +83,13 @@ module Twilio
|
|
76
83
|
|
77
84
|
# The Play verb plays an audio URL back to the caller.
|
78
85
|
# Examples:
|
79
|
-
# Twilio::Verb.
|
80
|
-
# Twilio::Verb.
|
86
|
+
# Twilio::Verb.play('http://foo.com/cowbell.mp3')
|
87
|
+
# Twilio::Verb.play('http://foo.com/cowbell.mp3', :loop => 3)
|
81
88
|
#
|
82
89
|
# If you need a longer pause between each loop, instead of explicitly calling the Pause
|
83
90
|
# verb within a block, you can set the convenient pause option:
|
84
91
|
#
|
85
|
-
# Twilio::Verb.
|
92
|
+
# Twilio::Verb.play('http://foo.com/cowbell.mp3', :loop => 3, :pause => true)
|
86
93
|
#
|
87
94
|
# Options (see http://www.twilio.com/docs/api_reference/TwiML/play) are passed in as a hash,
|
88
95
|
# but only 'loop' is currently supported.
|
@@ -117,10 +124,10 @@ module Twilio
|
|
117
124
|
# Options (see http://www.twilio.com/docs/api_reference/TwiML/gather) are passed in as a hash
|
118
125
|
#
|
119
126
|
# Examples:
|
120
|
-
# Twilio::Verb.
|
121
|
-
# Twilio::Verb.
|
122
|
-
# Twilio::Verb.
|
123
|
-
# Twilio::Verb.
|
127
|
+
# Twilio::Verb.gather
|
128
|
+
# Twilio::Verb.gather(:action => 'http://foobar.com')
|
129
|
+
# Twilio::Verb.gather(:finishOnKey => '*')
|
130
|
+
# Twilio::Verb.gather(:action => 'http://foobar.com', :finishOnKey => '*')
|
124
131
|
#
|
125
132
|
# Gather also lets you nest the Play, Say, and Pause verbs:
|
126
133
|
#
|
@@ -150,10 +157,10 @@ module Twilio
|
|
150
157
|
# Options (see http://www.twilio.com/docs/api_reference/TwiML/record) are passed in as a hash
|
151
158
|
#
|
152
159
|
# Examples:
|
153
|
-
# Twilio::Verb.
|
154
|
-
# Twilio::Verb.
|
155
|
-
# Twilio::Verb.
|
156
|
-
# Twilio::Verb.
|
160
|
+
# Twilio::Verb.record
|
161
|
+
# Twilio::Verb.record(:action => 'http://foobar.com')
|
162
|
+
# Twilio::Verb.record(:finishOnKey => '*')
|
163
|
+
# Twilio::Verb.record(:transcribe => true, :transcribeCallback => '/handle_transcribe')
|
157
164
|
def record(*args)
|
158
165
|
options = args.shift
|
159
166
|
output { @xml.Record(options) }
|
@@ -172,9 +179,9 @@ module Twilio
|
|
172
179
|
# Options (see http://www.twilio.com/docs/api_reference/TwiML/dial) are passed in as a hash
|
173
180
|
#
|
174
181
|
# Examples:
|
175
|
-
# Twilio::Verb.
|
176
|
-
# Twilio::Verb.
|
177
|
-
# Twilio::Verb.
|
182
|
+
# Twilio::Verb.dial('415-123-4567')
|
183
|
+
# Twilio::Verb.dial('415-123-4567', :action => 'http://foobar.com')
|
184
|
+
# Twilio::Verb.dial('415-123-4567', {:timeout => 10, :callerId => '858-987-6543'})
|
178
185
|
#
|
179
186
|
# Twilio also supports an alternate form in which a Number object is nested inside Dial:
|
180
187
|
#
|
@@ -259,7 +266,7 @@ module Twilio
|
|
259
266
|
# Examples:
|
260
267
|
# If your response is only a hangup:
|
261
268
|
#
|
262
|
-
# Twilio::Verb.
|
269
|
+
# Twilio::Verb.hangup
|
263
270
|
#
|
264
271
|
# If your response is chained:
|
265
272
|
#
|
@@ -104,7 +104,7 @@ dial_with_caller_id:
|
|
104
104
|
response: <?xml version="1.0" encoding="UTF-8"?><Response><Dial callerId="858-987-6543">415-123-4567</Dial></Response>
|
105
105
|
|
106
106
|
dial_with_timeout_and_caller_id:
|
107
|
-
response: <?xml version="1.0" encoding="UTF-8"?><Response><Dial
|
107
|
+
response: <?xml version="1.0" encoding="UTF-8"?><Response><Dial callerId="858-987-6543" timeout="10">415-123-4567</Dial></Response>
|
108
108
|
|
109
109
|
dial_with_redirect:
|
110
110
|
response: <?xml version="1.0" encoding="UTF-8"?><Response><Dial>415-123-4567</Dial><Redirect>http://www.foo.com/nextInstructions</Redirect></Response>
|
data/test/twilio/verb_test.rb
CHANGED
@@ -3,23 +3,23 @@ require File.dirname(__FILE__) + '/../test_helper'
|
|
3
3
|
class VerbTest < Test::Unit::TestCase #:nodoc: all
|
4
4
|
context "A Twilio Verb" do
|
5
5
|
should "say 'hi'" do
|
6
|
-
assert_equal verb_response(:say_hi), Twilio::Verb.
|
6
|
+
assert_equal verb_response(:say_hi), Twilio::Verb.say('hi')
|
7
7
|
end
|
8
8
|
|
9
9
|
should "say 'hi' with female voice" do
|
10
|
-
assert_equal verb_response(:say_hi_with_female_voice), Twilio::Verb.
|
10
|
+
assert_equal verb_response(:say_hi_with_female_voice), Twilio::Verb.say('hi', :voice => 'woman')
|
11
11
|
end
|
12
12
|
|
13
13
|
should "say 'hola' in Spanish with female voice" do
|
14
|
-
assert_equal verb_response(:say_hi_in_spanish_with_female_voice), Twilio::Verb.
|
14
|
+
assert_equal verb_response(:say_hi_in_spanish_with_female_voice), Twilio::Verb.say('hola', {:voice => 'woman', :language => 'es'})
|
15
15
|
end
|
16
16
|
|
17
17
|
should "say 'hi' three times" do
|
18
|
-
assert_equal verb_response(:say_hi_three_times), Twilio::Verb.
|
18
|
+
assert_equal verb_response(:say_hi_three_times), Twilio::Verb.say('hi', :loop => 3)
|
19
19
|
end
|
20
20
|
|
21
21
|
should "say 'hi' three times with pause" do
|
22
|
-
assert_equal verb_response(:say_hi_three_times_with_pause), Twilio::Verb.
|
22
|
+
assert_equal verb_response(:say_hi_three_times_with_pause), Twilio::Verb.say('hi', :loop => 3, :pause => true)
|
23
23
|
end
|
24
24
|
|
25
25
|
should "say 'hi' with pause and say 'bye'" do
|
@@ -41,43 +41,43 @@ class VerbTest < Test::Unit::TestCase #:nodoc: all
|
|
41
41
|
end
|
42
42
|
|
43
43
|
should "play mp3 response" do
|
44
|
-
assert_equal verb_response(:play_mp3), Twilio::Verb.
|
44
|
+
assert_equal verb_response(:play_mp3), Twilio::Verb.play('http://foo.com/cowbell.mp3')
|
45
45
|
end
|
46
46
|
|
47
47
|
should "play mp3 response two times" do
|
48
|
-
assert_equal verb_response(:play_mp3_two_times), Twilio::Verb.
|
48
|
+
assert_equal verb_response(:play_mp3_two_times), Twilio::Verb.play('http://foo.com/cowbell.mp3', :loop => 2)
|
49
49
|
end
|
50
50
|
|
51
51
|
should "play mp3 response two times with pause" do
|
52
|
-
assert_equal verb_response(:play_mp3_two_times_with_pause), Twilio::Verb.
|
52
|
+
assert_equal verb_response(:play_mp3_two_times_with_pause), Twilio::Verb.play('http://foo.com/cowbell.mp3', :loop => 2, :pause => true)
|
53
53
|
end
|
54
54
|
|
55
55
|
should "gather" do
|
56
|
-
assert_equal verb_response(:gather), Twilio::Verb.
|
56
|
+
assert_equal verb_response(:gather), Twilio::Verb.gather
|
57
57
|
end
|
58
58
|
|
59
59
|
should "gather with action" do
|
60
|
-
assert_equal verb_response(:gather_with_action), Twilio::Verb.
|
60
|
+
assert_equal verb_response(:gather_with_action), Twilio::Verb.gather(:action => 'http://foobar.com')
|
61
61
|
end
|
62
62
|
|
63
63
|
should "gather with GET method" do
|
64
|
-
assert_equal verb_response(:gather_with_get_method), Twilio::Verb.
|
64
|
+
assert_equal verb_response(:gather_with_get_method), Twilio::Verb.gather(:method => 'GET')
|
65
65
|
end
|
66
66
|
|
67
67
|
should "gather with timeout" do
|
68
|
-
assert_equal verb_response(:gather_with_timeout), Twilio::Verb.
|
68
|
+
assert_equal verb_response(:gather_with_timeout), Twilio::Verb.gather(:timeout => 10)
|
69
69
|
end
|
70
70
|
|
71
71
|
should "gather with finish key" do
|
72
|
-
assert_equal verb_response(:gather_with_finish_key), Twilio::Verb.
|
72
|
+
assert_equal verb_response(:gather_with_finish_key), Twilio::Verb.gather(:finishOnKey => '*')
|
73
73
|
end
|
74
74
|
|
75
75
|
should "gather with num digits" do
|
76
|
-
assert_equal verb_response(:gather_with_num_digits), Twilio::Verb.
|
76
|
+
assert_equal verb_response(:gather_with_num_digits), Twilio::Verb.gather(:numDigits => 5)
|
77
77
|
end
|
78
78
|
|
79
79
|
should "gather with all options set" do
|
80
|
-
assert_equal verb_response(:gather_with_all_options_set), Twilio::Verb.
|
80
|
+
assert_equal verb_response(:gather_with_all_options_set), Twilio::Verb.gather(:action => 'http://foobar.com', :method => 'GET', :timeout => 10, :finishOnKey => '*', :numDigits => 5)
|
81
81
|
end
|
82
82
|
|
83
83
|
should "gather and say instructions" do
|
@@ -101,63 +101,63 @@ class VerbTest < Test::Unit::TestCase #:nodoc: all
|
|
101
101
|
end
|
102
102
|
|
103
103
|
should "record" do
|
104
|
-
assert_equal verb_response(:record), Twilio::Verb.
|
104
|
+
assert_equal verb_response(:record), Twilio::Verb.record
|
105
105
|
end
|
106
106
|
|
107
107
|
should "record with action" do
|
108
|
-
assert_equal verb_response(:record_with_action), Twilio::Verb.
|
108
|
+
assert_equal verb_response(:record_with_action), Twilio::Verb.record(:action => 'http://foobar.com')
|
109
109
|
end
|
110
110
|
|
111
111
|
should "record with GET method" do
|
112
|
-
assert_equal verb_response(:record_with_get_method), Twilio::Verb.
|
112
|
+
assert_equal verb_response(:record_with_get_method), Twilio::Verb.record(:method => 'GET')
|
113
113
|
end
|
114
114
|
|
115
115
|
should "record with timeout" do
|
116
|
-
assert_equal verb_response(:record_with_timeout), Twilio::Verb.
|
116
|
+
assert_equal verb_response(:record_with_timeout), Twilio::Verb.record(:timeout => 10)
|
117
117
|
end
|
118
118
|
|
119
119
|
should "record with finish key" do
|
120
|
-
assert_equal verb_response(:record_with_finish_key), Twilio::Verb.
|
120
|
+
assert_equal verb_response(:record_with_finish_key), Twilio::Verb.record(:finishOnKey => '*')
|
121
121
|
end
|
122
122
|
|
123
123
|
should "record with max length" do
|
124
|
-
assert_equal verb_response(:record_with_max_length), Twilio::Verb.
|
124
|
+
assert_equal verb_response(:record_with_max_length), Twilio::Verb.record(:maxLength => 1800)
|
125
125
|
end
|
126
126
|
|
127
127
|
should "record with transcribe" do
|
128
|
-
assert_equal verb_response(:record_with_transcribe), Twilio::Verb.
|
128
|
+
assert_equal verb_response(:record_with_transcribe), Twilio::Verb.record(:transcribe => true, :transcribeCallback => '/handle_transcribe')
|
129
129
|
end
|
130
130
|
|
131
131
|
should "dial" do
|
132
|
-
assert_equal verb_response(:dial), Twilio::Verb.
|
132
|
+
assert_equal verb_response(:dial), Twilio::Verb.dial('415-123-4567')
|
133
133
|
end
|
134
134
|
|
135
135
|
should "dial with action" do
|
136
|
-
assert_equal verb_response(:dial_with_action), Twilio::Verb.
|
136
|
+
assert_equal verb_response(:dial_with_action), Twilio::Verb.dial('415-123-4567', :action => 'http://foobar.com')
|
137
137
|
end
|
138
138
|
|
139
139
|
should "dial with GET method" do
|
140
|
-
assert_equal verb_response(:dial_with_get_method), Twilio::Verb.
|
140
|
+
assert_equal verb_response(:dial_with_get_method), Twilio::Verb.dial('415-123-4567', :method => 'GET')
|
141
141
|
end
|
142
142
|
|
143
143
|
should "dial with timeout" do
|
144
|
-
assert_equal verb_response(:dial_with_timeout), Twilio::Verb.
|
144
|
+
assert_equal verb_response(:dial_with_timeout), Twilio::Verb.dial('415-123-4567', :timeout => 10)
|
145
145
|
end
|
146
146
|
|
147
147
|
should "dial with hangup on star" do
|
148
|
-
assert_equal verb_response(:dial_with_hangup_on_star), Twilio::Verb.
|
148
|
+
assert_equal verb_response(:dial_with_hangup_on_star), Twilio::Verb.dial('415-123-4567', :hangupOnStar => true)
|
149
149
|
end
|
150
150
|
|
151
151
|
should "dial with time limit" do
|
152
|
-
assert_equal verb_response(:dial_with_time_limit), Twilio::Verb.
|
152
|
+
assert_equal verb_response(:dial_with_time_limit), Twilio::Verb.dial('415-123-4567', :timeLimit => 3600)
|
153
153
|
end
|
154
154
|
|
155
155
|
should "dial with caller id" do
|
156
|
-
assert_equal verb_response(:dial_with_caller_id), Twilio::Verb.
|
156
|
+
assert_equal verb_response(:dial_with_caller_id), Twilio::Verb.dial('415-123-4567', :callerId => '858-987-6543')
|
157
157
|
end
|
158
158
|
|
159
159
|
should "dial with timeout and caller id" do
|
160
|
-
assert_equal verb_response(:dial_with_timeout_and_caller_id), Twilio::Verb.
|
160
|
+
assert_equal verb_response(:dial_with_timeout_and_caller_id), Twilio::Verb.dial('415-123-4567', {:timeout => 10, :callerId => '858-987-6543'})
|
161
161
|
end
|
162
162
|
|
163
163
|
should "dial with redirect" do
|
@@ -189,7 +189,7 @@ class VerbTest < Test::Unit::TestCase #:nodoc: all
|
|
189
189
|
end
|
190
190
|
|
191
191
|
should "hangup" do
|
192
|
-
assert_equal verb_response(:hangup), Twilio::Verb.
|
192
|
+
assert_equal verb_response(:hangup), Twilio::Verb.hangup
|
193
193
|
end
|
194
194
|
|
195
195
|
should "say hi and hangup" do
|