Recall the TaskRouter Task lifecycle:
Task Created → eligible Worker becomes available → Worker reserved → Reservation accepted → Task assigned to Worker.
Before we create our first Task, make sure that our Worker Alice is in a non-available Activity state. Bob's Activity state won't matter right now, as we will create a Spanish language Task that he is not eligible to handle.
With your Workspace open in the TaskRouter web portal, click 'Workers' then click to edit Alice and set her Activity to 'Offline'. Your Workers should look like this:
To simulate reality, we'll create a Task using the REST API rather than the web portal. We'll add on to our TwilioTaskRouterServlet
to create a task with our web server. Replace the {} with your Twilio AccountSid, Twilio AuthToken, WorkspaceSid, and WorkflowSid in your web.xml
.
Download and extract Twilio's Java helper library. Install the latest jar with dependencies into your web application library directory (for Tomcat this is taskrouter/WEB-INF/lib
).
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;91011public class TwilioTaskRouterServlet extends HttpServlet {12private String accountSid;13private String authToken;14private String workspaceSid;15private String workflowSid;1617@Override18public void init() {19accountSid = this.getServletConfig().getInitParameter("AccountSid");20authToken = this.getServletConfig().getInitParameter("AuthToken");21workspaceSid = this.getServletConfig().getInitParameter("WorkspaceSid");22workflowSid = this.getServletConfig().getInitParameter("WorkflowSid");2324Twilio.init(accountSid, authToken);25}2627// service() responds to both GET and POST requests.28// You can also use doGet() or doPost()29@Override30public void service(final HttpServletRequest request, final HttpServletResponse response)31throws IOException {32if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {33return;34}3536if (request.getPathInfo().equals("/assignment_callback")) {37response.setContentType("application/json");38response.getWriter().print("{}");39} else if (request.getPathInfo().equals("/create_task")) {40response.setContentType("application/json");41response.getWriter().print(createTask());42}43}4445public String createTask() {46String attributes = "{\"selected_language\":\"es\"}";4748Task task = Task.creator("WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")49.setAttributes("{\"type\":\"support\"}")50.setWorkflowSid("WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")51.create();5253return "{\"task_sid\":\"" + task.getSid() + "\"}";54}55}
1<?xml version="1.0" encoding="ISO-8859-1"?>2<web-app xmlns="http://java.sun.com/xml/ns/j2ee"3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"5version="2.4">67<display-name>Twilio TaskRouter App</display-name>89<servlet>10<servlet-name>TwilioTaskRouterServlet</servlet-name>11<servlet-class>com.twilio.TwilioTaskRouterServlet</servlet-class>12<init-param>13<param-name>AccountSid</param-name>14<param-value>{{ account_sid }}</param-value>15</init-param>16<init-param>17<param-name>AuthToken</param-name>18<param-value>{{ auth_token }}</param-value>19</init-param>20<init-param>21<param-name>WorkspaceSid</param-name>22<param-value>{{ workspace_sid }}</param-value>23</init-param>24<init-param>25<param-name>WorkflowSid</param-name>26<param-value>{{ workflow_sid }}</param-value>27</init-param>28</servlet>2930<servlet-mapping>31<servlet-name>TwilioTaskRouterServlet</servlet-name>32<url-pattern>/taskrouter/*</url-pattern>33</servlet-mapping>3435</web-app>
Alternatively, we can also create a Task using the command line utility curl, which should exist on any Mac or Linux workstation. Again, remember to replace the {} with your matching credentials and Sids, then execute the following command at your terminal:
1curl https://taskrouter.twilio.com/v1/Workspaces/{WorkspaceSid}/Tasks \2--data-urlencode Attributes='{"selected_language": "es"}' \3-d WorkflowSid={WorkflowSid} \4-u {AccountSid}:{AuthToken}
You can find your Twilio AccountSid and AuthToken on the TaskRouter Getting Started page by clicking 'show API credentials'.
If you don't have curl, you can run this request using an HTTP test tool or using the Task creation dialog in the TaskRouter web portal: with your Workspace open, click 'Tasks' then 'Create Task'.
To see our newly created Task in the TaskRouter web portal, with your Workspace open, click 'Tasks' in the main navigation. Notice that the Task has been added to the "Customer Care Requests - Spanish" Task Queue based on the Attributes we provided in the curl request. The Assignment Status is 'pending' because there is no available Worker that matches the Task Queue:
Look again at the TaskRouter Task lifecycle:
Task Created → eligible Worker becomes available → Worker reserved → Reservation accepted → Task assigned to Worker.
The first stage - 'Task Created' - is complete. To trigger an automatic Task Reservation, the next step is to bring en eligible Worker (in this case Alice) online.
With your Workspace open in the TaskRouter web portal, click 'Workers', then click to edit Alice and set her Activity to 'Idle':
When you hit save, Twilio will create a Reservation between Alice and our Task and you will receive a Webhook request at the Assignment Callback URL that we set up in the previous step. If you're using ngrok, open http://localhost:4040
in your web browser to see a detailed log of the request that Twilio made to your server, including all the parameters that your server might use to determine whether to accept a Reservation:
We're now one step further along the Task Reservation lifecycle:
Task Created → eligible Worker becomes available → Worker reserved → Reservation accepted → Task assigned to Worker.
Time to accept the Reservation.