1
votes

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.

1

1 Answers

0
votes

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