Twilio Voice -- Does Anyone Have it Working? [solved]

If so, it would be great to see how you set up client and server. I’ve found a lot of sample code, but I haven’t gotten it to work yet.

I got it working. I had to keep the callee’s number private, i.e. not displayed on the client, and it took some figuring out to get the right Twilio setup for that.

CLIENT

async function makeOutgoingCall() {
    //your callee ID goes here
    //then the server code looks up the phone number
    //this way the callee's phone # stays private
    var params = {
        nurseId: theNavigator.current?.userId
    };

    if (device) {
        const call = await device.connect({ params });
    } else {
        console.log("Unable to make call.");
    }
}

Note: there’s other setup for the Twilio device required on the client. The standard Twilio sample code works for that.

SERVER

WebApp.handlers.use('/voice', async (req, res, next) => {
    const twiml = new twilio.twiml.VoiceResponse();
    console.log('cp #1. req?.body: ', req?.body)
    let nurseId = req?.body?.nurseId;
    const navData = await getNavigatorData(nurseId);
    const toNumber = navData?.phone_1;
    const twilioPhoneNumber = Meteor.settings.private.twilio.phoneNumber;
    console.log('toNumber: ', toNumber)

    if (toNumber) {
        const dial = twiml.dial({ callerId: twilioPhoneNumber });
        dial.number(toNumber);
    } else {
        twiml.say('The person you are calling is not available at the moment.');
    }

    res.type('text/xml');
    res.send(twiml.toString());
});

Note: the ‘/voice’ endpoint has to be specified in your TwiML app setup on Twilio.

2 Likes