ExTwiml

Contains macros to make generating TwiML from Elixir far easier and more efficient. Just import ExTwiml and go!

Examples

How to generate nested verbs, such as <Gather>:

# Options are passed before "do"
gather digits: 1, finish_on_key: "#" do
  # More verbs here ...
end

# Generates
<Gather digits="1" finishOnKey="#"></Gather>

How to generate simpler verbs, such as <Say>:

# Options are passed as the second argument
say "words to say", voice: "woman"

# Generates
<Say voice="woman">words to say</Say>

How to generate command verbs, like <Leave> or <Pause>:

# If the verb has no attributes, just write the name
# of the verb:
leave

# Generates
<Leave />

# If the verb has attributes, like <Pause>, write them
# after the name of the verb:
pause length: 5

# Generates
<Pause length="5" />

A complete example:

import ExTwiml

twiml do
  play "/assets/welcome.mp3"
  gather digits: 1 do
    say "For more menus, please press 1.", voice: "woman"
    say "To speak with a real person, please press 2.", voice: "woman"
  end
end

Produces the following string:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Play>/assets/welcome.mp3</Play>
  <Gather digits="3">
    <Say voice="woman">For more menus, please press 1.</Say>
    <Say voice="woman">To speak with a real person, please press 2.</Say>
  </Gather>
</Response>

You'd then need to render this string to the browser.

Summary

get_buffer(buff)

Get the current state of a buffer

put_buffer(buff, content)

Update the buffer by pushing a new tag onto the beginning

render(buff)

Render the contents of the buffer into a string

start_buffer(state)

Start an Agent to store a given buffer state

stop_buffer(buff)

Stop a buffer

tag(name, options \\ [], list3)

Use this macro to generate a tag not yet supported by this Twiml library. Note that you'll also need to use the text macro to include text within this tag

text(string)

Adds whatever text is given to the current Twiml buffer, unmodified. As a result, this macro is really only useful when nested inside one of the other macros provided by this module

twiml(list1)

Start creating a TwiML document. Returns the rendered TwiML as a string. See the ExTwiml documentation to see how to call TwiML verbs from within the twiml/1 macro

Functions

get_buffer(buff)

Specs:

  • get_buffer(pid) :: list

Get the current state of a buffer.

put_buffer(buff, content)

Specs:

  • put_buffer(pid, any) :: atom

Update the buffer by pushing a new tag onto the beginning.

render(buff)

Specs:

Render the contents of the buffer into a string.

start_buffer(state)

Specs:

  • start_buffer(list) :: {:ok, pid}

Start an Agent to store a given buffer state.

stop_buffer(buff)

Specs:

  • stop_buffer(pid) :: atom

Stop a buffer.

Macros

tag(name, options \\ [], list3)

Use this macro to generate a tag not yet supported by this Twiml library. Note that you'll also need to use the text macro to include text within this tag.

Examples

tag :mms, to: "1112223333", from: "2223334444" do
  text "How are you doing?"
end

Will produce the following Twiml:

<Mms to="1112223333" from="2223334444">How are you doing?</Mms>
text(string)

Adds whatever text is given to the current Twiml buffer, unmodified. As a result, this macro is really only useful when nested inside one of the other macros provided by this module.

twiml(list1)

Start creating a TwiML document. Returns the rendered TwiML as a string. See the ExTwiml documentation to see how to call TwiML verbs from within the twiml/1 macro.

Example

iex> import ExTwiml
...> twiml do
...>   say "Hello World"
...> end
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><Say>Hello World</Say></Response>"