Make a call between two numbers not registered in twilio - twilio

There's some way of make a call between two of my users? I mean... I have a twilio acount with a registered number and I have to make a call to my client "Bill" so when he answer that, the call should be redirected to another client, that Bill choosed, let's say "Joe". So, Bill click's a button and he's phone rings, he answer that and start to call Joe. When some of them hangup, the all call should be over. Have someone ever made that? Help me! And I'm sorry about my bad english
(oh yeah, I'm using php for that)

This is just something simple to get you going, you can also look at connecting to a conference room https://www.twilio.com/docs/api/twiml/conference
You will need to use the Twilio PHP Helper Library (the "Services" folder from there) download from https://www.twilio.com/docs/libraries/php#install-via-zip-file
Project structure
/
/Services
/callBill.php
/callJoe.php
callBill.php
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<!-- // calling Bill -->
</Response>
<?php
// Include the Twilio PHP library
require 'Services/Twilio.php';
// Twilio REST API version
$version = "2010-04-01";
// Set our Account SID and AuthToken
$sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// A phone number you have at Twilio
$phonenumber = '5557779999';
// The URL Twilio will request when the call is answered
$twilioRequestUrl = "http://somewebsite.xyz/callJoe.php";
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
$phonenumber, // The number of the phone initiating the call
'7779995555', // call Bill at 7779995555
$twilioRequestUrl
);
//echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
//echo 'Error: ' . $e->getMessage();
}
?>
callJoe.php
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<!-- call and connect Joe at 9995557777 with Bill's caller ID-->
<Response>
<Pause length="2"/>
<Say>Please wait, I'm calling Joe.</Say>
<Dial callerId="7779995555">9995557777</Dial>
</Response>
Request http://somewebsite.xyz/callBill.php with your browser or with a click on a button.

Related

Twilio Twilio/Rest/Calls.php error

whenever I am trying to make a conference call it says application error and I get an error in error log as :
PHP Warning: strlen() expects parameter 1 to be string, array given in /home/aan/public_html/twilio/twilio-php-4.11.0/Services/Twilio/Rest/Calls.php on line 16
Here is the code
<?php
require("twilio-php-4.11.0/Services/Twilio/Twiml.php");
if($_REQUEST['Digits'] != '1') {
header("Location: twiml.php");
die;
}
$MODERATOR = $_GET['phone'];
$response = new Services_Twilio_Twiml();
$dial = $response->dial($MODERATOR);
$dial->conference('My conference', array(
'startConferenceOnEnter' => True
));
I have already made the call and gathered the digit , but when I dial second number and try to make these as conference I get this error
I usually use TwiML for making a conference call, It's simple to implement.
$my_conference = "My Conference";
$statusCallbackUrl = "https://example.net/Welcome/conference_control"; // call back url
<Response>
<Dial>
<Conference beep="false" statusCallback="<?php echo $statusCallbackUrl; ?>"
statusCallbackEvent="start end join leave mute hold" endConferenceOnExit="true"
startConferenceOnEnter="true">
<?php echo $my_conference; ?>
</Conference>
</Dial>
</Response>
Hopefully, it will help you.
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Dial><?php echo $phone ?></Dial>
</Response>
The snippet here was OP's fix for encountering an error using twilio-php library.

Twilio handling calls

I am trying to achive is whenever I receive an text message with certain keywords, I will get a call saying that I have received a text message and press 1 to talk to the person who sent the text message.
I have done it by creating three files, these are below
Twilio.php
This receives the post request on a text message and call me
$name = $_POST['name'];
$phone = $_POST['phone'];
$client = new Services_Twilio($AccountSid, $AuthToken);
try {
// make call
$call = $client->account->calls->create(
$caller,
$number,
array("url" => "http://somewebsite.net/twilio/twiml.php?phone=$phone&name=$name")
);
} catch (Exception $e) {
echo 'Error starting phone call: ' . $e->getMessage();
}
The next file if twiml.php which handle the call and ask me if I want to speak and press 1 to speak
twiml.php
$nm = $_GET['name'];
$ph = $_GET['phone'];
$name = "Deepak";
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Say>Hello <?php echo $name ?>.</Say>
<Gather numDigits="1" action="http://somewebsite.net/twilio/call.php?phone=<?php echo $ph ?>" method="POST">
<Say>You have a text message, press 1 to speak.</Say>
</Gather>
</Response>
The third file is called if I press 1 to speak and then it dials that number : below is the code:
Call.php
<?php
if($_REQUEST['Digits'] != '1') {
header("Location: twiml.php");
die;
}
$ph = $_GET['phone'];
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Dial>+1 <?php echo $ph ?></Dial>
</Response>
I am trying to achieve is to merge these three files into one so I need not to make a Post request and I could handle the whole call in a single file or at least I can merge twiml.php and call.php
Is there a way to combine these?
Twilio developer evangelist here.
You could put it all into one file and use the URI that was requested ($_SERVER['REQUEST_URI']) to switch on the code you then run. Or, it might be more interesting to try a small PHP framework that abstracts that away. Something like Slim or Silex might work for you.
I would ask why you need to run this from one file when your feature looks like it works?

