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.
_108import java.io.IOException;_108_108import javax.servlet.ServletException;_108import javax.servlet.http.HttpServlet;_108import javax.servlet.http.HttpServletRequest;_108import javax.servlet.http.HttpServletResponse;_108_108import com.twilio.Twilio;_108import com.twilio.rest.taskrouter.v1.workspace.Task;_108import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;_108import com.twilio.twiml.*;_108import com.twilio.twiml.voice.*;_108_108public 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:
_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.