twiliotwilio-twimltwilio-programmable-voice

Client-to-Client VoIP Voice SDK Call with Twilio


I am trying to make a VoIP client-to-client call using Twilio's Voice SDK. So far I am able to have my iPhone dial my Android phone and vice-versa but can only hear an automated voice say the "to" client's identity instead of both phones being able to just talk to each other. Am I missing something here?

For both platforms, I am just using the sample repos Twilio provided. For the server, I am using Twilio's sample NodeJS server-less code.

// /place-call

...

var url = 'https://' + context.DOMAIN_NAME + '/incoming';
const client = context.getTwilioClient();

client.calls.create({
    url: url,
    to: 'client:' + to,
    from: event.From,
}, function(err, result) {    
    // End our function
    if (err) {
        callback(err, null);
    } else {
        callback(null, "<Response/>");
    }
});

...

// /incoming

...

const toClient = (event.To).replace('client:', '');
const twiml = new Twilio.twiml.VoiceResponse();

const dial = twiml.dial();
const client = dial.client();
client.identity(toClient);
callback(null, twiml.toString());

...

When I look at the logs, I see the "/incoming" endpoint returning:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Dial>
        <Client>
            <Identity>android_user</Identity>
        </Client>
    </Dial>
</Response>

Solution

  • Twilio developer evangelist here.

    The issue is that you are returning the TwiML as a string and Twilio Functions then deem the response to be of Content-Type text/plain. Twilio then reads the content as if it was a <Say>.

    It is an easy fix though. Change:

    callback(null, twiml.toString());
    

    to

    callback(null, twiml);
    

    Twilio Functions will recognise that the twiml variable is of type VoiceResponse and set the Content-Type to text/xml and your application will work as expected.