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.
_80# -*- coding: latin-1 -*-_80_80from flask import Flask, request, Response_80from twilio.rest import Client_80from twilio.twiml.voice_response import VoiceResponse, Gather, Enqueue_80_80app = Flask(__name__)_80_80# Your Account Sid and Auth Token from twilio.com/user/account_80account_sid = "{{ account_sid }}"_80auth_token = "{{ auth_token }}"_80workspace_sid = "{{ workspace_sid }}"_80workflow_sid = "{{ workflow_sid }}"_80_80client = Client(account_sid, auth_token)_80_80@app.route("/assignment_callback", methods=['GET', 'POST'])_80def 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'])_80def 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'])_80def 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'])_80def 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'])_80def 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_80if __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:
_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.