3
votes

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?

1
Twilio employee here. I think this code sample might help: twilio.com/docs/voice/twiml/…. If you have other TwiML that you need to create, our API docs are the best place to start: twilio.com/docs/voice/twimlCharlie Weems
Twilio developer evangelist here. It sounds like you want to insert dynamic content from HTTP request parameters, so in addition to the great links @CharlieWeems suggested, I'd also recommend this one: support.twilio.com/hc/en-us/articles/….lizziepika
Thanks a lot for your answers. I had already read the links you have posted above. I have already used twilio documentation and have developed working code, communicating with twilio. The thing is that I haven't found in the documentation a way to emulate twiML with templates ({{}}) in node.js, meaning how to write twiMLs as the one I posted above, or twiMLs like the ones at lizziepika's link (in node). Is there any example in the twilio documentation which I haven't found yet?Antonis S
There was a development. Please read the UPDATE section of my post.Antonis S

1 Answers

1
votes

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.