0
votes

I'm deploying Twilio on my website following Javascript Quickstart: https://www.twilio.com/docs/voice/client/javascript/quickstart

I receive multiple calls and register info to my database, if I don't want my Twilio client answers an incoming call, I can hangup using the callSid according to this url: https://www.twilio.com/docs/voice/api/call-resource?code-sample=code-update-a-call-resource-to-end-the-call&code-language=PHP&code-sdk-version=5.x

Code:

<?php 

include "../../../twilio_client/vendor/autoload.php";

use Twilio\Rest\Client;

$callsid = $_POST['callsid'];

// put your Twilio API credentials here
$accountSid = 'AC2XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$authToken  = '67XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$twilio = new Client($accountSid, $authToken);

$call = $twilio->calls($callsid)->update(array("status" => "canceled"));
$calla = $twilio->calls($callsid)->fetch();
$parentCall = $calla->parentCallSid;
$call = $twilio->calls($parentCall)->update(array("status" => "completed"));

?>

This works great, so in the same way but If I want to answer an incoming call using the callSid I tried with this according to this url: https://www.twilio.com/docs/voice/api/call-resource?code-sample=code-update-a-call-in-progress-with-twiml

Code:

<?php 

include "../../../twilio_client/vendor/autoload.php";

use Twilio\Rest\Client;

$callsid = $_POST['callsid']; //callSid
$usrclient = $_POST['usrclient'];//client connected to receive calls

// put your Twilio API credentials here
$accountSid = 'AC2XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$authToken  = '67XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$twilio = new Client($accountSid, $authToken);

$call = $twilio->calls($callsid)->update(array("twiml" => "<Response><Dial><Client>".$usrclient."</Client></Dial></Response>"));

?>

But it doesn't work, It shows me this error: [Wed Jan 15 23:14:06.493068 2020] [:error] [pid 30189] [client 190.239.139.220:18718] PHP Fatal error: Uncaught exception 'Twilio\\Exceptions\\RestException' with message '[HTTP 400] Unable to update record: Call is not in-progress.

How can I fix it?

I'd like your help.

Thanks.

2
It is challenging to fully understand your call flow, so if you have a diagram or some bullet points of how you expect the call to flow, once the web site visitor initiates the client to place the call, that would be helpful.Alan

2 Answers

0
votes

You can return TwiML without modifying the call, since the call isn't established.You returned TwiML in the Application SID Voice URL to the website side Twilio Client (twilio.js), directing it to dial your client.

0
votes

You are getting a HTTP 400 error which is a BAD REQUEST. Twilio defines it as: The data given in the POST or PUT failed validation. Inspect the response body for details. https://www.twilio.com/docs/usage/your-request-to-twilio

And you have the error: call is not in-progress

As far as I can tell, your $_POST['usrclient'] is not correct. Check your POST to ensure it is valid.

You could also test your script by changing the Twiml to something simple and calling it yourself. Then you'll know if it's the script or the usrclient

<Response><Say>Ahoy there</Say></Response>