Remember when we created a Task and accepted it using the Reservations subresource of the REST API?
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 server.rb again, or execute the following curl command:
1curl https://taskrouter.twilio.com/v1/Workspaces/{WorkspaceSid}/Tasks \2--data-urlencode Attributes='{"selected_language": "es"}' \3-d WorkflowSid={WorkflowSid} \4-u {AccountSid}:{AuthToken}
This time, before bringing Alice online, we need to make changes to our assignment_callback
method in our server.rb. Open it and modify the existing code to reflect the following:
server.rb
1require 'rubygems'2require 'twilio-ruby'3require 'sinatra'4require 'json'56set :port, 808078# Get your Account Sid and Auth Token from twilio.com/user/account9account_sid = '{{ account_sid }}'10auth_token = '{{ auth_token }}'11workspace_sid = '{{ workspace_sid }}'12workflow_sid = '{{ workflow_sid }}'1314client = Twilio::REST::Client.new(account_sid, auth_token)1516post '/assignment_callback' do17# Respond to assignment callbacks with accept instruction18content_type :json19{"instruction": "accept"}.to_json20end2122get '/create-task' do23# Create a task24task = client.taskrouter.workspaces(workspace_sid)25.tasks26.create(27attributes: {28'selected_language' => 'es'29}.to_json,30workflow_sid: workflow_sid31)32task.attributes33end3435get '/accept_reservation' do36# Accept a Reservation37task_sid = params[:task_sid]38reservation_sid = params[:reservation_sid]3940reservation = client.taskrouter.workspaces(workspace_sid)41.tasks(task_sid)42.reservations(reservation_sid)43.update(reservation_status: 'accepted')44reservation.worker_name45end
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 console, 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'll learn about shortcuts to create Tasks originating from Twilio phone calls.