Skip to contentSkip to navigationSkip to topbar
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.

Call the Create Task endpoint exposed with TwilioTaskRouterServlet again, or execute the following 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}

This time, before bringing Alice online, we need to make changes to our assignment_callback method in our TwilioTaskRouterServlet. Open it and modify the existing code to reflect the following:


TwilioTaskRouterServlet.java

twiliotaskrouterservletjava page anchor
1
import java.io.IOException;
2
3
import javax.servlet.http.HttpServlet;
4
import javax.servlet.http.HttpServletRequest;
5
import javax.servlet.http.HttpServletResponse;
6
7
import com.twilio.Twilio;
8
import com.twilio.rest.taskrouter.v1.workspace.Task;
9
import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;
10
11
public class TwilioTaskRouterServlet extends HttpServlet {
12
13
private String accountSid;
14
private String authToken;
15
private String workspaceSid;
16
private String workflowSid;
17
18
@Override
19
public void init() {
20
accountSid = this.getServletConfig().getInitParameter("AccountSid");
21
authToken = this.getServletConfig().getInitParameter("AuthToken");
22
workspaceSid = this.getServletConfig().getInitParameter("WorkspaceSid");
23
workflowSid = this.getServletConfig().getInitParameter("WorkflowSid");
24
25
Twilio.init(accountSid, authToken);
26
}
27
28
// service() responds to both GET and POST requests.
29
// You can also use doGet() or doPost()
30
@Override
31
public void service(final HttpServletRequest request, final HttpServletResponse response)
32
throws IOException {
33
if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {
34
return;
35
}
36
37
if (request.getPathInfo().equals("/assignment_callback")) {
38
response.setContentType("application/json");
39
response.getWriter().print("{\"instruction\": \"accept\"}");
40
} else if (request.getPathInfo().equals("/create_task")) {
41
response.setContentType("application/json");
42
response.getWriter().print(createTask());
43
} else if (request.getPathInfo().equals("/accept_reservation")) {
44
response.setContentType("application/json");
45
final String taskSid = request.getParameter("TaskSid");
46
final String reservationSid = request.getParameter("ReservationSid");
47
response.getWriter().print(acceptReservation(taskSid, reservationSid));
48
}
49
}
50
51
public String createTask() {
52
String attributes = "{\"selected_language\":\"es\"}";
53
54
Task task = Task.creator(workspaceSid, attributes, workflowSid).create();
55
56
return "{\"task_sid\":\"" + task.getSid() + "\"}";
57
}
58
59
public String acceptReservation(final String taskSid, final String reservationSid) {
60
Reservation reservation = Reservation.updater(workspaceSid, taskSid, reservationSid)
61
.setReservationStatus(Reservation.Status.ACCEPTED)
62
.update();
63
64
return "{\"worker_name\":\"" + reservation.getWorkerName() + "\"}";
65
}
66
}

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 »

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.