In the previous step we created a Task from an incoming phone call using <Enqueue workflowSid="WW0123401234..">
. In this step we will create another call and dequeue it to an eligible Worker when one becomes available.
Back in Part 1 of the Quickstart we created a Worker named Alice that is capable of handling both English and Spanish inquiries. With your Workspace open in the TaskRouter web portal, click 'Workers' and click to edit the details of our Worker Alice. Ensure that Alice is set to a non-available Activity state such as 'Offline'. Next, edit Alice's JSON attributes and add a "contact_uri" field. Replace the dummy 555 number below with your own phone number.
Alice's modified JSON attributes:
_10{"languages": ["en", "es"], "contact_uri": "+15555555555"}
Or, as displayed in the web portal:
In this step, we again use <Enqueue>
to create a Task from an incoming phone call. When an eligible Worker (in this case Alice) becomes available, TaskRouter will make a request to our Assignment Callback URL. This time, we will respond with a special 'dequeue' instruction; this tells Twilio to call Alice at her 'contact_uri' and bridge to the caller.
For this part of the Quickstart, although not totally necessary it will be useful to have two phones available - one to call your Twilio number, and one to receive a call as Alice. Experienced Twilio users might consider using Twilio Client as one of the endpoints.
Before we add the 'dequeue' assignment instruction we need to create a new Activity in our TaskRouter Workspace. One of the nice things about integrating TaskRouter with TwiML is that our Worker will automatically transition through various Activities as the call is assigned, answered and even hung up. We need an Activity for our Worker to transition to when the call ends.
With your Workspace open in the TaskRouter web portal, click 'Activities' and then 'Create Activity'. Give the new Activity a name of 'WrapUp' and a value of 'unavailable'. Once you've saved it, make a note of the Activity Sid:
To return the 'dequeue' assignment instruction, modify server.rb
assignment_callback endpoint to now issue a dequeue instruction, substituting your new WrapUp ActivitySid between the curly braces:
_76require 'rubygems'_76require 'twilio-ruby'_76require 'sinatra'_76require 'json'_76_76set :port, 8080_76_76# Get your Account Sid and Auth Token from twilio.com/user/account_76account_sid = '{{ account_sid }}'_76auth_token = '{{ auth_token }}'_76workspace_sid = '{{ workspace_sid }}'_76workflow_sid = '{{ workflow_sid }}'_76_76@client = Twilio::REST::Client.new(account_sid, auth_token)_76_76post '/assignment_callback' do_76 # Respond to assignment callbacks with accept instruction_76 content_type :json_76 # from must be a verified phone number from your twilio account_76 {_76 "instruction" => "dequeue",_76 "from" => "+15556667777",_76 "post_work_activity_sid" => "WA0123401234..."_76 }.to_json_76end_76_76get '/create-task' do_76 # Create a task_76 task = @client.taskrouter.workspaces(workspace_sid)_76 .tasks_76 .create(_76 attributes: {_76 'selected_language' => 'es'_76 }.to_json,_76 workflow_sid: workflow_sid_76 )_76 task.attributes_76end_76_76get '/accept_reservation' do_76 # Accept a Reservation_76 task_sid = params[:task_sid]_76 reservation_sid = params[:reservation_sid]_76_76 reservation = @client.taskrouter.workspaces(workspace_sid)_76 .tasks(task_sid)_76 .reservations(reservation_sid)_76 .update(reservation_status: 'accepted')_76 reservation.worker_name_76end_76_76get '/incoming_call' do_76 Twilio::TwiML::VoiceResponse.new do |r|_76 r.gather(action: '/enqueue_call', method: 'POST', timeout: 5, num_digits: 1) do |gather|_76 gather.say('Para Español oprime el uno.', language: 'es')_76 gather.say('For English, please hold or press two.', language: 'en')_76 end_76 end.to_s_76end_76_76post '/enqueue_call' do_76 digit_pressed = params[:Digits]_76 if digit_pressed == 1_76 language = "es"_76 else_76 language = "en"_76 end_76_76 attributes = '{"selected_language":"'+language+'"}'_76_76 Twilio::TwiML::VoiceResponse.new do |r|_76 r.enqueue workflowSid: workflow_sid do |e|_76 e.task attributes_76 end_76 end.to_s_76end
This returns a very simple JSON object from the Assignment Callback URL:
_10{"instruction":"dequeue", "from": "+15556667777", "post_work_activity_sid": "WA01234012340123401234"}
The JSON instructs Twilio to dequeue the waiting call and, because we don't include an explicit "to" field in our JSON, connect it to our Worker at their "contact_uri". This is convenient default behavior provided by TaskRouter.
In the next step, we test our incoming call flow from end-to-end.