TwiML (TwiML v0.2.0) View Source

Examples

Say something:

iex> TwiML.say("Hello") |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>Hello</Say>
</Response>)

Say 2 things, one after the other:

iex> TwiML.say("Hello") |> TwiML.say("world") |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>Hello</Say>
  <Say>world</Say>
</Response>)

Say something in another voice:

iex> TwiML.say("Hello", voice: "woman") |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say voice="woman">Hello</Say>
</Response>)

Leaving the content empty for a TwiML verb, will create a TwiML element that has no body:

iex> TwiML.hangup() |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Hangup/>
</Response>)

You can embed TwiML tags into other tags using the into_* function:

iex> TwiML.say("Lets say this inside the gather")
...> |> TwiML.into_gather(language: "en-US", input: "speech")
...> |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Gather language="en-US" input="speech">
    <Say>Lets say this inside the gather</Say>
  </Gather>
</Response>)

If you have multiple TwiML tags you want to embed, that works too:

iex> TwiML.say("Hi")
...> |> TwiML.say("We cool?")
...> |> TwiML.into_gather(language: "en-US", input: "speech", hints: "yes, no")
...> |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Gather language="en-US" input="speech" hints="yes, no">
    <Say>Hi</Say>
    <Say>We cool?</Say>
  </Gather>
</Response>)

It is also possible to just include a few of the preceding tags into the body of another element. The 1 decides that we want to only put the last element into the Dial element's body:

iex> TwiML.say("Calling Yodel")
...> |> TwiML.number("+1 415-483-0400")
...> |> TwiML.into_dial(1)
...> |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>Calling Yodel</Say>
  <Dial>
    <Number>+1 415-483-0400</Number>
  </Dial>
</Response>)

The into_* functions can take the number of preceding tags, attributes or both as arguments:

iex> TwiML.identity("venkman")
...> |> TwiML.into_client(1)
...> |> TwiML.identity("stantz")
...> |> TwiML.into_client(1, method: "GET")
...> |> TwiML.into_dial(caller: "+1 415-483-0400")
...> |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial caller="+1 415-483-0400">
    <Client>
      <Identity>venkman</Identity>
    </Client>
    <Client method="GET">
      <Identity>stantz</Identity>
    </Client>
  </Dial>
</Response>)

Multiple calls to into_* functions allow building complex nested TwiML structures without losing readability in the code due to nested function calls:

iex> TwiML.identity("venkman")
...> |> TwiML.parameter(name: "first_name", value: "Peter")
...> |> TwiML.parameter(name: "last_name", value: "Venkman")
...> |> TwiML.into_client(3)
...> |> TwiML.into_dial(1)
...> |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial>
    <Client>
      <Identity>venkman</Identity>
      <Parameter name="first_name" value="Peter"/>
      <Parameter name="last_name" value="Venkman"/>
    </Client>
  </Dial>
</Response>)

Attributes can be provided both as snake_case or camelCase, but the latter is preferred as the code looks more Elixir-like.

iex> TwiML.dial("+1 415-483-0400", recordingStatusCallback: "https://example.org", recording_status_callback_method: "POST")
...> |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial recordingStatusCallback="https://example.org" recordingStatusCallbackMethod="POST">+1 415-483-0400</Dial>
</Response>)

Safe binary strings, IO Data or CDATA are supported as well. Make sure to only mark actually safe data as safe!

iex> TwiML.say({:safe, "<tag>Hello World</tag>"}) |> TwiML.to_xml()
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response>\n  <Say><tag>Hello World</tag></Say>\n</Response>"

iex> TwiML.say({:iodata, [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]}) |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>\n<Response>\n  <Say>hello world</Say>\n</Response>)

iex> TwiML.say({:cdata, "<Hello>\\<World>"}) |> TwiML.to_xml()
~s(<?xml version="1.0" encoding="UTF-8"?>\n<Response>\n  <Say><![CDATA[<Hello>\\<World>]]></Say>\n</Response>)

This also works with attributes:

iex> TwiML.say({:safe, "<tag>Hello World</tag>"}, voice: "Polly.Joanna") |> TwiML.to_xml()
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response>\n  <Say voice=\"Polly.Joanna\"><tag>Hello World</tag></Say>\n</Response>"

iex> TwiML.say({:iodata, [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]}, voice: "Polly.Joanna") |> TwiML.to_xml()
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response>\n  <Say voice=\"Polly.Joanna\">hello world</Say>\n</Response>"

iex> TwiML.say({:cdata, "<Hello>\\<World>"}, voice: "Polly.Joanna") |> TwiML.to_xml()
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response>\n  <Say voice=\"Polly.Joanna\"><![CDATA[<Hello>\\<World>]]></Say>\n</Response>"

Link to this section Summary

Functions

Link to this section Functions

Link to this function

autopilot(verbs, content, attrs)

View Source
Link to this function

client(verbs, content, attrs)

View Source
Link to this function

conference(verbs, attrs)

View Source
Link to this function

conference(verbs, content, attrs)

View Source
Link to this function

connect(verbs, content, attrs)

View Source
Link to this function

dial(verbs, content, attrs)

View Source
Link to this function

enqueue(verbs, content, attrs)

View Source
Link to this function

gather(verbs, content, attrs)

View Source
Link to this function

hangup(verbs, content, attrs)

View Source
Link to this function

identity(verbs, content, attrs)

View Source
Link to this function

into_autopilot(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_autopilot(verbs, last_n_elements, attrs)

View Source
Link to this function

into_client(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_client(verbs, last_n_elements, attrs)

View Source
Link to this function

into_conference(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_conference(verbs, last_n_elements, attrs)

View Source
Link to this function

into_connect(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_connect(verbs, last_n_elements, attrs)

View Source
Link to this function

into_dial(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_dial(verbs, last_n_elements, attrs)

View Source
Link to this function

into_enqueue(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_enqueue(verbs, last_n_elements, attrs)

View Source
Link to this function

into_gather(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_gather(verbs, last_n_elements, attrs)

View Source
Link to this function

into_hangup(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_hangup(verbs, last_n_elements, attrs)

View Source
Link to this function

into_identity(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_identity(verbs, last_n_elements, attrs)

View Source
Link to this function

into_leave(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_leave(verbs, last_n_elements, attrs)

View Source
Link to this function

into_number(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_number(verbs, last_n_elements, attrs)

View Source
Link to this function

into_parameter(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_parameter(verbs, last_n_elements, attrs)

View Source
Link to this function

into_pause(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_pause(verbs, last_n_elements, attrs)

View Source
Link to this function

into_pay(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_pay(verbs, last_n_elements, attrs)

View Source
Link to this function

into_play(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_play(verbs, last_n_elements, attrs)

View Source
Link to this function

into_prompt(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_prompt(verbs, last_n_elements, attrs)

View Source
Link to this function

into_queue(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_queue(verbs, last_n_elements, attrs)

View Source
Link to this function

into_record(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_record(verbs, last_n_elements, attrs)

View Source
Link to this function

into_redirect(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_redirect(verbs, last_n_elements, attrs)

View Source
Link to this function

into_refer(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_refer(verbs, last_n_elements, attrs)

View Source
Link to this function

into_reject(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_reject(verbs, last_n_elements, attrs)

View Source
Link to this function

into_say(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_say(verbs, last_n_elements, attrs)

View Source
Link to this function

into_sim(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_sim(verbs, last_n_elements, attrs)

View Source
Link to this function

into_sip(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_sip(verbs, last_n_elements, attrs)

View Source
Link to this function

into_siprec(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_siprec(verbs, last_n_elements, attrs)

View Source
Link to this function

into_stream(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_stream(verbs, last_n_elements, attrs)

View Source
Link to this function

into_virtual_agent(verbs, last_n_elements \\ :all)

View Source
Link to this function

into_virtual_agent(verbs, last_n_elements, attrs)

View Source
Link to this function

leave(verbs, content, attrs)

View Source
Link to this function

number(verbs, content, attrs)

View Source
Link to this function

parameter(verbs, content, attrs)

View Source
Link to this function

pause(verbs, content, attrs)

View Source
Link to this function

pay(verbs, content, attrs)

View Source
Link to this function

play(verbs, content, attrs)

View Source
Link to this function

prompt(verbs, content, attrs)

View Source
Link to this function

queue(verbs, content, attrs)

View Source
Link to this function

record(verbs, content, attrs)

View Source
Link to this function

redirect(verbs, content, attrs)

View Source
Link to this function

refer(verbs, content, attrs)

View Source
Link to this function

reject(verbs, content, attrs)

View Source
Link to this function

say(verbs, content, attrs)

View Source
Link to this function

sim(verbs, content, attrs)

View Source
Link to this function

sip(verbs, content, attrs)

View Source
Link to this function

siprec(verbs, content, attrs)

View Source
Link to this function

stream(verbs, content, attrs)

View Source
Link to this function

to_xml(verbs, opts \\ [])

View Source
Link to this function

virtual_agent(verbs, attrs)

View Source
Link to this function

virtual_agent(verbs, content, attrs)

View Source