Skip to contentSkip to navigationSkip to topbar
Rate this page:
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

_124
using System;
_124
using System.Net;
_124
using SimpleWebServer;
_124
using Twilio;
_124
using Twilio.Rest.Taskrouter.V1.Workspace;
_124
using Twilio.Rest.Taskrouter.V1.Workspace.Task;
_124
using Twilio.TwiML;
_124
_124
namespace taskroutercsharp
_124
{
_124
class MainClass
_124
{
_124
// Find your Account Sid and Auth Token at twilio.com/user/account
_124
const string AccountSid = "{{ account_sid }}";
_124
const string AuthToken = "{{ auth_token }}";
_124
const string WorkspaceSid = "{{ workspace_sid }}";
_124
const string WorkflowSid = "{{ workflow_sid }}";
_124
_124
public static void Main(string[] args)
_124
{
_124
// Initialize the Twilio client
_124
TwilioClient.Init(AccountSid, AuthToken);
_124
_124
WebServer ws = new WebServer(SendResponse, "http://localhost:8080/");
_124
ws.Run();
_124
Console.WriteLine("A simple webserver. Press a key to quit.");
_124
Console.ReadKey();
_124
ws.Stop();
_124
}
_124
_124
public static HttpListenerResponse SendResponse(HttpListenerContext ctx)
_124
{
_124
HttpListenerRequest request = ctx.Request;
_124
HttpListenerResponse response = ctx.Response;
_124
_124
String endpoint = request.RawUrl;
_124
_124
if (endpoint.EndsWith("assignment_callback"))
_124
{
_124
response.StatusCode = (int)HttpStatusCode.OK;
_124
response.ContentType = "application/json";
_124
response.StatusDescription = "{\"instruction\":\"accept\"}";
_124
return response;
_124
}
_124
else if (endpoint.EndsWith("create_task"))
_124
{
_124
response.StatusCode = (int)HttpStatusCode.OK;
_124
response.ContentType = "application/json";
_124
TaskResource task = TaskResource.Create(
_124
WorkspaceSid,
_124
attributes: "{\"selected_language\":\"es\"}",
_124
workflowSid: WorkflowSid);
_124
_124
response.StatusDescription = task.Attributes;
_124
return response;
_124
}
_124
else if (endpoint.EndsWith("accept_reservation"))
_124
{
_124
response.StatusCode = (int)HttpStatusCode.OK;
_124
response.ContentType = "application/json";
_124
var taskSid = request.QueryString["TaskSid"];
_124
var reservationSid = request.QueryString["ReservationSid"];
_124
ReservationResource reservation = ReservationResource.Update(
_124
WorkspaceSid,
_124
taskSid,
_124
reservationSid,
_124
ReservationResource.StatusEnum.Accepted);
_124
_124
response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";
_124
return response;
_124
}
_124
else if (endpoint.EndsWith("incoming_call"))
_124
{
_124
response.StatusCode = (int)HttpStatusCode.OK;
_124
response.ContentType = "application/xml";
_124
var twiml = new VoiceResponse();
_124
twiml.Gather(new Gather(numDigits: 1, action: "enqueue_call")
_124
.Say("Para Espanol oprima el uno.", language: "es")
_124
.Say("For English, please hold or press two.", language: "en"));
_124
_124
response.StatusDescription = twiml.ToString();
_124
return response;
_124
}
_124
else if (endpoint.Contains("enqueue_call"))
_124
{
_124
response.StatusCode = (int)HttpStatusCode.OK;
_124
response.ContentType = "application/xml";
_124
_124
int digitPressed = 0;
_124
var language = "";
_124
var digitsQuery = request.QueryString["Digits"];
_124
if (digitsQuery != null)
_124
{
_124
try
_124
{
_124
digitPressed = Int32.Parse(request.QueryString["Digits"]);
_124
}
_124
catch (FormatException e)
_124
{
_124
Console.WriteLine(e.Message);
_124
}
_124
}
_124
_124
if (digitPressed == 1)
_124
{
_124
language = "es";
_124
}
_124
else
_124
{
_124
language = "en";
_124
}
_124
_124
var twiml = new VoiceResponse();
_124
twiml.Enqueue(
_124
"{\"selected_language\":" + language + "\"}",
_124
workflowSid: WorkflowSid);
_124
response.StatusDescription = twiml.ToString();
_124
return response;
_124
}
_124
response.StatusCode = (int)HttpStatusCode.OK;
_124
return response;
_124
}
_124
}
_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

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Enqueue workflowSid="WW0123401234...">
_10
<Task>{"selected_language": "es"}</Task>
_10
</Enqueue>
_10
</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 »


Rate this page: