Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

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


In the previous step, you created a Task and accepted it using the Reservations subresource of the REST API. This time, you create another Task using the REST API, but have your 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 run.py again, or execute the following curl command:

1
curl 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 run.py. Open it and modify the existing code to reflect the following:


run.py

runpy page anchor
1
from flask import Flask, request, Response
2
from twilio.rest import Client
3
4
app = Flask(__name__)
5
6
# Your Account Sid and Auth Token from twilio.com/user/account
7
account_sid = "{{ account_sid }}"
8
auth_token = "{{ auth_token }}"
9
workspace_sid = "{{ workspace_sid }}"
10
workflow_sid = "{{ workflow_sid }}"
11
12
client = Client(account_sid, auth_token)
13
14
@app.route("/assignment_callback", methods=['GET', 'POST'])
15
def assignment_callback():
16
"""Respond to assignment callbacks with an acceptance and 200 response"""
17
18
ret = '{"instruction": "accept"}'
19
resp = Response(response=ret, status=200, mimetype='application/json')
20
return resp
21
22
@app.route("/create_task", methods=['GET', 'POST'])
23
def create_task():
24
"""Creating a Task"""
25
task = client.taskrouter.workspaces(workspace_sid) \
26
.tasks.create(workflow_sid=workflow_sid,
27
attributes='{"selected_language":"es"}')
28
29
print(task.attributes)
30
resp = Response({}, status=200, mimetype='application/json')
31
return resp
32
33
@app.route("/accept_reservation", methods=['GET', 'POST'])
34
def accept_reservation():
35
"""Accepting a Reservation"""
36
task_sid = request.args.get('task_sid')
37
reservation_sid = request.args.get('reservation_sid')
38
39
reservation = client.taskrouter.workspaces(workspace_sid) \
40
.tasks(task_sid) \
41
.reservations(reservation_sid) \
42
.update(reservation_status='accepted')
43
44
print(reservation.reservation_status)
45
print(reservation.worker_name)
46
47
resp = Response({}, status=200, mimetype='application/json')
48
return resp
49
50
if __name__ == "__main__":
51
app.run(debug=True)

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(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 in Customer Care Requests queue assigned with priority 1 and age 128.

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's status is Busy and not available as of 00:21:22 EST on 2015-02-11.

You created another Task using the REST API, accepted it via an assignment instruction at your Workflow's Assignment Callback URL, and saw that this immediately accepted the Reservation for your Worker.

Next, you learn shortcuts to create Tasks that originate from Twilio phone calls.

Part 3: Create Tasks from Phone Calls using TwiML »