We've seen how to create Tasks using the TaskRouter REST API and how to accept a Task Reservation using both the REST API and Assignment Callback instructions. TaskRouter also introduces new TwiML instructions that you can use to create a Task from a Twilio phone call.
To receive an incoming phone call, we first need a Twilio phone number. In this example we'll use a US toll-free number, but you can use a Voice capable number from any country.
Before purchasing or setting up the phone number, we need to add on to our run.py
to handle incoming calls:
_65# -*- coding: latin-1 -*-_65_65from flask import Flask, request, Response_65from twilio.rest import Client_65from twilio.twiml.voice_response import VoiceResponse_65_65app = Flask(__name__)_65_65# Your Account Sid and Auth Token from twilio.com/user/account_65account_sid = "{{ account_sid }}"_65auth_token = "{{ auth_token }}"_65workspace_sid = "{{ workspace_sid }}"_65workflow_sid = "{{ workflow_sid }}"_65_65client = Client(account_sid, auth_token)_65_65@app.route("/assignment_callback", methods=['GET', 'POST'])_65def assignment_callback():_65 """Respond to assignment callbacks with an acceptance and 200 response"""_65_65 ret = '{"instruction": "accept"}'_65 resp = Response(response=ret, status=200, mimetype='application/json')_65 return resp_65_65@app.route("/create_task", methods=['GET', 'POST'])_65def create_task():_65 """Creating a Task"""_65 task = client.workspaces(workspace_sid) \_65 .tasks.create(workflow_sid=workflow_sid,_65 attributes='{"selected_language":"es"}')_65_65 print(task.attributes)_65 resp = Response({}, status=200, mimetype='application/json')_65 return resp_65_65@app.route("/accept_reservation", methods=['GET', 'POST'])_65def accept_reservation(task_sid, reservation_sid):_65 """Accepting a Reservation"""_65 task_sid = request.args.get('task_sid')_65 reservation_sid = request.args.get('reservation_sid')_65_65 reservation = client.workspaces(workspace_sid) \_65 .tasks(task_sid) \_65 .reservations(reservation_sid) \_65 .update(reservation_status='accepted')_65_65 print(reservation.reservation_status)_65 print(reservation.worker_name)_65_65 resp = Response({}, status=200, mimetype='application/json')_65 return resp_65_65@app.route("/incoming_call", methods=['GET', 'POST'])_65def incoming_call():_65 """Respond to incoming requests."""_65_65 resp = VoiceResponse()_65 with resp.gather(numDigits=1, action="/enqueue_call", method="POST", timeout=5) as g:_65 g.say("Para Español oprime el uno.", language='es')_65 g.say("For English, please hold or press two.", language='en')_65_65 return str(resp)_65_65if __name__ == "__main__":_65 app.run(debug=True)
You can use the Buy Numbers section of the Twilio Voice and Messaging web portal to purchase a new phone number, or use an existing Twilio phone number. Open the phone number details page and point the Voice Request URL at your new endpoint:
Using any phone, call the Twilio number. You will be prompted to press one for Spanish or two for English. However, when you press a digit, you'll hear an error message. That's because our <Gather>
verb is pointing to another endpoint, enqueue_call
, which we haven't implemented yet. In the next step we'll add the required endpoint and use it to create a new Task based on the language selected by the caller.