Skip to contentSkip to navigationSkip to topbar
On this page

Create Tasks from Phone Calls using TwiML: Create a TaskRouter Task using <Enqueue>


In the previous step we received a call to a Twilio phone number and prompted the caller to select a preferred language. But when the caller selected their language, we weren't ready to handle that input. Let's fix that. Create a new endpoint called 'enqueue_call' and add the following code.


run.py

runpy page anchor
1
# -*- coding: latin-1 -*-
2
3
from flask import Flask, request, Response
4
from twilio.rest import Client
5
from twilio.twiml.voice_response import VoiceResponse, Gather, Enqueue
6
7
app = Flask(__name__)
8
9
# Your Account Sid and Auth Token from twilio.com/user/account
10
account_sid = "{{ account_sid }}"
11
auth_token = "{{ auth_token }}"
12
workspace_sid = "{{ workspace_sid }}"
13
workflow_sid = "{{ workflow_sid }}"
14
15
client = Client(account_sid, auth_token)
16
17
@app.route("/assignment_callback", methods=['GET', 'POST'])
18
def assignment_callback():
19
"""Respond to assignment callbacks with an acceptance and 200 response"""
20
21
ret = '{"instruction": "accept"}'
22
resp = Response(response=ret, status=200, mimetype='application/json')
23
return resp
24
25
@app.route("/create_task", methods=['GET', 'POST'])
26
def create_task():
27
"""Creating a Task"""
28
task = client.taskrouter.workspaces(workspace_sid) \
29
.tasks.create(workflow_sid=workflow_sid, attributes='{"selected_language":"es"}')
30
31
print(task.attributes)
32
resp = Response({}, status=200, mimetype='application/json')
33
return resp
34
35
@app.route("/accept_reservation", methods=['GET', 'POST'])
36
def accept_reservation(task_sid, reservation_sid):
37
"""Accepting a Reservation"""
38
task_sid = request.args.get('task_sid')
39
reservation_sid = request.args.get('reservation_sid')
40
41
reservation = client.taskrouter.workspaces(workspace_sid) \
42
.tasks(task_sid) \
43
.reservations(reservation_sid) \
44
.update(reservation_status='accepted')
45
46
print(reservation.reservation_status)
47
print(reservation.worker_name)
48
49
resp = Response({}, status=200, mimetype='application/json')
50
return resp
51
52
@app.route("/incoming_call", methods=['GET', 'POST'])
53
def incoming_call():
54
"""Respond to incoming requests."""
55
56
resp = VoiceResponse()
57
gather = Gather(num_digits=1, action="/enqueue_call", method="POST", timeout=5)
58
gather.say("Para Español oprime el uno.", language='es')
59
gather.say("For English, please hold or press two.", language='en')
60
resp.append(gather)
61
62
return str(resp)
63
64
@app.route("/enqueue_call", methods=['GET', 'POST'])
65
def enqueue_call():
66
digit_pressed = request.args.get('Digits')
67
if digit_pressed == 1 :
68
language = "es"
69
else:
70
language = "en"
71
72
resp = VoiceResponse()
73
enqueue = resp.enqueue(None, workflow_sid=workflow_sid)
74
enqueue.task('{"selected_language":"' + language + '"}')
75
resp.append(enqueue)
76
77
return str(resp)
78
79
if __name__ == "__main__":
80
app.run(debug=True)

Now call your Twilio phone number. When prompted, press one for Spanish. You should hear Twilio's default <Queue> hold music. Congratulations! You just added yourself to the 'Customer Care Requests - Spanish' Task Queue based on your selected language. To clarify how exactly this happened, look more closely at what is returned from enqueue_call to Twilio when our caller presses one:


enqueue_call - TwiML Output

enqueue_call---twiml-output page anchor
1
<?xml version="1.0" encoding="UTF-8"?>
2
<Response>
3
<Enqueue workflowSid="WW0123401234...">
4
<Task>{"selected_language": "es"}</Task>
5
</Enqueue>
6
</Response>

Just like when we created a Task using the TaskRouter REST API (via curl), a Task has been created with an attribute field selected_language of value "es". This instructs the Workflow to add the Task to the 'Customer Care Requests - Spanish' TaskQueue based on the Routing Configurations we defined when we set up our Workflow. TaskRouter then starts monitoring for an available Worker to handle the Task.

Looking in the TaskRouter web portal, you will see the newly created Task in the Tasks section, and if you make an eligible Worker available, you should see them assigned to handle the Task. However, we don't yet have a way to bridge the caller to the Worker when the Worker becomes available.

In the next section, we'll use a special Assignment Instruction to easily dequeue the call and route it to an eligible Worker - our good friend Alice. For now, you can hang up the call on hold.

Next: Dequeue a Call to a Worker »

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.