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.
1# -*- coding: latin-1 -*-23from flask import Flask, request, Response4from twilio.rest import Client5from twilio.twiml.voice_response import VoiceResponse, Gather, Enqueue67app = Flask(__name__)89# Your Account Sid and Auth Token from twilio.com/user/account10account_sid = "{{ account_sid }}"11auth_token = "{{ auth_token }}"12workspace_sid = "{{ workspace_sid }}"13workflow_sid = "{{ workflow_sid }}"1415client = Client(account_sid, auth_token)1617@app.route("/assignment_callback", methods=['GET', 'POST'])18def assignment_callback():19"""Respond to assignment callbacks with an acceptance and 200 response"""2021ret = '{"instruction": "accept"}'22resp = Response(response=ret, status=200, mimetype='application/json')23return resp2425@app.route("/create_task", methods=['GET', 'POST'])26def create_task():27"""Creating a Task"""28task = client.taskrouter.workspaces(workspace_sid) \29.tasks.create(workflow_sid=workflow_sid, attributes='{"selected_language":"es"}')3031print(task.attributes)32resp = Response({}, status=200, mimetype='application/json')33return resp3435@app.route("/accept_reservation", methods=['GET', 'POST'])36def accept_reservation(task_sid, reservation_sid):37"""Accepting a Reservation"""38task_sid = request.args.get('task_sid')39reservation_sid = request.args.get('reservation_sid')4041reservation = client.taskrouter.workspaces(workspace_sid) \42.tasks(task_sid) \43.reservations(reservation_sid) \44.update(reservation_status='accepted')4546print(reservation.reservation_status)47print(reservation.worker_name)4849resp = Response({}, status=200, mimetype='application/json')50return resp5152@app.route("/incoming_call", methods=['GET', 'POST'])53def incoming_call():54"""Respond to incoming requests."""5556resp = VoiceResponse()57gather = Gather(num_digits=1, action="/enqueue_call", method="POST", timeout=5)58gather.say("Para Español oprime el uno.", language='es')59gather.say("For English, please hold or press two.", language='en')60resp.append(gather)6162return str(resp)6364@app.route("/enqueue_call", methods=['GET', 'POST'])65def enqueue_call():66digit_pressed = request.args.get('Digits')67if digit_pressed == 1 :68language = "es"69else:70language = "en"7172resp = VoiceResponse()73enqueue = resp.enqueue(None, workflow_sid=workflow_sid)74enqueue.task('{"selected_language":"' + language + '"}')75resp.append(enqueue)7677return str(resp)7879if __name__ == "__main__":80app.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:
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.