SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site, support.twilio.com, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to help.twilio.com, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

How to use templates with TwiML Bins

When creating a TwiML Bin, you can make use of Mustache Templates to generate some TwiML dynamically at runtime. These templates allow you to do a few interesting things:

Insert Dynamic Content

Example 1: Dynamic content from HTTP request parameters:

You can use Mustache templates to dynamically insert content from the HTTP request parameters we send when reaching out to your Twilio app for TwiML instructions. One example of this process at work would be forwarding all incoming Twilio SMS messages to your personal cell phone. To set this up, you could save and configure a TwiML Bin that looks like this:

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

Notice: The to attribute will need to be updated with your personal cell phone's number.

Whenever an incoming message hits your Twilio number, we’ll make a request to your TwiML Bin. This TwiML tells us to send a new text message to the "to" attribute, and replace the “From” and “Body” values with the corresponding values passed in our HTTP request for each message you receive.

Example 2: Dynamic content from custom HTTP request parameters:

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 code to generate a call:

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

It's important here to point out that this url is set to the URL of your TwiML Bin, with additional "Name" and "Date" attributes added at the end. Your TwiML Bin code should 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 example, 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 custom greetings for callers from specific locations. 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>

The {{#FromCity}} and {{/FromCity}} elements tell us to only execute the TwiML contained inside - the message advising the caller which city we detect - if the FromCity parameter is passed in our HTTP request to your TwiML Bin. In this scenario, a local caller should hear the custom greeting. On the other hand, an incoming call from a toll-free 800 number is non-geographic, so Twilio would not send the FromCity parameter, and the caller would not hear the custom greeting.

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>

Related Topics

Have more questions? Submit a request
Powered by Zendesk