# Create Tasks from Phone Calls using TwiML: Dequeue a Call to a Worker

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](/docs/taskrouter/quickstart/ruby/setup) 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](https://www.twilio.com/console/taskrouter/workspaces), 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:

```json
{"languages": ["en", "es"], "contact_uri": "+15555555555"}
```

Or, as displayed in the web portal:

![Alice's worker details with offline status and contact URI.](https://docs-resources.prod.twilio.com/0a50132651449983733d6918f0ab2ac94472755b582271989b7410b222544b48.png)

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](/docs/taskrouter/handle-assignment-callbacks); 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 [the Twilio Dev Phone](/docs/labs/dev-phone) 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](https://www.twilio.com/console/taskrouter/workspaces), 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:

![Form to create activity with name WrapUp in Customer Care Center workspace, availability set to unavailable.](https://docs-resources.prod.twilio.com/a7fd4be1512fd8c7a57e183f5ad88c45a46114f3c67575bbf7e3d3f91c8f86a9.png)

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:

## server.rb

```rb
require 'rubygems'
require 'twilio-ruby'
require 'sinatra'
require 'json'

set :port, 8080

# Get your Account Sid and Auth Token from twilio.com/user/account
account_sid = '{{ account_sid }}'
auth_token = '{{ auth_token }}'
workspace_sid = '{{ workspace_sid }}'
workflow_sid = '{{ workflow_sid }}'

@client = Twilio::REST::Client.new(account_sid, auth_token)

post '/assignment_callback' do
  # Respond to assignment callbacks with accept instruction
  content_type :json
  # from must be a verified phone number from your twilio account
  {
    "instruction" => "dequeue",
    "from" => "+15556667777",
    "post_work_activity_sid" => "WA0123401234..."
  }.to_json
end

get '/create-task' do
  # Create a task
  task = @client.taskrouter.workspaces(workspace_sid)
                            .tasks
                            .create(
                                attributes: {
                                      'selected_language' => 'es'
                            }.to_json,
                                workflow_sid: workflow_sid
                            )
  task.attributes
end

get '/accept_reservation' do
  # Accept a Reservation
  task_sid = params[:task_sid]
  reservation_sid = params[:reservation_sid]

  reservation = @client.taskrouter.workspaces(workspace_sid)
                        .tasks(task_sid)
                        .reservations(reservation_sid)
                        .update(reservation_status: 'accepted')
  reservation.worker_name
end

get '/incoming_call' do
  Twilio::TwiML::VoiceResponse.new do |r|
    r.gather(action: '/enqueue_call', method: 'POST', timeout: 5, num_digits: 1) do |gather|
      gather.say('Para Español oprime el uno.', language: 'es')
      gather.say('For English, please hold or press two.', language: 'en')
    end
  end.to_s
end

post '/enqueue_call' do
  digit_pressed = params[:Digits]
  if digit_pressed == 1
    language = "es"
  else
    language = "en"
  end

  attributes = '{"selected_language":"'+language+'"}'

  Twilio::TwiML::VoiceResponse.new do |r|
    r.enqueue workflowSid: workflow_sid do |e|
      e.task attributes
    end
  end.to_s
end
```

This returns a very simple JSON object from the Assignment Callback URL:

## JSON Output

```js
{"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.

[Next: End-to-End Phone Call Task Assignment »](/docs/taskrouter/quickstart/ruby/twiml-end-to-end)
