Skip to contentSkip to navigationSkip to topbar
Rate this page:

Creating Tasks and Accepting Reservations: Accept a Reservation using Assignment Callback Instructions


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:


_10
curl 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 server.rb. Open it and modify the existing code to reflect the following:

server.rb


_45
require 'rubygems'
_45
require 'twilio-ruby'
_45
require 'sinatra'
_45
require 'json'
_45
_45
set :port, 8080
_45
_45
# Get your Account Sid and Auth Token from twilio.com/user/account
_45
account_sid = '{{ account_sid }}'
_45
auth_token = '{{ auth_token }}'
_45
workspace_sid = '{{ workspace_sid }}'
_45
workflow_sid = '{{ workflow_sid }}'
_45
_45
client = Twilio::REST::Client.new(account_sid, auth_token)
_45
_45
post '/assignment_callback' do
_45
# Respond to assignment callbacks with accept instruction
_45
content_type :json
_45
{"instruction": "accept"}.to_json
_45
end
_45
_45
get '/create-task' do
_45
# Create a task
_45
task = client.taskrouter.workspaces(workspace_sid)
_45
.tasks
_45
.create(
_45
attributes: {
_45
'selected_language' => 'es'
_45
}.to_json,
_45
workflow_sid: workflow_sid
_45
)
_45
task.attributes
_45
end
_45
_45
get '/accept_reservation' do
_45
# Accept a Reservation
_45
task_sid = params[:task_sid]
_45
reservation_sid = params[:reservation_sid]
_45
_45
reservation = client.taskrouter.workspaces(workspace_sid)
_45
.tasks(task_sid)
_45
.reservations(reservation_sid)
_45
.update(reservation_status: 'accepted')
_45
reservation.worker_name
_45
end

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(link takes you to an external page), 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':

Task is 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.

Alice is now Busy.

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.

Part 3: Create Tasks from Phone Calls using TwiML »


Rate this page: