How to use templates with TwiML Bins

When creating a TwiML Bin, you can make use of Mustache Templates to generate TwiML dynamically at runtime.  

The templates allow you to do a few interesting things:

  • Insert dynamic content
  • Render conditional content
  • Iterate through lists
  • Execute built-in functions

Insert Dynamic Content

Let’s imagine, for instance, that you would like to receive an SMS on your Twilio number before forwarding it to your personal cell phone. To do so, you could save and configure a TwiML Bin that looks like this:

<Response>
<Message to="+12065551212">{{From}}: {{Body}}</Message>
</Response>

When someone texts your Twilio number, we’ll make a request to your TwiML Bin, replacing the “From” and “Body” values with the corresponding values passed-in via the HTTP request for each message you receive.

You might want to pass in a custom parameter other than the standard Twilio parameters to your TwiML Bin. Here’s an example of how you can create a simple appointment reminder application that can accept some custom parameters in order to tailor the Voice experience.

You’ll first want to create an array or database with your clients’ names and appointment times. You can then use the following Python to generate a call:

client.calls.create( url="https://handler.twilio.com/twiml/EHxxx?Name=Jane&Date=Friday",
to="+14155551212",
from_="+15017250604")

The "url" parameter is set to the URL of your TwiML Bin. Your TwiML Bin would then look something like this:

<Response>
<Say>Hi, {{Name}}. This is a reminder that you have an appointment with Home Electric on {{Date}}</Say>
</Response>

When this API request above is executed, Jane will receive a call greeting her by name with the date of her appointment. Using this principle, you can create any sort of dynamic values that you’d like to pass to your TwiML Bin to handle more customized messages.

Render Conditional Content

There are times when you only want to render TwiML if certain conditions are met. Imagine for instance that you'd like to craft a custom greeting for a caller if you know where they are calling from. Using a conditional statement, you could specify a generic greeting if the data is unavailable and a more customized greeting if it is present:

<Response>
 <Say>Thank you for calling Bank 123.</Say>
 {{#FromCity}}
 <Say>We see you are calling from {{FromCity}}</Say>
 {{/FromCity}}
</Response>

Iterate Through Lists

If an array of parameters is passed into a TwiML Bin, you can easily iterate through the values. Let's build a simple Simulring using this feature. First, let's make a call to the REST API and pass 3 phone numbers as parameters:

query = "Dest[]=+12025551212&Dest[]=+17035551212&Dest[]=+12065551212”
client.calls.create(
   url="https://handler.twilio.com/twiml/EHxxx?" + query,
   to="+14155551212",
   from_="+15017250604")

We can then take advantage of the new TwiML Bin templates to convert the array of HTTP parameters into a list of <Number> nouns:

<Response>
 <Dial>
 {{#Dest}}
   <Number>{{.}}</Number>
 {{/Dest}}
 </Dial>
</Response>

This will end up being rendered at runtime as:

<Response>
 <Dial>
   <Number>+12025551212</Number>
   <Number>+17035551212</Number>
   <Number>+12065551212</Number>
 </Dial>
</Response>

Execute Built-in Functions

The last feature to share is access to built-in functions. The first such built-in function to ship makes it easy to pull a phone number out of parameters and reformat as E.164. This is very helpful when bridging SIP endpoints such as a Polycom phone with the PSTN world.

For example, let's say a Voice call comes in to a SIP Interface with a To value of sip:+15125551212@mydomain.sip.us1.twilio.com. Since the e.164 phone number is embedded in the SIP endpoint's address, you can use the new built-in e164 function to pull out this phone number and craft the proper TwiML to forward the call:

<Response>
 <Dial>{{#e164}}{{To}}{{/e164}}</Dial>
</Response>
Have more questions? Submit a request
Powered by Zendesk