We've seen how to create Tasks using the TaskRouter REST API and how to accept a Task Reservation using both the REST API and Assignment Callback instructions. TaskRouter also introduces new TwiML instructions that you can use to create a Task from a Twilio phone call.
To receive an incoming phone call, we first need a Twilio phone number. In this example we'll use a US toll-free number, but you can use a Voice capable number from any country.
Before purchasing or setting up the phone number, we need to add on to our TwilioTaskRouterServlet
to handle incoming calls:
_90import java.io.IOException;_90_90import javax.servlet.http.HttpServlet;_90import javax.servlet.http.HttpServletRequest;_90import javax.servlet.http.HttpServletResponse;_90_90import com.twilio.Twilio;_90import com.twilio.rest.taskrouter.v1.workspace.Task;_90import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;_90import com.twilio.twiml.Gather;_90import com.twilio.twiml.Say;_90import com.twilio.twiml.TwiMLException;_90import com.twilio.twiml.VoiceResponse;_90_90public class TwilioTaskRouterServlet extends HttpServlet {_90_90 private String accountSid;_90 private String authToken;_90 private String workspaceSid;_90 private String workflowSid;_90_90 @Override_90 public void init() {_90 accountSid = this.getServletConfig().getInitParameter("AccountSid");_90 authToken = this.getServletConfig().getInitParameter("AuthToken");_90 workspaceSid = this.getServletConfig().getInitParameter("WorkspaceSid");_90 workflowSid = this.getServletConfig().getInitParameter("WorkflowSid");_90_90 Twilio.init(accountSid, authToken);_90 }_90_90 // service() responds to both GET and POST requests._90 // You can also use doGet() or doPost()_90 @Override_90 public void service(final HttpServletRequest request, final HttpServletResponse response)_90 throws IOException {_90 if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {_90 return;_90 }_90_90 if (request.getPathInfo().equals("/assignment_callback")) {_90 response.setContentType("application/json");_90 response.getWriter().print("{\"instruction\":\"accept\"}");_90 } else if (request.getPathInfo().equals("/create_task")) {_90 response.setContentType("application/json");_90 response.getWriter().print(createTask());_90 } else if (request.getPathInfo().equals("/accept_reservation")) {_90 response.setContentType("application/json");_90 final String taskSid = request.getParameter("TaskSid");_90 final String reservationSid = request.getParameter("ReservationSid");_90 response.getWriter().print(acceptReservation(taskSid, reservationSid));_90 } else if (request.getPathInfo().equals("/incoming_call")) {_90 response.setContentType("application/xml");_90 response.getWriter().print(handleIncomingCall());_90 }_90 }_90_90 public String createTask() {_90 String attributes = "{\"selected_language\":\"es\"}";_90_90 Task task = Task.creator(workspaceSid, attributes, workflowSid).create();_90_90 return "{\"task_sid\":\"" + task.getSid() + "\"}";_90 }_90_90 public String acceptReservation(final String taskSid, final String reservationSid) {_90 Reservation reservation = Reservation.updater(workspaceSid, taskSid, reservationSid)_90 .setReservationStatus(Reservation.Status.ACCEPTED).update();_90_90 return "{\"worker_name\":\"" + reservation.getWorkerName() + "\"}";_90 }_90_90 public String handleIncomingCall() {_90 VoiceResponse twiml =_90 new VoiceResponse.Builder()_90 .gather(new Gather.Builder()_90 .say(new Say.Builder("Para Español oprime el uno.").language(Say.Language.ES)_90 .build())_90 .say(new Say.Builder("For English, please hold or press two.")_90 .language(Say.Language.EN).build())_90 .numDigits(1).timeout(5).build())_90 .build();_90_90 try {_90 return twiml.toXml();_90 } catch (TwiMLException e) {_90 return "Error creating TwiML: " + e.getMessage();_90 }_90 }_90}
You can use the Buy Numbers section of the Twilio Voice and Messaging web portal to purchase a new phone number, or use an existing Twilio phone number. Open the phone number details page and point the Voice Request URL at your new endpoint:
Using any phone, call the Twilio number. You will be prompted to press one for Spanish or two for English. However, when you press a digit, you'll hear an error message. That's because our <Gather>
verb is pointing to another endpoint, enqueue_call
, which we haven't implemented yet. In the next step we'll add the required endpoint and use it to create a new Task based on the language selected by the caller.