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.


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


_54
require 'rubygems'
_54
require 'twilio-ruby'
_54
require 'sinatra'
_54
require 'json'
_54
_54
set :port, 8080
_54
_54
# Get your Account Sid and Auth Token from twilio.com/user/account
_54
account_sid = 'AC99ba7b61fbdb6c039698505dea5f044c'
_54
auth_token = '{{ auth_token }}'
_54
workspace_sid = '{{ workspace_sid }}'
_54
workflow_sid = '{{ workflow_sid }}'
_54
_54
client = Twilio::REST::Client.new(account_sid, auth_token)
_54
_54
post '/assignment_callback' do
_54
# Respond to assignment callbacks with accept instruction
_54
content_type :json
_54
{"instruction": "accept"}.to_json
_54
end
_54
_54
get '/create-task' do
_54
# Create a task
_54
task = client.taskrouter.workspaces(workspace_sid)
_54
.tasks
_54
.create(
_54
attributes: {
_54
'selected_language' => 'es'
_54
}.to_json,
_54
workflow_sid: workflow_sid
_54
)
_54
task.attributes
_54
end
_54
_54
get '/accept_reservation' do
_54
# Accept a Reservation
_54
task_sid = params[:task_sid]
_54
reservation_sid = params[:reservation_sid]
_54
_54
reservation = client.taskrouter.workspaces(workspace_sid)
_54
.tasks(task_sid)
_54
.reservations(reservation_sid)
_54
.update(reservation_status: 'accepted')
_54
reservation.worker_name
_54
end
_54
_54
get '/incoming_call' do
_54
Twilio::TwiML::VoiceResponse.new do |r|
_54
r.gather(action: '/enqueue_call', method: 'POST', timeout: 5, num_digits: 1) do |gather|
_54
gather.say(message: 'Para Español oprime el uno.', language: 'es')
_54
gather.say(message: 'For English, please hold or press two.', language: 'en')
_54
end
_54
end.to_s
_54
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:

Set up voice call webhook.

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: