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

Create Tasks from Phone Calls using TwiML: Create a TaskRouter Task using <Enqueue>


In the previous step we received a call to a Twilio phone number and prompted the caller to select a preferred language. But when the caller selected their language, we weren't ready to handle that input. Let's fix that. Create a new endpoint called 'enqueue_call' and add the following code.


TwilioTaskRouterServlet.java

twiliotaskrouterservletjava page anchor

_108
import java.io.IOException;
_108
_108
import javax.servlet.ServletException;
_108
import javax.servlet.http.HttpServlet;
_108
import javax.servlet.http.HttpServletRequest;
_108
import javax.servlet.http.HttpServletResponse;
_108
_108
import com.twilio.Twilio;
_108
import com.twilio.rest.taskrouter.v1.workspace.Task;
_108
import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;
_108
import com.twilio.twiml.*;
_108
import com.twilio.twiml.voice.*;
_108
_108
public class TwilioTaskRouterServlet extends HttpServlet {
_108
_108
private String accountSid;
_108
private String authToken;
_108
private String workspaceSid;
_108
private String workflowSid;
_108
_108
@Override
_108
public void init() {
_108
accountSid = this.getServletConfig().getInitParameter("AccountSid");
_108
authToken = this.getServletConfig().getInitParameter("AuthToken");
_108
workspaceSid = this.getServletConfig().getInitParameter("WorkspaceSid");
_108
workflowSid = this.getServletConfig().getInitParameter("WorkflowSid");
_108
_108
Twilio.init(accountSid, authToken);
_108
}
_108
_108
// service() responds to both GET and POST requests.
_108
// You can also use doGet() or doPost()
_108
@Override
_108
public void service(final HttpServletRequest request, final HttpServletResponse response)
_108
throws IOException, ServletException {
_108
if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {
_108
return;
_108
}
_108
_108
if (request.getPathInfo().equals("/assignment_callback")) {
_108
response.setContentType("application/json");
_108
response.getWriter().print("{\"instruction\":\"accept\"}");
_108
} else if (request.getPathInfo().equals("/create_task")) {
_108
response.setContentType("application/json");
_108
final String taskAttributes = createTask();
_108
response.getWriter().print(createTask());
_108
} else if (request.getPathInfo().equals("/accept_reservation")) {
_108
response.setContentType("application/json");
_108
final String taskSid = request.getParameter("TaskSid");
_108
final String reservationSid = request.getParameter("ReservationSid");
_108
response.getWriter().print(acceptReservation(taskSid, reservationSid));
_108
} else if (request.getPathInfo().equals("/incoming_call")) {
_108
response.setContentType("application/xml");
_108
response.getWriter().print(handleIncomingCall());
_108
} else if (request.getPathInfo().equals("/enqueue_call")) {
_108
response.setContentType("application/xml");
_108
response.getWriter().print(enqueueTask());
_108
}
_108
_108
}
_108
_108
public String createTask() {
_108
String attributes = "{\"selected_language\":\"es\"}";
_108
_108
Task task = Task.creator(workspaceSid).setAttributes(attributes).setWorkflowSid(workflowSid).create();
_108
_108
return "{\"task_sid\":\"" + task.getSid() + "\"}";
_108
}
_108
_108
public String acceptReservation(final String taskSid, final String reservationSid) {
_108
Reservation reservation = Reservation.updater(workspaceSid, taskSid, reservationSid)
_108
.setReservationStatus(Reservation.Status.ACCEPTED).update();
_108
_108
return "{\"worker_name\":\"" + reservation.getWorkerName() + "\"}";
_108
}
_108
_108
public String handleIncomingCall() {
_108
VoiceResponse twiml =
_108
new VoiceResponse.Builder()
_108
.gather(new Gather.Builder()
_108
.say(new Say.Builder("Para Español oprime el uno.").language(Say.Language.ES_MX)
_108
.build())
_108
.say(new Say.Builder("For English, please hold or press two.")
_108
.language(Say.Language.EN_US).build())
_108
.numDigits(1).timeout(5).build())
_108
.build();
_108
_108
try {
_108
return twiml.toXml();
_108
} catch (TwiMLException e) {
_108
return "Error creating TwiML: " + e.getMessage();
_108
}
_108
}
_108
_108
public String enqueueTask() {
_108
com.twilio.twiml.voice.Task task = new com.twilio.twiml.voice.Task.Builder("{\"selected_language\":\"es\"}").build();
_108
_108
Enqueue enqueue = new Enqueue.Builder().task(task).workflowSid(workflowSid).build();
_108
_108
VoiceResponse twiml = new VoiceResponse.Builder().enqueue(enqueue).build();
_108
_108
try {
_108
return twiml.toXml();
_108
} catch (TwiMLException e) {
_108
return "Error creating TwiML: " + e.getMessage();
_108
}
_108
}
_108
}

Now call your Twilio phone number. When prompted, press one for Spanish. You should hear Twilio's default hold music. Congratulations! You just added yourself to the 'Customer Care Requests - Spanish' Task Queue based on your selected language. To clarify how exactly this happened, look more closely at what is returned from enqueue_call to Twilio when our caller presses one:


enqueue_call - TwiML Output

enqueue_call---twiml-output page anchor

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Enqueue workflowSid="WW0123401234...">
_10
<Task>{"selected_language": "es"}</Task>
_10
</Enqueue>
_10
</Response>

Just like when we created a Task using the TaskRouter REST API (via curl), a Task has been created with an attribute field "selected_language" of value "es". This instructs the Workflow to add the Task to the 'Customer Care Requests - Spanish' TaskQueue based on the Routing Configurations we defined when we set up our Workflow. TaskRouter then starts monitoring for an available Worker to handle the Task.

Looking in the TaskRouter web portal, you will see the newly created Task in the Tasks section, and if you make an eligible Worker available, you should see them assigned to handle the Task. However, we don't yet have a way to bridge the caller to the Worker when the Worker becomes available.

In the next section, we'll use a special Assignment Instruction to easily dequeue the call and route it to an eligible Worker - our good friend Alice. For now, you can hang up the call on hold.

Next: Dequeue a Call to a Worker »


Rate this page: