Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Create Tasks from Phone Calls using TwiML: Create a TaskRouter Task using <Enqueue>


In the previous step we received a call to a Twilio phone number and prompted the caller to select a preferred language. But when the caller selected their language, we weren't ready to handle that input. Let's fix that. Create a new PHP file called 'enqueue-call.php' and add the following code. Substitute {WorkflowSid} for the SID of the TaskRouter Workflow you created in Part 1.


enqueue-call.php

enqueue-callphp page anchor

_19
<?php
_19
_19
$digit_pressed = $_REQUEST['Digits'];
_19
_19
if ($digit_pressed == '1') {
_19
$language = "es";
_19
} else {
_19
$language = "en";
_19
}
_19
_19
header("Content-Type: application/xml; charset=utf-8");
_19
_19
?>
_19
_19
<Response>
_19
<Enqueue workflowSid="{WorkflowSid}">
_19
<Task>{"selected_language": "<?= $language ?>"}</Task>
_19
</Enqueue>
_19
</Response>

Now call your Twilio phone number. When prompted, press one for Spanish. You should hear Twilio's default <Queue> hold music. Congratulations! You just added yourself to the 'Customer Care Requests - Spanish' Task Queue based on your selected language. To clarify how exactly this happened, look more closely at what is returned from enqueue-call.php to Twilio when our caller presses one:


enqueue-call.php - TwiML Output

enqueue-callphp---twiml-output page anchor

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Enqueue workflowSid="WW0123401234...">
_10
<Task>{"selected_language": "es"}</Task>
_10
</Enqueue>
_10
</Response>

Just like when we created a Task using the TaskRouter REST API (via curl), a Task has been created with an attribute field "selected_language" of value "es". This instructs the Workflow to add the Task to the 'Customer Care Requests - Spanish' TaskQueue based on the Routing Configurations we defined when we set up our Workflow. TaskRouter then starts monitoring for an available Worker to handle the Task.

Looking in the TaskRouter web portal, you will see the newly created Task in the Tasks section, and if you make an eligible Worker available, you should see them assigned to handle the Task. However, we don't yet have a way to bridge the caller to the Worker when the Worker becomes available.

In the next section, we'll use a special Assignment Instruction to easily dequeue the call and route it to an eligible Worker - our good friend Alice. For now, you can hang up the call on hold.

Next: Dequeue a Call to a Worker »


Rate this page: