To indicate that a Worker has accepted or rejected a Task, you make an HTTP
POST
request to a Reservation instance resource. To do that, we need the
TaskSid, which is available via the web portal, and the ReservationSid. The
ReservationSid was passed to our Assignment Callback URL when a Worker was
reserved for our Task. Using the ngrok inspector page at
http://localhost:4040
, we can easily find the request parameters sent from
TaskRouter and copy the ReservationSid to our clipboard. **
The Reservation API resource is ephemeral and exists only within the context of a Task. As such, it doesn't have its own primary API resource and you'll find it documented in the Tasks resource section of the reference documentation.
With our trusty TaskSid and ReservationSid in hand, let's make another REST API request to accept our Task Reservation. We'll add on to our Program.cs
to accept a reservation with our web server. Remember to substitute your own account details in place of the curly braces.
_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 = "{}";_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}
If you'd like to use curl instead, put the following into your terminal:
_10curl https://taskrouter.twilio.com/v1/Workspaces/{WorkspaceSid}/Tasks/{TaskSid}/Reservations/{ReservationSid} \_10-d ReservationStatus=accepted \_10-u {AccountSid}:{AuthToken}
Examining the response from TaskRouter, we see that the Task Reservation has been accepted, and the Task has been assigned to the Worker Alice:
_10{... "worker_name": "Alice", "reservation_status": "accepted", ...}
If you don't see this, it's possible that your Reservation has timed out. If this is the case, set your Worker back to an available Activity state and create another Task. To prevent this occurring, you can increase the 'Task Reservation Timeout' value in your Workflow configuration.
With your Workspace open in the TaskRouter web portal, click 'Workers' and you'll see that Alice has been transitioned to the 'Assignment Activity' of the TaskQueue that assigned the Task. In this case, "Busy":
Hurrah! We've made it to the end of the Task lifecycle:
Task Created → eligible Worker becomes available → Worker reserved → Reservation accepted → Task assigned to Worker.
In the next steps, we'll examine more ways to perform common Task acceptance and rejection workflows.
Next: Accept a Reservation using Assignment Instructions »
** If you're not using ngrok or a similar tool, you can modify assignment.php to print the value of ReservationSid to your web server log. Or, you can use the Tasks REST API instance resource to look up the ReservationSid based on the TaskSid.