Building More Powerful Voice Applications With Enhanced TwiML Bins Templates

February 22, 2017
Written by

Twilio Bug Logo

Today I’m excited to announce that TwiML Bins now supports Mustache templates and a new built-in function that make it even easier to build Twilio applications.

Last October we added the ability for Twilio customers to use a simple templating syntax in their TwiML Bins to build more kinds of Voice and SMS experiences without the need to operate a web server. Customers used templates in ways that we expected:

  • Call forwarding
  • SMS forwarding

And in ways that we did not, such as customized voicemail experiences.

However, there were still some use cases that seemed simple on the surface, but weren’t possible with TwiML Bins, such as forwarding incoming SIP phone calls to PSTN destinations.

How to Build a Simulring Outbound Dialer using TwiML Bins

Let’s modify the outbound dial example from our previous blog post, but this time let’s pass an array of phone numbers that we’d like to dial. This is called a Simulring and the idea is to call several phone numbers with the first person to answer being connected. Here is a snippet of Python code that implements this outbound dial:
 

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

 

Now, once Twilio dials +14155551212 and they answer the phone, Twilio will then make an HTTP request to the TwiML Bin URL that we specified in our REST API call. We can then take advantage of the new TwiML Bin templates feature 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>

 

Bridging SIP Endpoints with PSTN

Another use case that has come up often has been bridging SIP endpoints, such as a Polycom phone, with the PSTN world. For instance, 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 URI’s address, it would be great if Twilio allowed customers to pull this value out and use it in a <Dial>. With the new built-in e164 function, you can do that:

<Response>
  <Dial>{{#e164}}{{To}}{{/e164}}</Dial>
</Response>

 

We know that standing up and operating web infrastructure can get in the way of what you really care about, which is building useful and reliable communications experiences. We think that Mustache templates combined with a limited set of built-in functions that unlock well-defined Voice and SMS use cases will provide developers and non-developers alike with a powerful set of tools to build useful communications experiences with Twilio.

If you have any any questions about TwiML Bins Templates don’t hesitate to reach out to me at @crtr0 on Twitter or carter@twilio.com.