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:
1curl 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:
1import java.io.IOException;23import javax.servlet.http.HttpServlet;4import javax.servlet.http.HttpServletRequest;5import javax.servlet.http.HttpServletResponse;67import com.twilio.Twilio;8import com.twilio.rest.taskrouter.v1.workspace.Task;9import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;1011public class TwilioTaskRouterServlet extends HttpServlet {1213private String accountSid;14private String authToken;15private String workspaceSid;16private String workflowSid;1718@Override19public void init() {20accountSid = this.getServletConfig().getInitParameter("AccountSid");21authToken = this.getServletConfig().getInitParameter("AuthToken");22workspaceSid = this.getServletConfig().getInitParameter("WorkspaceSid");23workflowSid = this.getServletConfig().getInitParameter("WorkflowSid");2425Twilio.init(accountSid, authToken);26}2728// service() responds to both GET and POST requests.29// You can also use doGet() or doPost()30@Override31public void service(final HttpServletRequest request, final HttpServletResponse response)32throws IOException {33if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {34return;35}3637if (request.getPathInfo().equals("/assignment_callback")) {38response.setContentType("application/json");39response.getWriter().print("{\"instruction\": \"accept\"}");40} else if (request.getPathInfo().equals("/create_task")) {41response.setContentType("application/json");42response.getWriter().print(createTask());43} else if (request.getPathInfo().equals("/accept_reservation")) {44response.setContentType("application/json");45final String taskSid = request.getParameter("TaskSid");46final String reservationSid = request.getParameter("ReservationSid");47response.getWriter().print(acceptReservation(taskSid, reservationSid));48}49}5051public String createTask() {52String attributes = "{\"selected_language\":\"es\"}";5354Task task = Task.creator(workspaceSid, attributes, workflowSid).create();5556return "{\"task_sid\":\"" + task.getSid() + "\"}";57}5859public String acceptReservation(final String taskSid, final String reservationSid) {60Reservation reservation = Reservation.updater(workspaceSid, taskSid, reservationSid)61.setReservationStatus(Reservation.Status.ACCEPTED)62.update();6364return "{\"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, 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':
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.
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.