Remember when we created a Task and accepted it using the Reservations subresource of the REST API? I do. And it was grand.
This time, we'll create another Task, again using the REST API, but we will have our server accept the Reservation as soon as it is notified, via a synchronous HTTP response.
Before we create the next Task, once again make sure that our Worker Alice is in a non-available Activity state.
Call the Create Task endpoint exposed with Program.cs
again, or execute the following curl command:
_10curl https://taskrouter.twilio.com/v1/Workspaces/{WorkspaceSid}/Tasks \_10--data-urlencode Attributes='{"selected_language": "es"}' \_10-d WorkflowSid={WorkflowSid} \_10-u {AccountSid}:{AuthToken}
This time, before bringing Alice online, we need to make changes to our assignment_callback method in our Program.cs
. Open it and modify the existing code to reflect the following:
_61using System;_61using System.Net;_61using SimpleWebServer;_61using Twilio;_61using Twilio.Rest.Taskrouter.V1.Workspace.Task;_61_61namespace taskroutercsharp_61{_61 class MainClass_61 {_61 // Find your Account Sid and Auth Token at twilio.com/user/account_61 const string AccountSid = "{{ account_sid }}";_61 const string AuthToken = "{{ auth_token }}";_61 const string WorkspaceSid = "{{ workspace_sid }}";_61 const string WorkflowSid = "{{ workflow_sid }}";_61_61 public static void Main (string[] args)_61 {_61 TwilioClient.Init(accountSid, authToken);_61 WebServer ws = new WebServer (SendResponse, "http://localhost:8080/");_61 ws.Run ();_61 Console.WriteLine ("A simple webserver. Press a key to quit.");_61 Console.ReadKey ();_61 ws.Stop ();_61 }_61_61 public static HttpListenerResponse SendResponse(HttpListenerContext ctx)_61 {_61 HttpListenerRequest request = ctx.Request;_61 HttpListenerResponse response = ctx.Response;_61_61 String endpoint = request.RawUrl;_61_61 if (endpoint.EndsWith("assignment_callback")) {_61 response.StatusCode = (int) HttpStatusCode.OK;_61 response.ContentType = "application/json";_61 response.StatusDescription = "{\"instruction\":\"accept\"}";_61 return response;_61 } else if (endpoint.EndsWith ("create_task")) {_61 response.StatusCode = (int)HttpStatusCode.OK;_61 response.ContentType = "application/json";_61 var task = TaskResource.Create(_61 WorkspaceSid, attributes: "{\"selected_language\":\"es\"}", workflowSid: WorkflowSid);_61 response.StatusDescription = task.Attributes;_61 return response;_61 } else if (endpoint.EndsWith ("accept_reservation")) {_61 response.StatusCode = (int)HttpStatusCode.OK;_61 response.ContentType = "application/json";_61 var taskSid = request.QueryString ["TaskSid"];_61 var reservationSid = request.QueryString ["ReservationSid"];_61 var reservation = ReservationResource.Update(_61 WorkspaceSid, taskSid, reservationSid,_61 ReservationResource.StatusEnum.Accepted);_61 response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";_61 return response;_61 }_61 response.StatusCode = (int) HttpStatusCode.OK;_61 return response;_61 }_61 }_61}
Instead of returning an empty JSON document as before, we've included an 'assignment instruction' in our response. The 'accept' assignment instruction tells TaskRouter to automatically accept the Reservation and assign the Task to the Worker it has been reserved for.
To kick this process off, we need to transition Alice to an available Activity. With your Workspace open in the TaskRouter web portal, click 'Workers' then click to edit Alice and set her Activity to 'Idle'.
Now, click 'Tasks' in the main navigation and you should see that the Task has an Assignment Status of 'assigned':
What actually happened is that Alice was reserved for a very short period of time. TaskRouter made a request to your web server at the Assignment Callback URL, and your server told TaskRouter to accept the Reservation. At that point, Alice's Activity transitioned to the 'Assignment Activity' of the TaskQueue that assigned the Task, as it did in the previous step.
And that's that. We created another Task using the REST API, accepted it via an assignment instruction at our Workflow's Assignment Callback URL and saw that this immediately accepted the Reservation for our Worker.
Onward! Next we learn about shortcuts to create Tasks originating from Twilio phone calls.