# 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.

## Receive an Incoming 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:

### TwilioTaskRouterServlet.java

```java
import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.Twilio;
import com.twilio.rest.taskrouter.v1.workspace.Task;
import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;
import com.twilio.twiml.Gather;
import com.twilio.twiml.Say;
import com.twilio.twiml.TwiMLException;
import com.twilio.twiml.VoiceResponse;

public class TwilioTaskRouterServlet extends HttpServlet {

  private String accountSid;
  private String authToken;
  private String workspaceSid;
  private String workflowSid;

  @Override
  public void init() {
    accountSid = this.getServletConfig().getInitParameter("AccountSid");
    authToken = this.getServletConfig().getInitParameter("AuthToken");
    workspaceSid = this.getServletConfig().getInitParameter("WorkspaceSid");
    workflowSid = this.getServletConfig().getInitParameter("WorkflowSid");

    Twilio.init(accountSid, authToken);
  }

  // service() responds to both GET and POST requests.
  // You can also use doGet() or doPost()
  @Override
  public void service(final HttpServletRequest request, final HttpServletResponse response)
      throws IOException {
    if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {
      return;
    }

    if (request.getPathInfo().equals("/assignment_callback")) {
      response.setContentType("application/json");
      response.getWriter().print("{\"instruction\":\"accept\"}");
    } else if (request.getPathInfo().equals("/create_task")) {
      response.setContentType("application/json");
      response.getWriter().print(createTask());
    } else if (request.getPathInfo().equals("/accept_reservation")) {
      response.setContentType("application/json");
      final String taskSid = request.getParameter("TaskSid");
      final String reservationSid = request.getParameter("ReservationSid");
      response.getWriter().print(acceptReservation(taskSid, reservationSid));
    } else if (request.getPathInfo().equals("/incoming_call")) {
      response.setContentType("application/xml");
      response.getWriter().print(handleIncomingCall());
    }
  }

  public String createTask() {
    String attributes = "{\"selected_language\":\"es\"}";

    Task task = Task.creator(workspaceSid, attributes, workflowSid).create();

    return "{\"task_sid\":\"" + task.getSid() + "\"}";
  }

  public String acceptReservation(final String taskSid, final String reservationSid) {
    Reservation reservation = Reservation.updater(workspaceSid, taskSid, reservationSid)
        .setReservationStatus(Reservation.Status.ACCEPTED).update();

    return "{\"worker_name\":\"" + reservation.getWorkerName() + "\"}";
  }

  public String handleIncomingCall() {
    VoiceResponse twiml =
        new VoiceResponse.Builder()
            .gather(new Gather.Builder()
                .say(new Say.Builder("Para Español oprime el uno.").language(Say.Language.ES)
                    .build())
                .say(new Say.Builder("For English, please hold or press two.")
                    .language(Say.Language.EN).build())
                .numDigits(1).timeout(5).build())
            .build();

    try {
      return twiml.toXml();
    } catch (TwiMLException e) {
      return "Error creating TwiML: " + e.getMessage();
    }
  }
}
```

You can use the [Buy Numbers](https://console.twilio.com/?frameUrl=%2Fconsole%2Fphone-numbers%2Fsearch) 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:

![TaskRouter Quickstart showing phone number properties and request URLs for voice and messaging.](https://docs-resources.prod.twilio.com/6a0de916bec885f256d33e8883f001eab45b017ff0e2bbe6615c10e0a0e53a13.png)

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.

[Next: Create a Task using Enqueue »](/docs/taskrouter/quickstart/java/twiml-create-task)
