Elby
Elby

Reputation: 1674

Twilio Caller ID set for outgoing Calls

In my web application I have used Twilio api for making calls,

params = {"PhoneNumber": 'xxxx123'};
params = {"callerId": 'aaaa987'};
params["PhoneNumber"] = 'xxxx123';
params["callerId"] = 'aaaa987';
Twilio.Device.connect(params); 

But in receiver phone/twilio call logs did not get from number aaaa987, it is some other number like xyz567 every time.

How can I set from number for Twilio outgoing calls ?

Upvotes: 1

Views: 4396

Answers (1)

Devin Rader
Devin Rader

Reputation: 10366

Twilio evangelist here.

So to make sure I understand your question:

  1. You want to use the Twilio Client for JavaScript to make a phone call from a browser to a regular PSTN phone
  2. You want to have the Client pass parameters to Twilio to set the phone number and caller ID of the call to the PSTN phone

Assuming I understand your question correctly, it looks like you are on the right track, passing an array of values into the Connect function. Now, what Twilio will do with that array is we will take each item in the array and convert it into a parameter that we send with the HTTP request we make to the Voice Request URL you have configured for your TwiML app.

Based on the tags you set for your question, it looks like you are using PHP? If that's correct then you would just set the Voice URL to a PHP page that you have hosted on the internet.

That PHP page can use the $_REQUEST object to access the HTTP parameters Twilio is including in its request and use them to dynamically generate TwiML. In your case since you want to have Twilio dial out to another phone number, you are going to need to generate TwiML containing the <Dial> verb.

Here is a sample that shows how you can use the Twilio PHP Helper library to generate TwiML:

$response = new Services_Twilio_Twiml();
$response->dial($_REQUEST['PhoneNumber'], array(
  'callerId' => $_REQUEST['callerId']
));
print $response;

In this case I am grabbing the PhoneNumber and callerId parameters from the HTTP request made by Twilio and injecting them into the dial method. This would result in the helper library generating some TwiML that looks like this:

<Response>
    <Dial callerId="aaaa987">xxxx123</Dial>
</Response>

Your PHP page returns the generated TwiML to Twilio in response to Twilios HTTP request. Twilio then executes the TwiML making cool things happen, like a phone call to the number you've specified.

If you want a bit more of a step-by-step walk-through for Twilio Client for JavaScript, I'd suggest talking a look at the Twilio Client for JavaScript Quickstart. Its available in PHP and will take you through using Client for both inbound and outbound phone calls, including setting up a server side application in PHP to generate and return TwiML and also passing parameters into that PHP file.

Hope that helps.

Upvotes: 1

Related Questions