We've seen how to create Tasks using the TaskRouter REST API and how to accept a Task Reservation using both the REST API and Assignment Callback instructions. TaskRouter also introduces new TwiML instructions that you can use to create a Task from a Twilio phone call.
To receive an incoming phone call, we first need a Twilio phone number. In this example we'll use a US toll-free number, but you can use a Voice capable number from any country.
Before purchasing or setting up the phone number, we need to add on to our Program.cs
to handle incoming calls:
_88using System;_88using System.Net;_88using SimpleWebServer;_88using Twilio;_88using Twilio.Rest.Taskrouter.V1.Workspace;_88using Twilio.Rest.Taskrouter.V1.Workspace.Task;_88using Twilio.TwiML;_88_88namespace taskroutercsharp_88{_88 class MainClass_88 {_88 // Find your Account Sid and Auth Token at twilio.com/user/account_88 const string AccountSid = "{{ account_sid }}";_88 const string AuthToken = "{{ auth_token }}";_88 const string WorkspaceSid = "{{ workspace_sid }}";_88 const string WorkflowSid = "{{ workflow_sid }}";_88_88 public static void Main(string[] args)_88 {_88 // Initialize the Twilio client_88 TwilioClient.Init(AccountSid, AuthToken);_88_88 WebServer ws = new WebServer(SendResponse, "http://localhost:8080/");_88 ws.Run();_88 Console.WriteLine("A simple webserver. Press a key to quit.");_88 Console.ReadKey();_88 ws.Stop();_88 }_88_88 public static HttpListenerResponse SendResponse(HttpListenerContext ctx)_88 {_88 HttpListenerRequest request = ctx.Request;_88 HttpListenerResponse response = ctx.Response;_88_88 String endpoint = request.RawUrl;_88_88 if (endpoint.EndsWith("assignment_callback"))_88 {_88 response.StatusCode = (int)HttpStatusCode.OK;_88 response.ContentType = "application/json";_88 response.StatusDescription = "{\"instruction\":\"accept\"}";_88 return response;_88 }_88 else if (endpoint.EndsWith("create_task"))_88 {_88 response.StatusCode = (int)HttpStatusCode.OK;_88 response.ContentType = "application/json";_88 TaskResource task = TaskResource.Create(_88 WorkspaceSid,_88 attributes: "{\"selected_language\":\"es\"}",_88 workflowSid: WorkflowSid);_88_88 response.StatusDescription = task.Attributes;_88 return response;_88 }_88 else if (endpoint.EndsWith("accept_reservation"))_88 {_88 response.StatusCode = (int)HttpStatusCode.OK;_88 response.ContentType = "application/json";_88 var taskSid = request.QueryString["TaskSid"];_88 var reservationSid = request.QueryString["ReservationSid"];_88 ReservationResource reservation = ReservationResource.Update(_88 WorkspaceSid, _88 taskSid, _88 reservationSid,_88 ReservationResource.StatusEnum.Accepted);_88_88 response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";_88 return response;_88 }_88 else if (endpoint.EndsWith("incoming_call"))_88 {_88 response.StatusCode = (int)HttpStatusCode.OK;_88 response.ContentType = "application/xml";_88 var twiml = new VoiceResponse();_88 twiml.Gather(new Gather(numDigits: 1, action: "enqueue_call")_88 .Say("Para Espanol oprima el uno.", language: "es" )_88 .Say("For English, please hold or press two.", language: "en"));_88_88 response.StatusDescription = twiml.ToString();_88 return response;_88 }_88 response.StatusCode = (int)HttpStatusCode.OK;_88 return response;_88 }_88 }_88}
You can use the Buy Numbers section of the Twilio Voice and Messaging web portal to purchase a new phone number, or use an existing Twilio phone number. Open the phone number details page and point the Voice Request URL at your new endpoint:
Using any phone, call the Twilio number. You will be prompted to press one for Spanish or two for English. However, when you press a digit, you'll hear an error message. That's because our <Gather>
verb is pointing to another endpoint, enqueue_call
, which we haven't implemented yet. In the next step we'll add the required endpoint and use it to create a new Task based on the language selected by the caller.