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

Looking for more inspiration?Visit the

Create Tasks from Phone Calls using TwiML: Create a TaskRouter Task using <Enqueue>


In the previous step we received a call to a Twilio phone number and prompted the caller to select a preferred language. This step handles that input. Create a new endpoint called 'enqueue_call' and add the following code.

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
57
58
post '/enqueue_call' do
59
digit_pressed = params[:Digits]
60
if digit_pressed == 1
61
language = "es"
62
else
63
language = "en"
64
end
65
66
attributes = '{"selected_language":"'+language+'"}'
67
68
Twilio::TwiML::VoiceResponse.new do |r|
69
r.enqueue workflowSid: workflow_sid do |e|
70
e.task attributes
71
end
72
end.to_s
73
end

Now call your Twilio phone number. When prompted, press one for Spanish. You should hear Twilio's default <Queue> hold music. Congratulations! You just added yourself to the 'Customer Care Requests - Spanish' Task Queue based on your selected language. To clarify how exactly this happened, look more closely at what is returned from enqueue_call to Twilio when our caller presses one:

enqueue_call - TwiML Output

enqueue_call---twiml-output page anchor
1
<?xml version="1.0" encoding="UTF-8"?>
2
<Response>
3
<Enqueue workflowSid="WW0123401234...">
4
<Task>{"selected_language": "es"}</Task>
5
</Enqueue>
6
</Response>

Just like when we created a Task using the TaskRouter REST API (via curl), a Task has been created with an attribute field selected_language of value "es". This instructs the Workflow to add the Task to the 'Customer Care Requests - Spanish' TaskQueue based on the Routing Configurations we defined when we set up our Workflow. TaskRouter then starts monitoring for an available Worker to handle the Task.

Looking in the TaskRouter web portal, you will see the newly created Task in the Tasks section, and if you make an eligible Worker available, you should see them assigned to handle the Task. However, we still need a way to bridge the caller to the Worker when the Worker becomes available.

In the next section, we'll use a special Assignment Instruction to easily dequeue the call and route it to an eligible Worker - our good friend Alice. For now, you can hang up the call on hold.

Next: Dequeue a Call to a Worker >>