Search code examples
node.jstwiliossml

Twilio SSML Using Runtime Functions


I am trying to add SSML text using Say-verbs in Twilio Runtime functions.

let twiml = new Twilio.twiml.VoiceResponse();
twiml.say('<prosody rate="-15%">Hello World</prosody>');

Above code results in encoded Text when rendered. Is there a way to have Twilio accept the text without encoding?

&lt;prosody rate='-15%'&gt;Hello World&lt;/prosody&gt;

I am aware of the ssml methods in the Say object, however they are rather cumbersome, and it would be easier to just write the tags as part of the Say-String.


Solution

  • Twilio developer evangelist here.

    The say method (and the other TwiML methods) are intended to make it easier to write XML responses without having to hand write and encode XML yourself. Thus these helper methods will escape the input. There isn't currently a way to have the library not escape the input and I recommend using the SSML helpers.

    If you really don't want to use the helpers, then you can just write the <Response> and <Say> by hand too. Make sure you return a Twilio.Response object with the right headers set though.

    exports.handler = function(context, event, callback) {
        const twiml = `
          <Response>
            <Say>
              <prosody rate="-15%">Hello World</prosody>
            </Say>
          </Response>
        `;
        const response = new Twilio.Response();
        response.setBody(twiml);
        response.appendHeader('Content-Type', 'application/xml');
        callback(null, response);
    }