0

Can I connect two phone numbers with twilio?

Here's my explanation about my question.

  1. I'll call agent number with my twilio phone number.
  2. If the agent picks up then call client with my twilio phone number.
  3. Here's the goal.

I want to connect agent and client if they are all available.

Is it possible?

I know about call forwarding but it can be used when agent calls me.

It seems like some source codes are available for this.

enter image description here

philnash
  • 70,667
  • 10
  • 60
  • 88
Sergey
  • 73
  • 9

1 Answers1

1

Twilio developer evangelist here.

You can absolutely connect two people in the way that you describe.

First up, to generate the call you need to use the REST API. I noticed you tagged the post with node.js, so you can make this easy on yourself by using the Twilio Node library. You need to provide 3 parameters, your Twilio number that you are dialling from, the number you want to dial and a URL. I'll come to the URL after the code you need:

var accountSid = 'YOUR_ACCOUNT_SID';
var authToken = 'YOUR_AUTH_TOKEN';
var client = require('twilio')(accountSid, authToken);

client.calls.create({
    url: 'http://example.com/connect',
    to: 'AGENT_NUMBER',
    from: 'YOUR_TWILIO_NUMBER'
}, function(err, call) {
    if (err) { console.error('There was a problem starting the call: ', err); }
    console.log(`Call with sid: ${call.sid} was started`);
});

The URL you supply should point at your application to an endpoint that will return some TwiML that tells Twilio what to do next with the call. In this case, we want to connect the call onto the client's number, so we will use <Dial>. Imagining that you are using Express as the server, your endpoint would look a bit like this:

const VoiceResponse = require('twilio').twiml.VoiceResponse;

app.post('/connect', (req, res) => {
  const response = new VoiceResponse();
  const dial = response.dial();
  dial.number('CLIENT_NUMBER');
  res.send(response.toString());
});

This TwiML will tell Twilio to connect the call to the CLIENT_NUMBER and your agent and client will be talking.

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thanks for your reply philnash. Is there any source code about this on github? – Sergey Aug 10 '17 at 16:31
  • There is actually a tutorial that walks you through all of this. It's called click to call, though you can adapt it to your own needs. The tutorial is here: https://www.twilio.com/docs/tutorials/click-to-call-node-express and the source is here on GitHub: https://github.com/TwilioDevEd/clicktocall-node – philnash Aug 10 '17 at 16:46
  • That's what I have done but I have encountered some bugs and I will fix them out. Thanks again!!! – Sergey Aug 10 '17 at 16:59
  • If you have any bugs that you can't sort, just open up a new question on Stack Overflow, include the code and what isn't working, and tag it Twilio and I'll swing by to help :) – philnash Aug 10 '17 at 17:07
  • No serious errors but how can I run that on my local? – Sergey Aug 10 '17 at 17:51
  • I recommend you take a look at [ngrok](https://ngrok.com) which is a little tool that allows you to generate a URL that can tunnel through to an application on your local machine. Here's a blog post I wrote about [using ngrok to test webhooks](https://www.twilio.com/blog/2015/09/6-awesome-reasons-to-use-ngrok-when-testing-webhooks.html). – philnash Aug 10 '17 at 17:57