Skip to contentSkip to navigationSkip to topbar
Page tools
Looking for more inspiration?Visit the

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


To accept a Reservation using an assignment instruction, return an accept instruction from your Assignment Callback URL. In this step, you create another Task using the REST API and have your server accept the Reservation as soon as it is notified, via a synchronous HTTP response.

Before you create the next Task, make sure that the 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
1
<?php
2
// Get the Twilio-PHP library from twilio.com/docs/libraries/php,
3
// following the instructions to install it with Composer.
4
require_once "vendor/autoload.php";
5
use Twilio\Rest\Client;
6
7
// put your Twilio API credentials here
8
// To set up environmental variables, see https://twil.io/secure
9
$accountSid = getenv('TWILIO_ACCOUNT_SID');
10
$authToken = getenv('TWILIO_AUTH_TOKEN');
11
12
// Set your WorkspaceSid and WorkflowSid
13
$workspaceSid = 'WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
14
$workflowSid = 'WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
15
16
// instantiate a new Twilio Rest Client
17
$client = new Client($accountSid, $authToken);
18
19
// create a new task
20
$task = $client->taskrouter
21
->workspaces($workspaceSid)
22
->tasks
23
->create(array(
24
'attributes' => '{"selected_language": "es"}',
25
'workflowSid' => $workflowSid,
26
));
27
28
29
// display a confirmation message on the screen
30
echo "Created a new task";

and the curl command:

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

Before bringing Alice online, open your assignment.php file and modify the existing code to match the following:

1
<?php
2
3
$assignment_instruction = [
4
'instruction' => 'accept'
5
];
6
7
header('Content-Type: application/json');
8
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 in Customer Care Requests queue assigned with priority 1 and age 128.

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's status is Busy and not available as of 00:21:22 EST on 2015-02-11.

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

Next, learn shortcuts to create Tasks originating from Twilio phone calls.

Part 3: Create Tasks from Phone Calls using TwiML »