twiliotwilio-apitwilio-twimltwilio-click-to-call

How make a direct phone call to exist between two customers using Twilio API


I have this idea where I need to connect customer A (who is hiring) with customer B (who is to be hired) via a direct phone call with just a push of a button, and customer B (who is to be hired) don't want anyone to just have access to his personal phone number (to avoid spam calls).

Well, to make this work I found out that Twilio can handle programmable voice calls which I implemented using ASP.NET Core but that's not exactly what i wanted because customer A (who is hiring) is not allowed to speak directly with customer B (who is to be hired) while the TwiML is at work.

Using Twilio, is there a way for these two customers to communicate via direct calls while hiding the phone number of customer B (who is to be hired) from customer A (who is hiring)? To throw more light to this, on behalf of customer A I want to place a call to customer B's phone number using Twilio's phone number. Any help will be appreciated, thank you.


Solution

  • Twilio developer evangelist here.

    You absolutely can connect two customers via a direct call while hiding customer B's number.

    I'll give you the very basics first, then suggest some ways to make it more scalable.

    To create a number that A can call that will connect A to B you need to buy that number from Twilio and configure it so that when a call comes in to A it returns TwiML that connects to B. For this initial example, you could use a TwiML Bin or some static TwiML that you host. The TwiML needs to use <Dial> and <Number>, like this:

    <Response>
      <Dial callerID="YOUR_TWILIO_NUMBER">
        <Number>Customer B's phone number</Number>
      </Dial>
    </Response>
    

    Now, when A calls the number they will get connected to B. And if you set the callerId to your Twilio number it will also appear to B to come from your Twilio number, keeping their phone number private too.

    Using hard-coded TwiML like this doesn't scale though. You have a few options to improve this.

    First, you could have A initiate the call from your application, by clicking a button. That button could trigger a call to A using the REST API and pass it the TwiML above so that when A answers the phone it then dials B. Like:

    using System;
    using Twilio;
    using Twilio.Rest.Api.V2010.Account;
    
    
    class Program
    {
        static void Main(string[] args)
        {
            // Find your Account SID and Auth Token at twilio.com/console
            // and set the environment variables. See http://twil.io/secure
            string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
            string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");
            string twilioNumber = Environment.GetEnvironmentVariable("TWILIO_NUMBER");
    
            TwilioClient.Init(accountSid, authToken);
    
            var call = CallResource.Create(
                twiml: new Twilio.Types.Twiml($"<Response><Dial callerId='{twilioNumber}'><Number>CUSTOMER_B_NUMBER</Number></Dial></Response>"),
                to: new Twilio.Types.PhoneNumber(CUSTOMER_A_NUMBER),
                from: new Twilio.Types.PhoneNumber(twilioNumber)
            );
    
            Console.WriteLine(call.Sid);
        }
    }
    

    Another alternative is to let A call directly from within your application using Twilio Client to make the phone calls from your browser.

    To dig a bit deeper, you can also use Twilio Proxy to create a session between the two customers and your Twilio number, creating a connection that would allow either of A or B to call the Twilio number and get connected to the other for the duration of the session. Under the hood Proxy works to automate the delivery of the TwiML described above whilst also maintaining the session between the users until it is done with and the number can be reused by customer A to connect to a new customer. Check out the Proxy quickstart to get a better idea how it works.

    Using one of Twilio Proxy or Twilio Client is probably the best for your application, but it depends on the interface you want to provide for your users.