Building More Powerful Voice Applications With Enhanced TwiML Bins Templates

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:
 

 

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:

 

This will end up being rendered at runtime as:

 

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:

 

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.

  • Mick Stevens

    Sorry, I must be being dim. Looking at the outbound dialler example, if you want inbound calls to +14155551212 to basically be forwarded to a selection of other numbers why would you not simply set the “Webhooks/Twiml” to point to a TwimlBin containing the rendered Twiml above?

    Or is +14155551212 not a Twilio no? Perhaps I’m misunderstanding the call scenario? You say the call to +14155551212, from +15017250604, is answered? & then Twilio makes outgoing calls to the selection of numbers, so +14155551212 (& +15017250604?) are conferenced in to the first person that answers? Sorry this seems to me to be more code for something that can just be done in a TwimlBin to begin with?

    Please explain what I’m missing? :)

    • Hey Mick,

      In that outbound dialler example, the initial call to +14155551212 (which doesn’t have to be a Twilio number, but is definitely called _from_ a Twilio number) is an outbound call generated from the REST API so you need to supply a URL to the request which defines what happens when the dialled party answers the call. In this case, you could just create a TwiML Bin that statically had the simulring TwiML.

      However, if you wanted to generate calls to other numbers and give them different numbers to simulring you would either have to create TwiML Bins for each set of numbers, or just use this dynamic TwiML Bin to update the numbers you dial with just the URL query parameters.

      Does that make more sense?

      • Mick Stevens

        Hi Phil, that does, thanks, trying it out with some stuff now! :)

  • Theodis Butler

    I had spent an entire day searching the internet to figure out how to do this with Twilio. Who would have thought it was called “simulring”. Thanks for sharing.