Skip to contentSkip to navigationSkip to topbar
Rate this page:
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

_80
# -*- coding: latin-1 -*-
_80
_80
from flask import Flask, request, Response
_80
from twilio.rest import Client
_80
from twilio.twiml.voice_response import VoiceResponse, Gather, Enqueue
_80
_80
app = Flask(__name__)
_80
_80
# Your Account Sid and Auth Token from twilio.com/user/account
_80
account_sid = "{{ account_sid }}"
_80
auth_token = "{{ auth_token }}"
_80
workspace_sid = "{{ workspace_sid }}"
_80
workflow_sid = "{{ workflow_sid }}"
_80
_80
client = Client(account_sid, auth_token)
_80
_80
@app.route("/assignment_callback", methods=['GET', 'POST'])
_80
def assignment_callback():
_80
"""Respond to assignment callbacks with an acceptance and 200 response"""
_80
_80
ret = '{"instruction": "accept"}'
_80
resp = Response(response=ret, status=200, mimetype='application/json')
_80
return resp
_80
_80
@app.route("/create_task", methods=['GET', 'POST'])
_80
def create_task():
_80
"""Creating a Task"""
_80
task = client.taskrouter.workspaces(workspace_sid) \
_80
.tasks.create(workflow_sid=workflow_sid, attributes='{"selected_language":"es"}')
_80
_80
print(task.attributes)
_80
resp = Response({}, status=200, mimetype='application/json')
_80
return resp
_80
_80
@app.route("/accept_reservation", methods=['GET', 'POST'])
_80
def accept_reservation(task_sid, reservation_sid):
_80
"""Accepting a Reservation"""
_80
task_sid = request.args.get('task_sid')
_80
reservation_sid = request.args.get('reservation_sid')
_80
_80
reservation = client.taskrouter.workspaces(workspace_sid) \
_80
.tasks(task_sid) \
_80
.reservations(reservation_sid) \
_80
.update(reservation_status='accepted')
_80
_80
print(reservation.reservation_status)
_80
print(reservation.worker_name)
_80
_80
resp = Response({}, status=200, mimetype='application/json')
_80
return resp
_80
_80
@app.route("/incoming_call", methods=['GET', 'POST'])
_80
def incoming_call():
_80
"""Respond to incoming requests."""
_80
_80
resp = VoiceResponse()
_80
gather = Gather(num_digits=1, action="/enqueue_call", method="POST", timeout=5)
_80
gather.say("Para Español oprime el uno.", language='es')
_80
gather.say("For English, please hold or press two.", language='en')
_80
resp.append(gather)
_80
_80
return str(resp)
_80
_80
@app.route("/enqueue_call", methods=['GET', 'POST'])
_80
def enqueue_call():
_80
digit_pressed = request.args.get('Digits')
_80
if digit_pressed == 1 :
_80
language = "es"
_80
else:
_80
language = "en"
_80
_80
resp = VoiceResponse()
_80
enqueue = resp.enqueue(None, workflow_sid=workflow_sid)
_80
enqueue.task('{"selected_language":"' + language + '"}')
_80
resp.append(enqueue)
_80
_80
return str(resp)
_80
_80
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

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Enqueue workflowSid="WW0123401234...">
_10
<Task>{"selected_language": "es"}</Task>
_10
</Enqueue>
_10
</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 »


Rate this page: