Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Create Tasks from Phone Calls using TwiML: Receive an Incoming Call


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:


run.py

runpy page anchor

_65
# -*- coding: latin-1 -*-
_65
_65
from flask import Flask, request, Response
_65
from twilio.rest import Client
_65
from twilio.twiml.voice_response import VoiceResponse
_65
_65
app = Flask(__name__)
_65
_65
# Your Account Sid and Auth Token from twilio.com/user/account
_65
account_sid = "{{ account_sid }}"
_65
auth_token = "{{ auth_token }}"
_65
workspace_sid = "{{ workspace_sid }}"
_65
workflow_sid = "{{ workflow_sid }}"
_65
_65
client = Client(account_sid, auth_token)
_65
_65
@app.route("/assignment_callback", methods=['GET', 'POST'])
_65
def 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'])
_65
def 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'])
_65
def 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'])
_65
def 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
_65
if __name__ == "__main__":
_65
app.run(debug=True)

You can use the Buy Numbers(link takes you to an external page) 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:

Phone Number Setup Details.

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.

Next: Create a Task using Enqueue »


Rate this page: