Search code examples
phptwiliotwilio-phptwilio-taskroutertwilio-conference

Twilio TaskRouter: Need Help in Conference Call


I use task router enqueue and dequeue instructions to connect the caller to the worker. Everything is working perfectly fine. Now I want to add another agent in the same call. For that purpose, I need to convert that call into a conference. I am using Laravel. Here is I am enqueuing the call

$taskAttributes = [
        'channel' => 'voice', // Specify that it's a voice call
        'type' => $deptName,
        'departmentId' => $department_id,
    ];
    $enqueue->task(json_encode($taskAttributes),['timeout' => 60]);
    return response($response)->header('Content-Type', 'text/xml');

That is how I am dequeuing it

return response()->json([
            "instruction" => "dequeue",
            "status_callback_events" => "initiated,ringing,answered,completed",
            "record" => "record-from-answer",
            "status_callback_url" => url('ivr/statuseventCallback'),
        ]);

What I need is to convert the in progress call to conference so I can add another person to the same call.


Solution

  • I managed to do that. Sharing here for information.

    When the Call is attended and in progress. I trigger an API to update the task

    $task = $twilio->taskrouter->v1->workspaces($workspaceSid)
                                ->tasks($taskSid)->fetch()->toArray();
    
            $existing = json_decode($task['attributes'],true);
            $new = [
                "conference" => "redirect"
            ];
            $updated = array_merge($existing,$new);
            // dd($task['attributes']);
            $task = $twilio->taskrouter->v1->workspaces($workspaceSid)
                                ->tasks($taskSid)
                                ->update([
                                        "attributes" => json_encode($updated)
                                    ]
                                );
    

    Now every event in my webhook call will have this attribute in Task Detail. I am updating first the child leg of the call then the parent leg.

    if(isset($taskAttributes['conference'])){
       $task = isset($postDate['TaskSid']) ? $postDate['TaskSid'] : '';
       $twilio = new Client($accountSid, $authToken);
       $twilio->calls($callData['call_sid'])->update([
          "method" => "POST",
           "url" => url('api/phone/addConf?tsid='.$task),
       ]);
       $twilio->calls($taskAttributes['call_sid'])->update([
           "method" => "POST",
           "url" => url('api/phone/addConf?tsid='.$task),
       ]);
    } catch (Exception $e) {
      setErrorLogs('Twilio callIsNotUpdating'.$e->getMessage());
    }             
    exit;}
    

    on this addconf just return the twiml conference.