Forwarding live Calls to a new Twiml from the browser - twilio

I am following the tutorial on https://www.twilio.com/docs/api/rest/change-call-state#post I am coding in php the portion that allows you to forward a current inbound call to a new Twiml URL. How do I get the inbound call Sid? Currently, the call Sid that I am retrieving forwards my browser to the new Twiml URL and hangs up the inbound caller. I think that I may have the wrong call Sid since I want to forward the current inbound caller to the new Twiml URL. Not The Browser. Can someone please give me some advice on retrieving the inbound call Sid to use in this php script? Thanks
Twilio.Device.incoming(function (conn) {
callSid = conn.parameters.CallSid;
$("#log").text("Incoming connection from " + conn.parameters.From);
// accept the incoming connection and start two-way audio
conn.accept();
});
This is how I am getting the Call Sid. If I input this Call Sid into
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('TwilioAPI/twilio-php-master/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = '';
$token = '';
$client = new Services_Twilio($sid, $token);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->account->calls->get("THE CALL SID I GOT FROM THE JS GOES HERE");
$call->update(array(
"Url" => "http://twimlets.com/message?Message%5B0%5D=I%20finally%20did%20it&",
"Method" => "POST"
));
echo $call->to;
?>
This code forwards the browser which receives the call to the new Twiml URL. Not the inbound caller.

Related

How to answer incoming call in browser using javascript and php

connection.on('incoming', function(conn) {}) function not called.
I am trying to implement the incoming call in the browser. What I tried is a javascript code
var number = $("#number").val();
params = {
"PhoneNumber": number,
"CallerId": "+13604924000",
"AgentName": "Noman Javed",
};
Twilio.Device.setup(token);
Twilio.Device.ready(function(device) {
console.log('Ready');
// ---------------------------------------------------
// Explicitly create a new outgoing connection
var connection = Twilio.Device.connect(params);
console.log('PhoneNumber: ' + params.PhoneNumber);
$('#hangup_btn_span').show();
$("#hangup_btn").show();
connection.on('error', function(error) {
console.log(error);
});
connection.on('accept', function(conn) {
});
connection.on('incoming', function(conn) {
console.log("incoming call connection object log");
console.log(conn);
console.log('Incoming connection from ' + conn.parameters.From);
// accept the incoming connection and start two-way audio
// conn.accept();
});
}
For outgoing calls, I had to add the URL of the Twiml App outgoing call URL and dial the call using rest API to hit twiml page.
For incoming calls, I had add incoming call URL in the phone number incoming call URL that is
mydomain.com/Welcome/incoming_call
The incoming call urls code is:
header('Content-Type: application/xml');
// Load the required files
require APPPATH . 'libraries/vendor/autoload.php';
use Twilio\Jwt\ClientToken;
use Twilio\Rest\Client;
use Twilio\TwiMl;
use Twilio\TwiML\VoiceResponse;
file_put_contents('request.log', "\n" . "incoming call - : " . json_encode($_REQUEST) . "\n", FILE_APPEND);
?>
<Response>
<Pause length="2"/>
<Say voice="woman">Dialing number</Say>
<Dial callerId="<?php echo $_REQUEST['From']; ?>" >
<?php echo $_REQUEST['To']; ?>
</Dial>
</Response>
How I will receive the call in the browser in this function with parameters
connection.on('incoming', function(conn) {
console.log("incoming call connection object log");
console.log(conn);
console.log('Incoming connection from ' + conn.parameters.From);
// accept the incoming connection and start two-way audio
// conn.accept();
});
Do I need to add an incoming call URL in Twiml App too or just add in the phone number incoming URL?
Thanks in advance for helping and guiding.
First, in the twiml app you have to add the url for your backend api where you have your php code and add that twiml app to your incoming number.
Now, while generating the token for your user you must have used an 'identity'. We need to use that identity value to connect the incoming call to the user. Something like below:
$response = new VoiceResponse();
$dial = $response->dial('');
$dial->client(IDENTITY_VALUE);
this dial->client will connect the incoming call to the browser.
If you have not used identity attribute in your token generation, check this url:
https://www.twilio.com/docs/iam/access-tokens?code-sample=code-creating-an-access-token-voice-2&code-language=PHP&code-sdk-version=5.x

How to transfer twilio inbound call between clients

I'm struggling with implementation of transferring inbound call between two clients.
Twilio tutorials are as informative as it possibly can but i just can't get what do i need to do to transfer inbound customer call from one client to another.
This is simplified example of my controller's method that handles inbound call.
public function inbound(): Twiml
{
$this->twiml->dial()->client('publishers');
return $this->twiml;
}
And it works great.
But the trouble comes when an agent press "Forward Call" - somehow the caller gets disconnected from the call and two clients gets connected with each other.
This is a method, that updates current call.
public function redirect(Request $request)
{
$input = $request->all();
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$client = new Client($sid, $token);
$client
->calls($input['CallSid'])
->update(array(
"method" => "POST",
"url" => "https://some-api.ngrok.io/api/connect"
)
);
}
And this is method that returns new TwiML instructions for Twilio
public function connect(): Twiml
{
$this->twiml->dial()->client('collectors');
return $this->twiml;
}
What am I doing wrong? Would appreciate any advises.
Twilio developer evangelist here.
In Twilio calls there are two legs to each call, each between Twilio and the person on the phone/client.
When you are updating the call, you are sending the callSid of the agent's call and then updating their call with the new TwiML, thus connecting your two agents.
Instead, since the call is initiated by the incoming caller, you need to find the parent call SID. You can do that by fetching your current call from the API and using the call's parent_call_sid property to update the original incoming call.
Try something like this:
public function redirect(Request $request)
{
$input = $request->all();
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$client = new Client($sid, $token);
$call = $client
->calls($input['CallSid'])
->fetch()
$client
->calls($call->parentCallSid)
->update(array(
"method" => "POST",
"url" => "https://some-api.ngrok.io/api/connect"
)
);
}

Record an outbound call in twillio

I am trying to record an outbound call using twillio php code :
Once i execute the code in browser, call will go to destination number [+919999999999].
Issue :
but once it recieved, some default voice will play and call will be disconnected....
Requirement :
But i want both pepoles [ source & destination ] should speak and want to record that conversation....
<?php
require_once '/var/www/html/ecom1/vendor/autoload.php';
use Twilio\Rest\Client;
$sid = "account_sid";
$token = "auth_token";
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create("+919999999999",
"+918888888888",
array(
"record" => True,
"url" => "http://twimlets.com/forward?PhoneNumber=%2B918888888888&"
)
);
print($call->sid);
?>
I am using Trail account....
Append the parameter "Record=true" when making a POST request to tell Twilio to record an outgoing call via the REST API.
By default, the recording will be single-channel (mono). For dual-channel recordings (two legs of the call each in separate stereo channels), append the parameter "RecordingChannels=dual".
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
$account_sid = '<account_sid value>';
$auth_token = '<auth_token value>';
$client = new Client($account_sid, $auth_token);
$calls = $client->accounts("<account id>")
->calls->create("<Valid To number>", "<Valid From number>", array(
'Method' => "POST",
'Record' => "true",
'RecordingChannels' => "dual"
));
Please Note :
When attempt to initiate an outbound phone call, ensure the URL you specified to handle the call should be valid URL.
If you specified an Application Sid for your outbound phone call, the application must have a valid VoiceUrl or the call will fail.
Hope this information helps you !

Twillo,making call with REST API,call gets disconnect after receiving

In outgoing call,when make call using REST API then call gets disconnected after reading the twiml on the my_url that we pass when creating call.
I want to know what TWIML or what i do on that my_url to keep the call go as normal call(means talk to the person who I have called).
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);
$call = $client->calls->create(
"+14155551212", "+14158675309",
array("url" => "my_url")
);
echo $call->sid;
The twiml is
<Response>
<Say>Hello </Say>
</Response>
I want to connect the same caller.Like if agent A dail a user number then I want to connect the agent A with user.and I don't want to dail another number to connect with user

Twilio inbound call status callback

I know I can set the status callback url when make a outbound call.
But how can I set a status callback url for an inbound call?
Twilio developer evangelist here.
You can either set your inbound call webhook in your Twilio Console by updating your numbers. Or you can set it via the REST API by POSTing an update to your number with the VoiceUrl parameter set to your new URL.
That would look like this in PHP:
$sid = "your_account_sid";
$token = "your_auth_token";
$client = new Services_Twilio($sid, $token);
$number = $client->account->incoming_phone_numbers->get("your_phone_number_sid");
$number->update(array(
"VoiceUrl" => "http://example.com/voice"
));
Let me know if that helps at all.

Resources