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
1require 'rubygems'2require 'twilio-ruby'3require 'sinatra'4require 'json'56set :port, 808078# Get your Account Sid and Auth Token from twilio.com/user/account9account_sid = 'AC99ba7b61fbdb6c039698505dea5f044c'10auth_token = '{{ auth_token }}'11workspace_sid = '{{ workspace_sid }}'12workflow_sid = '{{ workflow_sid }}'1314client = Twilio::REST::Client.new(account_sid, auth_token)1516post '/assignment_callback' do17# Respond to assignment callbacks with accept instruction18content_type :json19{"instruction": "accept"}.to_json20end2122get '/create-task' do23# Create a task24task = client.taskrouter.workspaces(workspace_sid)25.tasks26.create(27attributes: {28'selected_language' => 'es'29}.to_json,30workflow_sid: workflow_sid31)32task.attributes33end3435get '/accept_reservation' do36# Accept a Reservation37task_sid = params[:task_sid]38reservation_sid = params[:reservation_sid]3940reservation = client.taskrouter.workspaces(workspace_sid)41.tasks(task_sid)42.reservations(reservation_sid)43.update(reservation_status: 'accepted')44reservation.worker_name45end4647get '/incoming_call' do48Twilio::TwiML::VoiceResponse.new do |r|49r.gather(action: '/enqueue_call', method: 'POST', timeout: 5, num_digits: 1) do |gather|50gather.say(message: 'Para Español oprime el uno.', language: 'es')51gather.say(message: 'For English, please hold or press two.', language: 'en')52end53end.to_s54end5556post '/enqueue_call' do57digit_pressed = params[:Digits]58if digit_pressed == 159language = "es"60else61language = "en"62end6364attributes = '{"selected_language":"'+language+'"}'6566Twilio::TwiML::VoiceResponse.new do |r|67r.enqueue workflowSid: workflow_sid do |e|68e.task attributes69end70end.to_s71end
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.