Skip to contentSkip to navigationSkip to topbar

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. But when the caller selected their language, we weren't ready to handle that input. Let's fix that. Create a new endpoint called 'enqueue_call' and add the following code.

server.rb

1
require 'rubygems'
2
require 'twilio-ruby'
3
require 'sinatra'
4
require 'json'
5
6
set :port, 8080
7
8
# Get your Account Sid and Auth Token from twilio.com/user/account
9
account_sid = 'AC99ba7b61fbdb6c039698505dea5f044c'
10
auth_token = '{{ auth_token }}'
11
workspace_sid = '{{ workspace_sid }}'
12
workflow_sid = '{{ workflow_sid }}'
13
14
client = Twilio::REST::Client.new(account_sid, auth_token)
15
16
post '/assignment_callback' do
17
# Respond to assignment callbacks with accept instruction
18
content_type :json
19
{"instruction": "accept"}.to_json
20
end
21
22
get '/create-task' do
23
# Create a task
24
task = client.taskrouter.workspaces(workspace_sid)
25
.tasks
26
.create(
27
attributes: {
28
'selected_language' => 'es'
29
}.to_json,
30
workflow_sid: workflow_sid
31
)
32
task.attributes
33
end
34
35
get '/accept_reservation' do
36
# Accept a Reservation
37
task_sid = params[:task_sid]
38
reservation_sid = params[:reservation_sid]
39
40
reservation = client.taskrouter.workspaces(workspace_sid)
41
.tasks(task_sid)
42
.reservations(reservation_sid)
43
.update(reservation_status: 'accepted')
44
reservation.worker_name
45
end
46
47
get '/incoming_call' do
48
Twilio::TwiML::VoiceResponse.new do |r|
49
r.gather(action: '/enqueue_call', method: 'POST', timeout: 5, num_digits: 1) do |gather|
50
gather.say(message: 'Para Español oprime el uno.', language: 'es')
51
gather.say(message: 'For English, please hold or press two.', language: 'en')
52
end
53
end.to_s
54
end
55
56
post '/enqueue_call' do
57
digit_pressed = params[:Digits]
58
if digit_pressed == 1
59
language = "es"
60
else
61
language = "en"
62
end
63
64
attributes = '{"selected_language":"'+language+'"}'
65
66
Twilio::TwiML::VoiceResponse.new do |r|
67
r.enqueue workflowSid: workflow_sid do |e|
68
e.task attributes
69
end
70
end.to_s
71
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

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 don't yet have 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 >>

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.