Skip to contentSkip to navigationSkip to topbar
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
1
# -*- coding: latin-1 -*-
2
3
from flask import Flask, request, Response
4
from twilio.rest import Client
5
from twilio.twiml.voice_response import VoiceResponse
6
7
app = Flask(__name__)
8
9
# Your Account Sid and Auth Token from twilio.com/user/account
10
account_sid = "{{ account_sid }}"
11
auth_token = "{{ auth_token }}"
12
workspace_sid = "{{ workspace_sid }}"
13
workflow_sid = "{{ workflow_sid }}"
14
15
client = Client(account_sid, auth_token)
16
17
@app.route("/assignment_callback", methods=['GET', 'POST'])
18
def assignment_callback():
19
"""Respond to assignment callbacks with an acceptance and 200 response"""
20
21
ret = '{"instruction": "accept"}'
22
resp = Response(response=ret, status=200, mimetype='application/json')
23
return resp
24
25
@app.route("/create_task", methods=['GET', 'POST'])
26
def create_task():
27
"""Creating a Task"""
28
task = client.workspaces(workspace_sid) \
29
.tasks.create(workflow_sid=workflow_sid,
30
attributes='{"selected_language":"es"}')
31
32
print(task.attributes)
33
resp = Response({}, status=200, mimetype='application/json')
34
return resp
35
36
@app.route("/accept_reservation", methods=['GET', 'POST'])
37
def accept_reservation(task_sid, reservation_sid):
38
"""Accepting a Reservation"""
39
task_sid = request.args.get('task_sid')
40
reservation_sid = request.args.get('reservation_sid')
41
42
reservation = client.workspaces(workspace_sid) \
43
.tasks(task_sid) \
44
.reservations(reservation_sid) \
45
.update(reservation_status='accepted')
46
47
print(reservation.reservation_status)
48
print(reservation.worker_name)
49
50
resp = Response({}, status=200, mimetype='application/json')
51
return resp
52
53
@app.route("/incoming_call", methods=['GET', 'POST'])
54
def incoming_call():
55
"""Respond to incoming requests."""
56
57
resp = VoiceResponse()
58
with resp.gather(numDigits=1, action="/enqueue_call", method="POST", timeout=5) as g:
59
g.say("Para Español oprime el uno.", language='es')
60
g.say("For English, please hold or press two.", language='en')
61
62
return str(resp)
63
64
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 »

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.