Twilio inbound call status callback - twilio

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.

Related

How to switch an ongoing Twilio phone call to a <Pay> connector

I would like to use the < Pay> connector optionally while in an ongoing phone call. I cannot find out how to do trigger a new resource during an ongoing phone call.
You can modify an "in progress" call by passing new TwiML (XML) to execute which could contain your "<Pay>".
You must provide
the ID of the call you want to modify (the "CallSid" "CAe1644a7eed5088b159577c5802d8be38")
and an URL where Twilio will find the instructions (the "Url" "http://demo.twilio.com/docs/voice.xml")
I don't know what language you're using but in PHP with Twilio's library the code would look someting like this:
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Find your Account Sid and Auth Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
$sid = "ACc0966dd96e4d55d26ae72df4d6dc3494";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$call = $twilio->calls("CAe1644a7eed5088b159577c5802d8be38")
->update(array(
"method" => "POST",
"url" => "http://demo.twilio.com/docs/voice.xml"
)
);
print($call->to);
You can read more about this here
(https://www.twilio.com/docs/voice/tutorials/how-to-modify-calls-in-progress).

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"
)
);
}

How to make a customable message in Twilio Voice call

i've successfully make a voice call with twilio like this:
$call = $client->calls->create(
$toNumber, $fromNumber,
array("url" => "http://demo.twilio.com/docs/voice.xml")
);
the above code will call to the $toNumber with twilio demo message, when i try to use TwiML to create the dynamic message like this:
$response = new TwiML();
$response->say('Votre otp est 2234', ['voice' => 'woman', 'language' => 'fr']);
$call = $client->calls->create(
$toNumber, $fromNumber,
$response
);
i got an error like this:
[HTTP 400] Unable to create record: Url parameter is required. For
more information, see http://www.twilio.com/docs/api/twiml
Twilio developer evangelist here.
You can't send the TwiML to Twilio when you make a call like that. You need to send a URL that Twilio will send an HTTP request to when the call connects (like in the original example). So, to fix your code you should replace the demo URL with your own URL.
$call = $client->calls->create(
$toNumber, $fromNumber,
array("url" => $YOUR_URL_HERE)
);
That URL can be your own application, or something like a TwiML Bin or Twilio Function. Check out this article to see how to respond with TwiML to a request from Twilio.
I notice that you are building something for one time passwords too. You might be interested in checking out the Authy API that can implement and deliver OTP codes via calls, SMS and app.

Twilio, how to get Child Sid from Parent Sid?

please anybody has any solution regarding this? I have got parent sid from but i want child sid to get call duration. Or is it possible to get child sid separately using other parameter/ independently?
Twilio developer evangelist here.
You can find the child calls from a parent SID by using the Calls list resource and filtering by ParentCallSid.
Something like (with Node.js) this:
client.calls.list({
parentCallSid: parentCallSid
},
calls => console.log(calls);
);
And in PHP
$sid = "your_account_sid";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$calls = $twilio->calls->read(array(
"parentCallSid" => $parentCallSid
));

Forwarding live Calls to a new Twiml from the browser

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.

Resources