Search code examples
node.jstwiliotwilio-twiml

How to convert a twilio twiML with templates to node.js code


I'm trying to convert a twiML with templates to node.js code using twilio npm library. More specifically I am trying to make a similar call (with node.js) to the following twiML:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Dial callerId="+302111982501">{{#e164}}{{To}}{{/e164}}</Dial>
</Response>

I have used other twiMLs in my code using twilio npm library in my code. I am having trouble with how to convert the ones which use templates in {{}} form.

UPDATE: The twiML above was used, as a voice URL link at twilio's Voice SIP domain, in order for all outbound calls from my sip domain to appear to be originated from the same number(callerId). I have changed the URL, to hit a rest api in node.js, and also have been able to respond to twilio (from the REST API) with an XML, using the code below:

const voiceResponse = new VoiceResponse();
const dial = voiceResponse.dial({ callerId: '+302111982501' }); 
dial.number('+306944444444'); 
res.status(200).contentType('text/xml') 
.send(voiceResponse.toString());

The call works great.

The problem is that I want to get the number which was dialed, the {{To}}, and put it at the dial.number(). So I have tried to read the req, which comes to my REST API, using req.body, req.params, req.query. I was not able to find data regarding the twilio call. The REST API is running at google app engine, using express.

So the updated question is:

How can I get twilio outbound information (To), from the POST request that twilio makes when voice SIP Domain is linked with a Voice URL, which hits a REST API, instead of a TwiML?


Solution

  • I finally figured it out.

    I had to add:app.use(bodyParser.urlencoded({ extended: true })); to the express rest api since the requests from twilio are of content-type: x-www-form-urlencoded.