node.jstwiliotwilio-api

How to use Twilio Proxy in Node.js


I've been trying for 3 days to properly setup a backend that will connect two phones anonymously for voice calling, with the help of Twilio. This is what I'm trying to do:

1

I've watched the live coding example and it doesn't work, at all.

  1. I've installed the gem using `gem install twilio-ruby -v 5.25.0'
  2. Entered the REPL
  3. And started to follow the video:

$ irb

require 'twilio-ruby' client = Twilio::REST::Client('...', '...') client.service.create(friendly_name: 'Test 1') NoMethodError: undefined method `service' for #

Ok ok, the video is from 2 years ago, it's fair that the SDK changed. So I followed the interactive tutorial that is available in the website, this one.

Before continuing... there is a dedicated page about the new Proxy feature, and in one of the sections there is the following image:

enter image description here

This is clearly Python, so here I go... I've installed the Python SDK, started a REPL and tried to follow the code... it doesn't work. Damn Twilio, update your things! (Or am I the stupid here?)...

Anyway... let's jump into the node.js tutorial, I'm sure at least the interative tutorial is working (so I thought).

So, I went to my console and created a service there, then I wrote the code:

import twilio from 'twilio'

const client = twilio('accountSid', 'authToken')

async function test() {
  // create session
  const session = await client.proxy.services('<MY_SERVICE>')
            .sessions
            .create({uniqueName: 'MyFirstSession'})

  // add participant 1 to session
  client.proxy.services('<MY_SERVICE>')
            .sessions(session.sid)
            .participants
            .create({ friendlyName: 'Me', identifier: '<my_number>'})

  // add participant 2 to session
  client.proxy.services('<MY_SERVICE>')
            .sessions(session.sid)
            .participants
            .create({ friendlyName: 'Me 2', identifier: '<my_number_2>'})
}

test()

Then what?

The docs say:

If your Twilio Phone Numbers are voice capable, you're now ready for a proxied voice conversation. Following the names from the previous steps, get Alice to make a call to her Proxy Identifier number. Twilio's Proxy service will then make a call from Bob's Proxy Number to his real number and connect the two calls.

It doesn't work... I've tried to call to the proxy identifier number, and it doesn't work.

I'm not sure what to do anymore, I've tried to reach out to them, nobody replies...


Solution

  • I looked at the "node.js tutorial" and I can see how that can be confusing.

    Here is what you can do to get this working and get a better understanding of how it works:


    Prerequisites:


    Step 1.

    Step 2.


    Step 3.


    Step 4.


    Step 5.

    This is the step where you add the participants, which is about your two phone numbers. This part can't be done on the Twilio's console as far as I know. I did it running Node.js code.

    You need to have Node.js installed on your computer (https://nodejs.org/en/), then:

    Here is the code for index.js

    const accountSid = 'ACxxxxx...';
    const authToken = 'xxxxx...';
    const client = require('twilio')(accountSid, authToken);
    
    client.proxy.services('KSxxxxx...')
        .sessions('KCxxxxx...')
        .participants
        .create({ friendlyName: 'Alice', identifier: '+19997775555' })
        .then(participant => console.log(participant.proxyIdentifier));  
    

    In the code above, replace the values for

    Next run this code to add the first participant, in the Terminal run node index.js

    Last thing is to add the second participant, in the code above change the Alice to let's say Bob and also replace your first phone number with your second phone number, then run node index.js again.


    After you added the second participant, you're ready. Now you can try to call or text your Twilio phone number (which acts as a proxy) from any of your numbers (phones) and the other of your numbers (phones) will receive a text or ring.


    Note: the things that you did on the Twilio console, can be done with Node.js code, read the tutorial again and try to understand the steps and how the code works.