when we make call using rest api then what twiml we use on that url parameter

I am Creating the call using the rest api-
try{
// Initiate a new outbound call
$call = $this->client->calls->create(
// to call.
"num2",
// Step 5: Change the 'From' number below to be a valid Twilio number
// that you've purchased or verified with Twilio.
"num1",
array("url" => "url-tw",
'IfMachine'=>'Continue')
);
echo "Started call: " . $call->sid;
} catch(Exception $e){
echo "Error: " . $e->getMessage();
}
and on the url-tw what twiml should I use which can't disconnect the call.
Before I was handling the call using the TwiML but now I have to detect the AnsweredBy option which is only available if I make the call using the REST API so.
for now I m using the same twiml I have used before when I was making calls using the twiML like use the <Dial> which let to dial again but if I dont use any twiml it disconnect the call.So any advice where I m going wrong.
Twilio evangelist here.
The value of the url parameter should be a publicly accessible URL that returns TwiML containing the instructions that you want Twilio to execute to the caller.
Your PHP to start the call would look like:
// Initiate a new outbound call
$call = $this->client->calls->create(
"num2",
"num1",
array("url" => "http://example.com/answer.php", 'IfMachine'=>'Continue')
);
Then in answer.php, you can do two things:
Check the AnsweredBy parameter to see if Twilio detected a machine or a human.
Generate the Twiml you want to return based on that value
For example to say something different to a machine v a human, you could do something like this in your PHP:
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<?php if ($_REQUEST['AnsweredBy']=='machine'): ?>
<Response>
<Say>Hello machine</Say>
</Response>
<?php else: ?>
<Response>
<Dial>+15555555555</Dial>
</Response>
<?php endif ?>
Hope that helps.

What is the best way to add CNAM (Caller ID Name) to a SMS processed through twilio

My twilio app is just like this sample app
<?php
header('Content-Type: text/html');
?>
<Response>
<Message to="<?=$_REQUEST['PhoneNumber']?>">
<?=htmlspecialchars(substr($_REQUEST['From'] . ": " . $_REQUEST['Body'], 0, 160))?>
</Message>
</Response>
I would like to add the CNAM (Caller ID name) to the message when I send it on to the destination, but twilio support says:
We unfortunately do not expose or provide caller ID name in the API response.
Is there any work around or other way to do this?
Try this :
<?php
header("content-type: text/xml");
?>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Message from="<?php echo $_REQUEST["To"]; ?>" to="+1<?php echo $_REQUEST['FrwdNum']; ?>">
<?php echo htmlspecialchars(substr(substr($_REQUEST['From'],0,10) . ":" . $_REQUEST['Body'], 0, 160)); ?>
</Message>
</Response>
If your script is www.example.com/answer.php, then give below url as sms url for your twilio number
www.example.com/answer.php?FrwdNum=9876543210
You can also use Twilio Lookup to get the CNAM of a number.
Using the REST API, you would specify the CallerName property in your GET request:
https://www.twilio.com/docs/api/lookups#lookups-caller-name
And the output would look like:
{
"caller_name": "Caller Name",
...
...
}
Caller ID/CNAM is only displayed on landlines. Cell phones do NOT support CNAM because carriers defer to your personal contact list to populate names on inbound calls. The terminating carrier (the carrier for the number you are calling) is responsible for displaying CNAM.

Pass Inbound call between twilio clients

i have several twilio client users
Sky - Kevin - Paul
A twilio registered number connects to callsky.xml by default / that code looking like.
<Response>
<Dial>
<Agent>sky</Agent>
</Dial>
</Response>
a call comes in and goes to sky, she then wants to pass that call over to Kevin or Paul.
how is this possible ?
tried:
$caid = $_REQUEST["CallSID"];
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->get($caid);
$url = 'http://thesite.com/twiml.xml';
$call->update(
'Url' => $url,
'Method' => "GET",
);
This is executed through Ajax, when sky presses a button with the destination users name on it.
<button onclick="divert('kevin')">Kevin</button>
Also: Oddly, the update command shows as a syntax error in my IDE.
Twilio evangelist here.
The update method takes an array, so that might be causing the syntax error:
https://www.twilio.com/docs/api/rest/change-call-state
I'd also recommened checking out the AppMonitor (https://www.twilio.com/user/account/developer-tools/app-monitor) to see if Twilio is logging and errors when trying to retreive the TwiML from your URL.
The URL you point the call at can return static XML, or you ca ndynamically generate it using the PHP helper library. Here is an example of using the <Dial> verb to dial a Client instance:
$response = new Services_Twilio_Twiml;
$dial = $response->dial(NULL, array(
'callerId' => '+14152223333'
));
$dial->client('client-id');
print $response;
This will output TwiML that looks like:
<Response>
<Dial callerId="+14152223333">
<Client>client-id</Client>
</Dial>
</Response>
Hope that helps.

Resources