I’m working on getting Twilio app-to-phone voice calls working.
Here’s my current flow.
- Client clicks the “Make Call” button on my website.
- My site sets up the Twilio device and calls
const call = await twilioDevice.connect({ params });
- On my server, the endpoint specified in Twilio App-> Voice Configuration ->Request URL is hit. This endpoint is called “/make-calls” (This all seems to work as expected up to this point).
- My “make-calls” endpoint calls
twilioClient.calls.create()
and returns this xml to Twilio:
<?xml version="1.0" encoding="UTF-8"?><Response>
<Connect>true</Connect>
<Play>true</Play>
</Response>
- I get a Twilio error saying: “”XML Validation warning”,”line”:”2″,”parserMessage”:” Element ‘Connect’ cannot have character [children]”,”ErrorCode”:”12200″”
Questions:
-
How can I correct this xml?
-
Is it okay to have
twilioClient.calls.create()
in that endpoint, or is it supposed to be somewhere else?Here’s the code from my “make-calls” endpoint:
console.log('-----') console.log('/make-calls. req?.body:', req?.body); let nurseId = req?.body?.nurseId; const navData = await getNavigatorData(nurseId); const toNumber = navData?.phone_1; let nurseName = navData?.name_first; const twilioPhoneNumber = Meteor.settings.private.twilio.phoneNumber; console.log('toNumber:', toNumber); let xml = ''; const twilioClient = twilio(accountSid, authToken); try { const call = await twilioClient.calls.create({ url: `${baseUrl}twiml`, to: toNumber, from: twilioPhoneNumber, // twiml: twimlString, machineDetection: 'Enable', AsyncAmdStatusCallback: `${baseUrl}handle-amd-result`, statusCallbackEvent: ['initiated', 'ringing', 'answered', 'completed'], statusCallbackMethod: 'POST', machineDetectionTimeout: 30 }) let responseObject = { Response: { Connect: true, Play: true } } xml = "<?xml version="1.0" encoding="UTF-8"?>" + xmlJs.js2xml(responseObject, xmlJsOptions); console.log('xml: ', xml) res.set('Content-Type', 'text/xml'); res.status(200).send(xml); } catch (err) { let responseObject = { Response: { Success: false, Message: 'Failed to initiate call', Error: err.message } } xml = "<?xml version="1.0" encoding="UTF-8"?>" + xmlJs.js2xml(responseObject, xmlJsOptions); res.set('Content-Type', 'text/xml'); res.status(500).send(xml); }