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.

Call the Create Task endpoint exposed with TwilioTaskRouterServlet again, or execute the following 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_callback method in our TwilioTaskRouterServlet. Open it and modify the existing code to reflect the following:


TwilioTaskRouterServlet.java

twiliotaskrouterservletjava page anchor

_66
import java.io.IOException;
_66
_66
import javax.servlet.http.HttpServlet;
_66
import javax.servlet.http.HttpServletRequest;
_66
import javax.servlet.http.HttpServletResponse;
_66
_66
import com.twilio.Twilio;
_66
import com.twilio.rest.taskrouter.v1.workspace.Task;
_66
import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;
_66
_66
public class TwilioTaskRouterServlet extends HttpServlet {
_66
_66
private String accountSid;
_66
private String authToken;
_66
private String workspaceSid;
_66
private String workflowSid;
_66
_66
@Override
_66
public void init() {
_66
accountSid = this.getServletConfig().getInitParameter("AccountSid");
_66
authToken = this.getServletConfig().getInitParameter("AuthToken");
_66
workspaceSid = this.getServletConfig().getInitParameter("WorkspaceSid");
_66
workflowSid = this.getServletConfig().getInitParameter("WorkflowSid");
_66
_66
Twilio.init(accountSid, authToken);
_66
}
_66
_66
// service() responds to both GET and POST requests.
_66
// You can also use doGet() or doPost()
_66
@Override
_66
public void service(final HttpServletRequest request, final HttpServletResponse response)
_66
throws IOException {
_66
if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {
_66
return;
_66
}
_66
_66
if (request.getPathInfo().equals("/assignment_callback")) {
_66
response.setContentType("application/json");
_66
response.getWriter().print("{\"instruction\": \"accept\"}");
_66
} else if (request.getPathInfo().equals("/create_task")) {
_66
response.setContentType("application/json");
_66
response.getWriter().print(createTask());
_66
} else if (request.getPathInfo().equals("/accept_reservation")) {
_66
response.setContentType("application/json");
_66
final String taskSid = request.getParameter("TaskSid");
_66
final String reservationSid = request.getParameter("ReservationSid");
_66
response.getWriter().print(acceptReservation(taskSid, reservationSid));
_66
}
_66
}
_66
_66
public String createTask() {
_66
String attributes = "{\"selected_language\":\"es\"}";
_66
_66
Task task = Task.creator(workspaceSid, attributes, workflowSid).create();
_66
_66
return "{\"task_sid\":\"" + task.getSid() + "\"}";
_66
}
_66
_66
public String acceptReservation(final String taskSid, final String reservationSid) {
_66
Reservation reservation = Reservation.updater(workspaceSid, taskSid, reservationSid)
_66
.setReservationStatus(Reservation.Status.ACCEPTED)
_66
.update();
_66
_66
return "{\"worker_name\":\"" + reservation.getWorkerName() + "\"}";
_66
}
_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 »


Rate this page: