TwiML CreationΒΆ

TwiML creation begins with the Response verb. Each successive verb is created by calling various methods on the response, such as say() or play(). These methods return the verbs they create to ease creation of nested TwiML. To finish, call the toxml() method on the Response, which returns raw TwiML.

require 'twilio-ruby'

Twilio::TwiML::Response.new do |r|
  r.Say "Hello"
end.text
<?xml version="1.0" encoding="utf-8"?>
<Response><Say>Hello</Say><Response>

The verb methods (outlined in the complete reference) take the body (only text) of the verb as the first argument. All attributes are keyword arguments.

require 'twilio-ruby'

Twilio::TwiML::Response.new do |r|
  r.Play "https://api.twilio.com/cowbell.mp3", loop: 5
end.text
<?xml version="1.0" encoding="utf-8"?>
<Response>
  <Play loop="3">https://api.twilio.com/cowbell.mp3</Play>
<Response>

Any example of nesting nouns in verbs

require 'twilio-ruby'

Twilio::TwiML::Response.new do |r|
  r.Say "hello"
  r.Gather finishOnKey: => 4 do |g|
    g.Say "world"
  end
end.text

which returns the following

<?xml version="1.0" encoding="utf-8"?>
<Response>
  <Say>Hello</Say>
  <Gather finishOnKey="4"><Say>World</Say></Gather>
</Response>