In the previous step we created a Task from an incoming phone call using <Enqueue workflowSid="WW0123401234..">
. In this step we will create another call and dequeue it to an eligible Worker when one becomes available.
Back in Part 1 of the Quickstart, we created a Worker named Alice that is capable of handling both English and Spanish inquiries. With your Workspace open in the TaskRouter web portal, click 'Workers' and click to edit the details of our Worker Alice. Ensure that Alice is set to a non-available Activity state such as 'Offline'. Next, edit Alice's JSON attributes and add a contact_uri
field. Replace the dummy 555 number below with your own phone number.
Alice's modified JSON attributes:
{"languages": ["en", "es"], "contact_uri": "+15555555555"}
Or, as displayed in the web portal:
In this step, we again use <Enqueue>
to create a Task from an incoming phone call. When an eligible Worker (in this case Alice) becomes available, TaskRouter will make a request to our Assignment Callback URL. This time, we will respond with a special 'dequeue' instruction; this tells Twilio to call Alice at her 'contact_uri' and bridge to the caller.
For this part of the Quickstart, although not totally necessary it will be useful to have two phones available - one to call your Twilio number, and one to receive a call as Alice. Experienced Twilio users might consider using the Twilio Dev Phone as one of the endpoints.
Before we add the 'dequeue' assignment instruction we need to create a new Activity in our TaskRouter Workspace. One of the nice things about integrating TaskRouter with TwiML is that our Worker will automatically transition through various Activities as the call is assigned, answered and even hung up. We need an Activity for our Worker to transition to when the call ends.
With your Workspace open in the TaskRouter web portal, click 'Activities' and then 'Create Activity'. Give the new Activity a name of 'WrapUp' and a value of 'unavailable'. Once you've saved it, make a note of the Activity Sid:
To return the 'dequeue' assignment instruction, modify Program.cs
assignment_callback endpoint to now issue a dequeue instruction, substituting your new WrapUp ActivitySid between the curly braces:
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")) {39response.StatusCode = (int) HttpStatusCode.OK;40response.ContentType = "application/json";41response.StatusDescription = "{\"instruction\":\"dequeue\", \"from\":\"+15556667777\", \"post_work_activity_sid\":\"WA0123401234...\"}";42return response;43} else if (endpoint.EndsWith ("create_task")) {44response.StatusCode = (int)HttpStatusCode.OK;45response.ContentType = "application/json";46TaskResource task = TaskResource.Create(47WorkspaceSid,48attributes: "{\"selected_language\":\"es\"}",49workflowSid: WorkflowSid);5051response.StatusDescription = task.Attributes;52return response;53} else if (endpoint.EndsWith ("accept_reservation")) {54response.StatusCode = (int)HttpStatusCode.OK;55response.ContentType = "application/json";56var taskSid = request.QueryString ["TaskSid"];57var reservationSid = request.QueryString ["ReservationSid"];58ReservationResource reservation = ReservationResource.Update(59WorkspaceSid,60taskSid,61reservationSid,62ReservationResource.StatusEnum.Accepted);6364response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";65return response;66} else if (endpoint.EndsWith ("incoming_call")) {67response.StatusCode = (int)HttpStatusCode.OK;68response.ContentType = "application/xml";69var twiml = new VoiceResponse();70twiml.Gather(new Gather(numDigits: 1, action: "enqueue_call")71.Say("Para Espanol oprima el uno.", language: "es")72.Say("For English, please hold or press two.", language: "en"));7374response.StatusDescription = twiml.ToString();75return response;76} else if (endpoint.Contains("enqueue_call")) {77response.StatusCode = (int)HttpStatusCode.OK;78response.ContentType = "application/xml";7980int digitPressed = 0;81var language = "";82var digitsQuery = request.QueryString["Digits"];83if(digitsQuery != null) {84try85{86digitPressed = Int32.Parse(request.QueryString ["Digits"]);87} catch (FormatException e)88{89Console.WriteLine(e.Message);90}91}9293if (digitPressed == 1) {94language = "es";95} else {96language = "en";97}9899var twiml = new VoiceResponse();100twiml.Enqueue(101"{\"selected_language\":" + language + "\"}",102workflowSid: WorkflowSid);103104response.StatusDescription = twiml.ToString();105return response;106}107response.StatusCode = (int) HttpStatusCode.OK;108return response;109}110}111}
This returns a very simple JSON object from the Assignment Callback URL:
{"instruction":"dequeue", "from": "+15556667777", "post_work_activity_sid": "WA01234012340123401234"}
The JSON instructs Twilio to dequeue the waiting call and, because we don't include an explicit "to" field in our JSON, connect it to our Worker at their contact_uri
. This is convenient default behavior provided by TaskRouter.
In the next step, we test our incoming call flow from end-to-end.