Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

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

receive-an-incoming-call page anchor

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

twiliotaskrouterservletjava page anchor

_90
import java.io.IOException;
_90
_90
import javax.servlet.http.HttpServlet;
_90
import javax.servlet.http.HttpServletRequest;
_90
import javax.servlet.http.HttpServletResponse;
_90
_90
import com.twilio.Twilio;
_90
import com.twilio.rest.taskrouter.v1.workspace.Task;
_90
import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;
_90
import com.twilio.twiml.Gather;
_90
import com.twilio.twiml.Say;
_90
import com.twilio.twiml.TwiMLException;
_90
import com.twilio.twiml.VoiceResponse;
_90
_90
public 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(link takes you to an external page) 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:

Phone Number Setup Details.

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 »


Rate this page: