Skip to contentSkip to navigationSkip to topbar
Page tools
Useful for sharing or LLM

On this page
Looking for more inspiration?Visit the

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.


Receive an Incoming Call

receive-an-incoming-call page anchor

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 server.rb to handle incoming calls:

server.rb

serverrb page anchor
1
require 'rubygems'
2
require 'twilio-ruby'
3
require 'sinatra'
4
require 'json'
5
6
set :port, 8080
7
8
# Find your Account SID at twilio.com/console
9
# Provision API Keys at twilio.com/console/runtime/api-keys
10
account_sid = 'AC99ba7b61fbdb6c039698505dea5f044c'
11
api_key = '{{ api_key }}'
12
api_secret = '{{ api_secret }}'
13
workspace_sid = '{{ workspace_sid }}'
14
workflow_sid = '{{ workflow_sid }}'
15
16
client = Twilio::REST::Client.new(api_key, api_secret, account_sid)
17
18
post '/assignment_callback' do
19
# Respond to assignment callbacks with accept instruction
20
content_type :json
21
{"instruction": "accept"}.to_json
22
end
23
24
get '/create-task' do
25
# Create a task
26
task = client.taskrouter.workspaces(workspace_sid)
27
.tasks
28
.create(
29
attributes: {
30
'selected_language' => 'es'
31
}.to_json,
32
workflow_sid: workflow_sid
33
)
34
task.attributes
35
end
36
37
get '/accept_reservation' do
38
# Accept a Reservation
39
task_sid = params[:task_sid]
40
reservation_sid = params[:reservation_sid]
41
42
reservation = client.taskrouter.workspaces(workspace_sid)
43
.tasks(task_sid)
44
.reservations(reservation_sid)
45
.update(reservation_status: 'accepted')
46
reservation.worker_name
47
end
48
49
get '/incoming_call' do
50
Twilio::TwiML::VoiceResponse.new do |r|
51
r.gather(action: '/enqueue_call', method: 'POST', timeout: 5, num_digits: 1) do |gather|
52
gather.say(message: 'Para Español oprime el uno.', language: 'es')
53
gather.say(message: 'For English, please hold or press two.', language: 'en')
54
end
55
end.to_s
56
end

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:

Voice call webhook settings with ngrok URL for incoming calls.

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 »