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.
1using System;2using System.Net;3using SimpleWebServer;4using Twilio;5using Twilio.Rest.Taskrouter.V1.Workspace;6using Twilio.Rest.Taskrouter.V1.Workspace.Task;7using Twilio.TwiML;89namespace taskroutercsharp10{11class MainClass12{13// Find your Account Sid and Auth Token at twilio.com/user/account14const string AccountSid = "{{ account_sid }}";15const string AuthToken = "{{ auth_token }}";16const string WorkspaceSid = "{{ workspace_sid }}";17const string WorkflowSid = "{{ workflow_sid }}";1819public static void Main(string[] args)20{21// Initialize the Twilio client22TwilioClient.Init(AccountSid, AuthToken);2324WebServer ws = new WebServer(SendResponse, "http://localhost:8080/");25ws.Run();26Console.WriteLine("A simple webserver. Press a key to quit.");27Console.ReadKey();28ws.Stop();29}3031public static HttpListenerResponse SendResponse(HttpListenerContext ctx)32{33HttpListenerRequest request = ctx.Request;34HttpListenerResponse response = ctx.Response;3536String endpoint = request.RawUrl;3738if (endpoint.EndsWith("assignment_callback"))39{40response.StatusCode = (int)HttpStatusCode.OK;41response.ContentType = "application/json";42response.StatusDescription = "{\"instruction\":\"accept\"}";43return response;44}45else if (endpoint.EndsWith("create_task"))46{47response.StatusCode = (int)HttpStatusCode.OK;48response.ContentType = "application/json";49TaskResource task = TaskResource.Create(50WorkspaceSid,51attributes: "{\"selected_language\":\"es\"}",52workflowSid: WorkflowSid);5354response.StatusDescription = task.Attributes;55return response;56}57else if (endpoint.EndsWith("accept_reservation"))58{59response.StatusCode = (int)HttpStatusCode.OK;60response.ContentType = "application/json";61var taskSid = request.QueryString["TaskSid"];62var reservationSid = request.QueryString["ReservationSid"];63ReservationResource reservation = ReservationResource.Update(64WorkspaceSid,65taskSid,66reservationSid,67ReservationResource.StatusEnum.Accepted);6869response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";70return response;71}72else if (endpoint.EndsWith("incoming_call"))73{74response.StatusCode = (int)HttpStatusCode.OK;75response.ContentType = "application/xml";76var twiml = new VoiceResponse();77twiml.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"));8081response.StatusDescription = twiml.ToString();82return response;83}84else if (endpoint.Contains("enqueue_call"))85{86response.StatusCode = (int)HttpStatusCode.OK;87response.ContentType = "application/xml";8889int digitPressed = 0;90var language = "";91var digitsQuery = request.QueryString["Digits"];92if (digitsQuery != null)93{94try95{96digitPressed = Int32.Parse(request.QueryString["Digits"]);97}98catch (FormatException e)99{100Console.WriteLine(e.Message);101}102}103104if (digitPressed == 1)105{106language = "es";107}108else109{110language = "en";111}112113var twiml = new VoiceResponse();114twiml.Enqueue(115"{\"selected_language\":" + language + "\"}",116workflowSid: WorkflowSid);117response.StatusDescription = twiml.ToString();118return response;119}120response.StatusCode = (int)HttpStatusCode.OK;121return 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:
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.