I am trying to develop a system where callers call my Twilio number and I want them to talk to my AI Voice agent in Retell. My AI voice agent will gather their first name and last name and then I want Retell to transfer the call back to Twilio for further processing.
So far I have done the following:
Twilio incoming call transfer to Retell using Retell Register call api endpoint. This step is working fine and allowing to dail sip and the call is getting transferred to Retell Voice AI agent.
Retell AI Voice agent is talking and capturing their first and last name and posting data through webhook to my server. This step is also working fine.
Now, I want the call routed back to Twilio. So I am using transfer call function in Retell to transfer the call back to Twilio. I am stuck at this point. I am transferring the call back to Twilio by transferring to a different number in Twilio. Now call in point 1 is different to this transfer which I don't want.
Twilio To Retell Transfer:
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => ";,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n \"agent_id\": \"agent_0aa908475hydtgf4ebc5406973\",\n \"from_number\": \"+19876543210\",\n \"to_number\": \"+19988776655\",\n \"direction\": \"inbound\"\n}",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer Retell_API_KEY_Here",
"Content-Type: application/json"
],
]);
$response_retell = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$response_retell = json_decode($response_retell);
$dial = $response->dial();
$dial->sip("sip address here");
}
return $response;
How can I transfer the call back to Twilio from Retell keeping everything under one call? Can someone guide me to the correct way please?