Create Tasks from Phone Calls using TwiML: Receive an Incoming Call
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:
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;9import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;10import com.twilio.twiml.Gather;11import com.twilio.twiml.Say;12import com.twilio.twiml.TwiMLException;13import com.twilio.twiml.VoiceResponse;1415public class TwilioTaskRouterServlet extends HttpServlet {1617private String accountSid;18private String apiKey;19private String apiSecret;20private String workspaceSid;21private String workflowSid;2223@Override24public void init() {25accountSid = this.getServletConfig().getInitParameter("AccountSid");26apiKey = this.getServletConfig().getInitParameter("ApiKey");27apiSecret = this.getServletConfig().getInitParameter("ApiSecret");28workspaceSid = this.getServletConfig().getInitParameter("WorkspaceSid");29workflowSid = this.getServletConfig().getInitParameter("WorkflowSid");3031Twilio.init(apiKey, apiSecret, accountSid);32}3334// service() responds to both GET and POST requests.35// You can also use doGet() or doPost()36@Override37public void service(final HttpServletRequest request, final HttpServletResponse response)38throws IOException {39if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {40return;41}4243if (request.getPathInfo().equals("/assignment_callback")) {44response.setContentType("application/json");45response.getWriter().print("{\"instruction\":\"accept\"}");46} else if (request.getPathInfo().equals("/create_task")) {47response.setContentType("application/json");48response.getWriter().print(createTask());49} else if (request.getPathInfo().equals("/accept_reservation")) {50response.setContentType("application/json");51final String taskSid = request.getParameter("TaskSid");52final String reservationSid = request.getParameter("ReservationSid");53response.getWriter().print(acceptReservation(taskSid, reservationSid));54} else if (request.getPathInfo().equals("/incoming_call")) {55response.setContentType("application/xml");56response.getWriter().print(handleIncomingCall());57}58}5960public String createTask() {61String attributes = "{\"selected_language\":\"es\"}";6263Task task = Task.creator(workspaceSid, attributes, workflowSid).create();6465return "{\"task_sid\":\"" + task.getSid() + "\"}";66}6768public String acceptReservation(final String taskSid, final String reservationSid) {69Reservation reservation = Reservation.updater(workspaceSid, taskSid, reservationSid)70.setReservationStatus(Reservation.Status.ACCEPTED).update();7172return "{\"worker_name\":\"" + reservation.getWorkerName() + "\"}";73}7475public String handleIncomingCall() {76VoiceResponse twiml =77new VoiceResponse.Builder()78.gather(new Gather.Builder()79.say(new Say.Builder("Para Español oprime el uno.").language(Say.Language.ES)80.build())81.say(new Say.Builder("For English, please hold or press two.")82.language(Say.Language.EN).build())83.numDigits(1).timeout(5).build())84.build();8586try {87return twiml.toXml();88} catch (TwiMLException e) {89return "Error creating TwiML: " + e.getMessage();90}91}92}
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.