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
_71require 'rubygems'_71require 'twilio-ruby'_71require 'sinatra'_71require 'json'_71_71set :port, 8080_71_71# Get your Account Sid and Auth Token from twilio.com/user/account_71account_sid = 'AC99ba7b61fbdb6c039698505dea5f044c'_71auth_token = '{{ auth_token }}'_71workspace_sid = '{{ workspace_sid }}'_71workflow_sid = '{{ workflow_sid }}'_71_71client = Twilio::REST::Client.new(account_sid, auth_token)_71_71post '/assignment_callback' do_71 # Respond to assignment callbacks with accept instruction_71 content_type :json_71 {"instruction": "accept"}.to_json_71end_71_71get '/create-task' do_71 # Create a task_71 task = client.taskrouter.workspaces(workspace_sid)_71 .tasks_71 .create(_71 attributes: {_71 'selected_language' => 'es'_71 }.to_json,_71 workflow_sid: workflow_sid_71 )_71 task.attributes_71end_71_71get '/accept_reservation' do_71 # Accept a Reservation_71 task_sid = params[:task_sid]_71 reservation_sid = params[:reservation_sid]_71_71 reservation = client.taskrouter.workspaces(workspace_sid)_71 .tasks(task_sid)_71 .reservations(reservation_sid)_71 .update(reservation_status: 'accepted')_71 reservation.worker_name_71end_71_71get '/incoming_call' do_71 Twilio::TwiML::VoiceResponse.new do |r|_71 r.gather(action: '/enqueue_call', method: 'POST', timeout: 5, num_digits: 1) do |gather|_71 gather.say(message: 'Para Español oprime el uno.', language: 'es')_71 gather.say(message: 'For English, please hold or press two.', language: 'en')_71 end_71 end.to_s_71end_71_71post '/enqueue_call' do_71 digit_pressed = params[:Digits]_71 if digit_pressed == 1_71 language = "es"_71 else_71 language = "en"_71 end_71_71 attributes = '{"selected_language":"'+language+'"}'_71_71 Twilio::TwiML::VoiceResponse.new do |r|_71 r.enqueue workflowSid: workflow_sid do |e|_71 e.task attributes_71 end_71 end.to_s_71end
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
_10<?xml version="1.0" encoding="UTF-8"?>_10<Response>_10<Enqueue workflowSid="WW0123401234...">_10<Task>{"selected_language": "es"}</Task>_10</Enqueue>_10</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.