twiliotwilio-twiml

Embedded javascript in Twilio Bin Response entered from Bin Console?


This code seems to embed a php snippet into a bin response:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <?php if ($_POST['From'] == "+447969123456") { ?>
    <Reject reason="busy" />
  <?php } else { ?>
    <Redirect method="GET">../other_twiml.php</Redirect>
  <?php } ?>
</Response>

What would be the xml tag to use to embed a js snippet?


Solution

  • This seems to be "normal PHP" code that is embedded in XML. It's not a TwiML feature.

    It is not possible to have embedded JS in TwiML. There is limited support for handlebars when using TwiML Bin but I would strongly advice to use use a JS-based platform to returned TwiML based on the input. Twilio Serverless would be a good option:

    exports.handler = function(context, event, callback) {
      const response = new Twilio.twiml.VoiceResponse();
    
      if (event.From === "+447969123456") {
        response.reject({ reason: "busy" });
      } else {
        response.redirect({ method: "GET" }, "../other_twiml.php");
      }
    
      callback(null, response);
    };