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.
1import java.io.IOException;23import javax.servlet.ServletException;4import javax.servlet.http.HttpServlet;5import javax.servlet.http.HttpServletRequest;6import javax.servlet.http.HttpServletResponse;78import com.twilio.Twilio;9import com.twilio.rest.taskrouter.v1.workspace.Task;10import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;11import com.twilio.twiml.*;12import com.twilio.twiml.voice.*;1314public class TwilioTaskRouterServlet extends HttpServlet {1516private String accountSid;17private String authToken;18private String workspaceSid;19private String workflowSid;2021@Override22public void init() {23accountSid = this.getServletConfig().getInitParameter("AccountSid");24authToken = this.getServletConfig().getInitParameter("AuthToken");25workspaceSid = this.getServletConfig().getInitParameter("WorkspaceSid");26workflowSid = this.getServletConfig().getInitParameter("WorkflowSid");2728Twilio.init(accountSid, authToken);29}3031// service() responds to both GET and POST requests.32// You can also use doGet() or doPost()33@Override34public void service(final HttpServletRequest request, final HttpServletResponse response)35throws IOException, ServletException {36if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {37return;38}3940if (request.getPathInfo().equals("/assignment_callback")) {41response.setContentType("application/json");42response.getWriter().print("{\"instruction\":\"accept\"}");43} else if (request.getPathInfo().equals("/create_task")) {44response.setContentType("application/json");45final String taskAttributes = createTask();46response.getWriter().print(createTask());47} else if (request.getPathInfo().equals("/accept_reservation")) {48response.setContentType("application/json");49final String taskSid = request.getParameter("TaskSid");50final String reservationSid = request.getParameter("ReservationSid");51response.getWriter().print(acceptReservation(taskSid, reservationSid));52} else if (request.getPathInfo().equals("/incoming_call")) {53response.setContentType("application/xml");54response.getWriter().print(handleIncomingCall());55} else if (request.getPathInfo().equals("/enqueue_call")) {56response.setContentType("application/xml");57response.getWriter().print(enqueueTask());58}5960}6162public String createTask() {63String attributes = "{\"selected_language\":\"es\"}";6465Task task = Task.creator(workspaceSid).setAttributes(attributes).setWorkflowSid(workflowSid).create();6667return "{\"task_sid\":\"" + task.getSid() + "\"}";68}6970public String acceptReservation(final String taskSid, final String reservationSid) {71Reservation reservation = Reservation.updater(workspaceSid, taskSid, reservationSid)72.setReservationStatus(Reservation.Status.ACCEPTED).update();7374return "{\"worker_name\":\"" + reservation.getWorkerName() + "\"}";75}7677public String handleIncomingCall() {78VoiceResponse twiml =79new VoiceResponse.Builder()80.gather(new Gather.Builder()81.say(new Say.Builder("Para Español oprime el uno.").language(Say.Language.ES_MX)82.build())83.say(new Say.Builder("For English, please hold or press two.")84.language(Say.Language.EN_US).build())85.numDigits(1).timeout(5).build())86.build();8788try {89return twiml.toXml();90} catch (TwiMLException e) {91return "Error creating TwiML: " + e.getMessage();92}93}9495public String enqueueTask() {96com.twilio.twiml.voice.Task task = new com.twilio.twiml.voice.Task.Builder("{\"selected_language\":\"es\"}").build();9798Enqueue enqueue = new Enqueue.Builder().task(task).workflowSid(workflowSid).build();99100VoiceResponse twiml = new VoiceResponse.Builder().enqueue(enqueue).build();101102try {103return twiml.toXml();104} catch (TwiMLException e) {105return "Error creating TwiML: " + e.getMessage();106}107}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:
1<?xml version="1.0" encoding="UTF-8"?>2<Response>3<Enqueue workflowSid="WW0123401234...">4<Task>{"selected_language": "es"}</Task>5</Enqueue>6</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.