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

Creating Tasks and Accepting Reservations: Accept a Reservation using Assignment Callback Instructions


Remember when we created a Task and accepted it using the Reservations subresource of the REST API? I do. And it was grand.

This time, we'll create another Task, again using the REST API, but we will have our server accept the Reservation as soon as it is notified, via a synchronous HTTP response.

Before we create the next Task, once again make sure that our Worker Alice is in a non-available Activity state.

Repeat the commands from step 2 to create another Task that only Alice is eligible to handle. Here's our PHP file from earlier:

create-task.php

create-taskphp page anchor

_30
<?php
_30
// Get the Twilio-PHP library from twilio.com/docs/libraries/php,
_30
// following the instructions to install it with Composer.
_30
require_once "vendor/autoload.php";
_30
use Twilio\Rest\Client;
_30
_30
// put your Twilio API credentials here
_30
// To set up environmental variables, see http://twil.io/secure
_30
$accountSid = getenv('TWILIO_ACCOUNT_SID');
_30
$authToken = getenv('TWILIO_AUTH_TOKEN');
_30
_30
// Set your WorkspaceSid and WorkflowSid
_30
$workspaceSid = 'WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
_30
$workflowSid = 'WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
_30
_30
// instantiate a new Twilio Rest Client
_30
$client = new Client($accountSid, $authToken);
_30
_30
// create a new task
_30
$task = $client->taskrouter
_30
->workspaces($workspaceSid)
_30
->tasks
_30
->create(array(
_30
'attributes' => '{"selected_language": "es"}',
_30
'workflowSid' => $workflowSid,
_30
));
_30
_30
_30
// display a confirmation message on the screen
_30
echo "Created a new task";

and the curl command:


_10
curl https://taskrouter.twilio.com/v1/Workspaces/{WorkspaceSid}/Tasks \
_10
--data-urlencode Attributes='{"selected_language": "es"}' \
_10
-d WorkflowSid={WorkflowSid} \
_10
-u {AccountSid}:{AuthToken}

This time, before bringing Alice online, we need to make changes to our assignment.php file. Open it and modify the existing code to reflect the following:


assignment.php

assignmentphp page anchor

_10
<?php
_10
_10
$assignment_instruction = [
_10
'instruction' => 'accept'
_10
];
_10
_10
header('Content-Type: application/json');
_10
echo json_encode($assignment_instruction);

Instead of returning an empty JSON document as before, we've included an 'assignment instruction' in our response. The 'accept' assignment instruction tells TaskRouter to automatically accept the Reservation and assign the Task to the Worker it has been reserved for.

To kick this process off, we need to transition Alice to an available Activity. With your Workspace open in the TaskRouter web portal(link takes you to an external page), click 'Workers' then click to edit Alice and set her Activity to 'Idle'.

Now, click 'Tasks' in the main navigation, and you should see that the Task has an Assignment Status of 'assigned':

Task is Assigned to Alice.

What actually happened is that Alice was reserved for a very short period of time. TaskRouter made a request to your web server at the Assignment Callback URL, and your server told TaskRouter to accept the Reservation. At that point, Alice's Activity transitioned to the 'Assignment Activity' of the TaskQueue that assigned the Task, as it did in the previous step.

Alice is now Busy.

And that's that. We created another Task using the REST API, accepted it via an assignment instruction at our Workflow's Assignment Callback URL and saw that this immediately accepted the Reservation for our Worker.

Onward! Next we learn about shortcuts to create Tasks originating from Twilio phone calls.

Part 3: Create Tasks from Phone Calls using TwiML »


Rate this page: