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.


Program.cs

programcs page anchor
1
using System;
2
using System.Net;
3
using SimpleWebServer;
4
using Twilio;
5
using Twilio.Rest.Taskrouter.V1.Workspace;
6
using Twilio.Rest.Taskrouter.V1.Workspace.Task;
7
using Twilio.TwiML;
8
9
namespace taskroutercsharp
10
{
11
class MainClass
12
{
13
// Find your Account Sid and Auth Token at twilio.com/user/account
14
const string AccountSid = "{{ account_sid }}";
15
const string AuthToken = "{{ auth_token }}";
16
const string WorkspaceSid = "{{ workspace_sid }}";
17
const string WorkflowSid = "{{ workflow_sid }}";
18
19
public static void Main(string[] args)
20
{
21
// Initialize the Twilio client
22
TwilioClient.Init(AccountSid, AuthToken);
23
24
WebServer ws = new WebServer(SendResponse, "http://localhost:8080/");
25
ws.Run();
26
Console.WriteLine("A simple webserver. Press a key to quit.");
27
Console.ReadKey();
28
ws.Stop();
29
}
30
31
public static HttpListenerResponse SendResponse(HttpListenerContext ctx)
32
{
33
HttpListenerRequest request = ctx.Request;
34
HttpListenerResponse response = ctx.Response;
35
36
String endpoint = request.RawUrl;
37
38
if (endpoint.EndsWith("assignment_callback"))
39
{
40
response.StatusCode = (int)HttpStatusCode.OK;
41
response.ContentType = "application/json";
42
response.StatusDescription = "{\"instruction\":\"accept\"}";
43
return response;
44
}
45
else if (endpoint.EndsWith("create_task"))
46
{
47
response.StatusCode = (int)HttpStatusCode.OK;
48
response.ContentType = "application/json";
49
TaskResource task = TaskResource.Create(
50
WorkspaceSid,
51
attributes: "{\"selected_language\":\"es\"}",
52
workflowSid: WorkflowSid);
53
54
response.StatusDescription = task.Attributes;
55
return response;
56
}
57
else if (endpoint.EndsWith("accept_reservation"))
58
{
59
response.StatusCode = (int)HttpStatusCode.OK;
60
response.ContentType = "application/json";
61
var taskSid = request.QueryString["TaskSid"];
62
var reservationSid = request.QueryString["ReservationSid"];
63
ReservationResource reservation = ReservationResource.Update(
64
WorkspaceSid,
65
taskSid,
66
reservationSid,
67
ReservationResource.StatusEnum.Accepted);
68
69
response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";
70
return response;
71
}
72
else if (endpoint.EndsWith("incoming_call"))
73
{
74
response.StatusCode = (int)HttpStatusCode.OK;
75
response.ContentType = "application/xml";
76
var twiml = new VoiceResponse();
77
twiml.Gather(new Gather(numDigits: 1, action: "enqueue_call")
78
.Say("Para Espanol oprima el uno.", language: "es")
79
.Say("For English, please hold or press two.", language: "en"));
80
81
response.StatusDescription = twiml.ToString();
82
return response;
83
}
84
else if (endpoint.Contains("enqueue_call"))
85
{
86
response.StatusCode = (int)HttpStatusCode.OK;
87
response.ContentType = "application/xml";
88
89
int digitPressed = 0;
90
var language = "";
91
var digitsQuery = request.QueryString["Digits"];
92
if (digitsQuery != null)
93
{
94
try
95
{
96
digitPressed = Int32.Parse(request.QueryString["Digits"]);
97
}
98
catch (FormatException e)
99
{
100
Console.WriteLine(e.Message);
101
}
102
}
103
104
if (digitPressed == 1)
105
{
106
language = "es";
107
}
108
else
109
{
110
language = "en";
111
}
112
113
var twiml = new VoiceResponse();
114
twiml.Enqueue(
115
"{\"selected_language\":" + language + "\"}",
116
workflowSid: WorkflowSid);
117
response.StatusDescription = twiml.ToString();
118
return response;
119
}
120
response.StatusCode = (int)HttpStatusCode.OK;
121
return response;
122
}
123
}
124
}

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.