Skip to contentSkip to navigationSkip to topbar
On this page

Create Tasks from Phone Calls using TwiML: Create a TaskRouter Task using <Enqueue>


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.


TwilioTaskRouterServlet.java

twiliotaskrouterservletjava page anchor
1
import java.io.IOException;
2
3
import javax.servlet.ServletException;
4
import javax.servlet.http.HttpServlet;
5
import javax.servlet.http.HttpServletRequest;
6
import javax.servlet.http.HttpServletResponse;
7
8
import com.twilio.Twilio;
9
import com.twilio.rest.taskrouter.v1.workspace.Task;
10
import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;
11
import com.twilio.twiml.*;
12
import com.twilio.twiml.voice.*;
13
14
public class TwilioTaskRouterServlet extends HttpServlet {
15
16
private String accountSid;
17
private String authToken;
18
private String workspaceSid;
19
private String workflowSid;
20
21
@Override
22
public void init() {
23
accountSid = this.getServletConfig().getInitParameter("AccountSid");
24
authToken = this.getServletConfig().getInitParameter("AuthToken");
25
workspaceSid = this.getServletConfig().getInitParameter("WorkspaceSid");
26
workflowSid = this.getServletConfig().getInitParameter("WorkflowSid");
27
28
Twilio.init(accountSid, authToken);
29
}
30
31
// service() responds to both GET and POST requests.
32
// You can also use doGet() or doPost()
33
@Override
34
public void service(final HttpServletRequest request, final HttpServletResponse response)
35
throws IOException, ServletException {
36
if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {
37
return;
38
}
39
40
if (request.getPathInfo().equals("/assignment_callback")) {
41
response.setContentType("application/json");
42
response.getWriter().print("{\"instruction\":\"accept\"}");
43
} else if (request.getPathInfo().equals("/create_task")) {
44
response.setContentType("application/json");
45
final String taskAttributes = createTask();
46
response.getWriter().print(createTask());
47
} else if (request.getPathInfo().equals("/accept_reservation")) {
48
response.setContentType("application/json");
49
final String taskSid = request.getParameter("TaskSid");
50
final String reservationSid = request.getParameter("ReservationSid");
51
response.getWriter().print(acceptReservation(taskSid, reservationSid));
52
} else if (request.getPathInfo().equals("/incoming_call")) {
53
response.setContentType("application/xml");
54
response.getWriter().print(handleIncomingCall());
55
} else if (request.getPathInfo().equals("/enqueue_call")) {
56
response.setContentType("application/xml");
57
response.getWriter().print(enqueueTask());
58
}
59
60
}
61
62
public String createTask() {
63
String attributes = "{\"selected_language\":\"es\"}";
64
65
Task task = Task.creator(workspaceSid).setAttributes(attributes).setWorkflowSid(workflowSid).create();
66
67
return "{\"task_sid\":\"" + task.getSid() + "\"}";
68
}
69
70
public String acceptReservation(final String taskSid, final String reservationSid) {
71
Reservation reservation = Reservation.updater(workspaceSid, taskSid, reservationSid)
72
.setReservationStatus(Reservation.Status.ACCEPTED).update();
73
74
return "{\"worker_name\":\"" + reservation.getWorkerName() + "\"}";
75
}
76
77
public String handleIncomingCall() {
78
VoiceResponse twiml =
79
new 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();
87
88
try {
89
return twiml.toXml();
90
} catch (TwiMLException e) {
91
return "Error creating TwiML: " + e.getMessage();
92
}
93
}
94
95
public String enqueueTask() {
96
com.twilio.twiml.voice.Task task = new com.twilio.twiml.voice.Task.Builder("{\"selected_language\":\"es\"}").build();
97
98
Enqueue enqueue = new Enqueue.Builder().task(task).workflowSid(workflowSid).build();
99
100
VoiceResponse twiml = new VoiceResponse.Builder().enqueue(enqueue).build();
101
102
try {
103
return twiml.toXml();
104
} catch (TwiMLException e) {
105
return "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:


enqueue_call - TwiML Output

enqueue_call---twiml-output page anchor
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.

Next: Dequeue a Call to a Worker »

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.