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

Creating Tasks and Accepting Reservations: Set up the Assignment Callback URL


The basic lifecycle of a [successful] TaskRouter Task is as follows:

Task Created → eligible Worker becomes available → Worker reserved → Reservation accepted → Task assigned to Worker.

In this part of the tutorial, we'll create Tasks and observe them through each of these stages. We start by creating a Task using the Tasks REST API. First time around we accept the Task using the Reservations REST API, then we create another Task and accept it using assignment callback instructions.

(information)

Info

Both the Reservations REST API and assignment callback instructions are valid methods for accepting a Reservation; it's likely that you'll choose one or the other based on the amount of background work that must be performed by your server before it accepts or rejects a Reservation. For example, due to the amount of time required, if you were to build a user interface that allowed human agents to inspect a Task before accepting it, you would need to accept the Reservation asynchronously using the Reservations REST API.


Set up the Assignment Callback URL

set-up-the-assignment-callback-url page anchor

Whether we accept Reservations via the REST API or via assignment callback instructions, we always need an Assignment Callback URL that is reachable by TaskRouter. This is the URL at which TaskRouter will notify us when a Worker is reserved to perform a Task. Before creating any Tasks, let's get the Assignment Callback URL up and running.

Finally time to write some (albeit minimalist!) code.

We are going to write a Java servlet to respond to HTTP requests, so we can tell Twilio what to do when we receive an assignment callback.

First set up the Java servlet class that will respond to incoming requests.

TwilioTaskRouterServlet.java

twiliotaskrouterservletjava page anchor

_22
import java.io.IOException;
_22
_22
import javax.servlet.http.HttpServlet;
_22
import javax.servlet.http.HttpServletRequest;
_22
import javax.servlet.http.HttpServletResponse;
_22
_22
public class TwilioTaskRouterServlet extends HttpServlet {
_22
// service() responds to both GET and POST requests.
_22
// You can also use doGet() or doPost()
_22
@Override
_22
public void service(final HttpServletRequest request, final HttpServletResponse response)
_22
throws IOException {
_22
if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {
_22
return;
_22
}
_22
_22
if (request.getPathInfo().equals("/assignment_callback")) {
_22
response.setContentType("application/json");
_22
response.getWriter().print("{}");
_22
}
_22
}
_22
}

taskrouter/WEB-INF/web.xml

taskrouterweb-infwebxml page anchor

_19
<?xml version="1.0" encoding="ISO-8859-1"?>
_19
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
_19
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
_19
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
_19
version="2.4">
_19
_19
<display-name>Twilio TaskRouter App</display-name>
_19
_19
<servlet>
_19
<servlet-name>TwilioTaskRouterServlet</servlet-name>
_19
<servlet-class>com.twilio.TwilioTaskRouterServlet</servlet-class>
_19
</servlet>
_19
_19
<servlet-mapping>
_19
<servlet-name>TwilioTaskRouterServlet</servlet-name>
_19
<url-pattern>/taskrouter/*</url-pattern>
_19
</servlet-mapping>
_19
_19
</web-app>

This returns an empty JSON document to TaskRouter with a 200 (OK) response code. This tells TaskRouter that the assignment callback was successfully received and parsed, but that we don't want to take any action on the Reservation right now. Instead, it's implied that we will use the REST API to accept or reject the Reservation when we are ready.

Make sure your Java server is running (Tomcat, Jetty, JBoss, etc.). Your servlet will be available here: http://yourwebserver.com/<path-to-servlet>/taskrouter.

Let Twilio find your server

let-twilio-find-your-server page anchor

At this point in the tutorial, you will need to find a way to expose your server to the public Internet for Twilio to interact with your development server. Here are some helpful tools:

For this tutorial, we'll use ngrok(link takes you to an external page). After installing ngrok to your $PATH, run the following command at your terminal to start listening for requests from the outside world:

ngrok http 8080

Your local Java server is now accessible to anyone (including Twilio's servers) at your ngrok URL.

With those things working, edit your "Incoming Customer Care Requests" Workflow to point at the newly implemented Assignment Callback URL:

http://yourngrokserver.com/<path-to-servlet>/taskrouter/assignment_callback

Excellent. We're ready to create Tasks and accept Reservations.

Next: Create a Task using the REST API »


Rate this page: