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.
_124using System;_124using System.Net;_124using SimpleWebServer;_124using Twilio;_124using Twilio.Rest.Taskrouter.V1.Workspace;_124using Twilio.Rest.Taskrouter.V1.Workspace.Task;_124using Twilio.TwiML;_124_124namespace 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:
_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.