Skip to main content

TeXML Bin Dynamic Content

In this guide, we’ll cover how to create dynamic parameters in TeXML Bin using Mustache Templates.

You can get started with TeXML Bin in the Mission Control Portal by following the first steps in our previous guide.

Dynamic parameters for TeXML

Mustache is a logic-less web template system used mainly for mobile and web applications. And a great way to generate instructions dynamically at runtime when creating a TeXML set of instructions.

By using Mustache Dynamic templating you can insert content into your TeXML instructions through HTTP request parameters in the webhook URL that is used to fetch your TeXML set of instructions.

For example, you could create a TeXML set of instructions that calls a phone number which is set until the HTTP request is made to fetch your TeXML instructions.

To do this you'll first need create your TeXML instructions using Mustache Templating, and set the {{PhoneNumber}} as a variable like this:

  <?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
<Number>{{PhoneNumber}}</Number>
</Dial>
</Response>
Note

After pasting the above content, kindly check and remove any new lines added

The phone number can now be replaced at runtime by setting your TeXML webhook url to have PhoneNumber as a parameter like so:

Note

Don't forget to update YOUR_API_KEY here.

    curl -X POST https://api.telnyx.com/v2/texml/calls/{connection_id} \
--data-urlencode "To=+13121230000" \
--data-urlencode "From=+13120001234" \
--data-urlencode "Url=https://www.example.com/texml.xml?PhoneNumber=+18771234567" \
--data-urlencode "StatusCallback=https://www.example.com/statuscallback-listener" \
--header "Authorization: Bearer YOUR_API_KEY"
Note

After pasting the above content, Kindly check and remove any new line added

The request parameters set by Telnyx - for example CallSid, From and To - are also available for the Mustache Template. The list of the parameters for each of the callbacks can be found on our TeXML Translator page.

Iterating through lists

TeXML Bin allows users to set arrays as parameters in the TeXML webhook url and let Mustache Template handle them.

For example, if you want the dial command to have two numbers you could add a Numbers list parameter to your callback Url.

    curl -X POST https://api.telnyx.com/v2/texml/calls/{connection_id} \
--data-urlencode "To=+13121230000" \
--data-urlencode "From=+13120001234" \
--data-urlencode "Url=https://www.example.com/texml.xml?PhoneNumbers[]=+18771234567&PhoneNumbers[]=+18771234568" \
--data-urlencode "StatusCallback=https://www.example.com/statuscallback-listener" \
--header "Authorization: Bearer APIAuthKey_fromPortal"
Note

After pasting the above content, Kindly check and remove any new line added

Then you can handle the PhoneNumbers parameter in the TeXML instructions using the following syntax:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
{{#PhoneNumbers}}
<Number>{{.}}</Number>
{{/PhoneNumbers}}
</Dial>
</Response>
Note

After pasting the above content, kindly check and remove any new lines added

This will end up being parsed as:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
<Number>+18771234567</Number>
<Number>+18771234568</Number>
</Dial>
</Response>
Note

After pasting the above content, kindly check and remove any new lines added

How to render conditional content

Conditional content is supported by using if/else statements in the TeXML instructions. You could define a set of instructions to dial an specific number depending on From parameter present in the HTTP request.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
{{#if {{From}} == +18771234567}}
<Dial>
<Number>+18771234568</Number>
</Dial>
{{#elseif {{From}} == +18771234568}}
<Dial>
<Number>+18771234567</Number>
</Dial>
{{#else}}
<Say>No valid number is present</Say>
{{/if}}
</Response>
Note

After pasting the above content, kindly check and remove any new lines added

Note that supported operators are ==, != and there is no operator for checking if the parameter value is not null.