Modify Calls In Progress with Ruby
Learn to modify live phone calls and change active call execution with Twilio Programmable Voice and your Ruby web application by using the Twilio SDKs to send a POST request to update an active Call resource. You can use this guide to implement call center operations, build interactive voice response systems, handle dynamic call routing, and manage active call sessions.
See Related reference documentation to learn more about the Call resource and state management parameters used in this guide.
We'll use Twilio's Ruby SDK in these examples to interact with the Twilio REST API's Calls endpoint.
The simplest way to control the flow of a Twilio phone call is with TwiML itself.
You can use the "action" parameters of verbs like <Gather> and <Record> to tell Twilio to get new instructions from your applications during a call. You can also use the <Redirect> verb to explicitly tell Twilio to fetch new TwiML.
But sometimes you need to change a live phone call outside of Twilio's normal request-response cycle. For those cases you can update the Call to tell Twilio to immediately change the TwiML it's using in a phone call.
1# Download the helper library from https://www.twilio.com/docs/ruby/install2require 'twilio-ruby'34# Find your Account SID and Auth Token at twilio.com/console5# and set the environment variables. See http://twil.io/secure6account_sid = ENV['TWILIO_ACCOUNT_SID']7auth_token = ENV['TWILIO_AUTH_TOKEN']8@client = Twilio::REST::Client.new(account_sid, auth_token)910call = @client11.api12.v201013.calls('CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')14.update(twiml: '<Response><Say>Ahoy there</Say></Response>')1516puts call.sid
Response
1{2"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"answered_by": null,4"api_version": "2010-04-01",5"caller_name": null,6"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",7"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",8"direction": "inbound",9"duration": "15",10"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",11"forwarded_from": "+141586753093",12"from": "+14158675308",13"from_formatted": "(415) 867-5308",14"group_sid": null,15"parent_call_sid": null,16"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",17"price": "-0.03000",18"price_unit": "USD",19"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",20"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",21"status": "completed",22"subresource_uris": {23"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",24"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",25"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",26"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",27"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",28"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",29"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",30"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",31"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"32},33"to": "+14158675309",34"to_formatted": "(415) 867-5309",35"trunk_sid": null,36"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",37"queue_time": "1000"38}
In order to update the call you will need to use the CallSid. Twilio returns this when you initiate an outgoing call, and also includes the CallSid in its request to your application for an incoming call's initial TwiML.
As an alternative to updating your call with TwiML directly, you may also redirect a Call to a new URL that responds with your requested TwiML.
1# Download the helper library from https://www.twilio.com/docs/ruby/install2require 'twilio-ruby'34# Find your Account SID and Auth Token at twilio.com/console5# and set the environment variables. See http://twil.io/secure6account_sid = ENV['TWILIO_ACCOUNT_SID']7auth_token = ENV['TWILIO_AUTH_TOKEN']8@client = Twilio::REST::Client.new(account_sid, auth_token)910call = @client11.api12.v201013.calls('CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')14.update(15method: 'POST',16url: 'http://demo.twilio.com/docs/voice.xml'17)1819puts call.sid
Response
1{2"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"answered_by": null,4"api_version": "2010-04-01",5"caller_name": null,6"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",7"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",8"direction": "inbound",9"duration": "15",10"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",11"forwarded_from": "+141586753093",12"from": "+14158675308",13"from_formatted": "(415) 867-5308",14"group_sid": null,15"parent_call_sid": null,16"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",17"price": "-0.03000",18"price_unit": "USD",19"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",20"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",21"status": "completed",22"subresource_uris": {23"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",24"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",25"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",26"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",27"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",28"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",29"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",30"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",31"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"32},33"to": "+14158675309",34"to_formatted": "(415) 867-5309",35"trunk_sid": null,36"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",37"queue_time": "1000"38}
Twilio will end phone calls for you when it encounters a <Hangup> verb or when it runs out of TwiML to process. But you can also end phone calls whenever you like by passing a "completed" status to a CallSid in progress.
1# Download the helper library from https://www.twilio.com/docs/ruby/install2require 'twilio-ruby'34# Find your Account SID and Auth Token at twilio.com/console5# and set the environment variables. See http://twil.io/secure6account_sid = ENV['TWILIO_ACCOUNT_SID']7auth_token = ENV['TWILIO_AUTH_TOKEN']8@client = Twilio::REST::Client.new(account_sid, auth_token)910call = @client11.api12.v201013.calls('CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')14.update(status: 'completed')1516puts call.to
Response
1{2"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"answered_by": null,4"api_version": "2010-04-01",5"caller_name": null,6"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",7"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",8"direction": "inbound",9"duration": "15",10"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",11"forwarded_from": "+141586753093",12"from": "+14158675308",13"from_formatted": "(415) 867-5308",14"group_sid": null,15"parent_call_sid": null,16"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",17"price": "-0.03000",18"price_unit": "USD",19"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",20"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",21"status": "completed",22"subresource_uris": {23"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",24"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",25"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",26"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",27"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",28"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",29"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",30"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",31"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"32},33"to": "+14158675309",34"to_formatted": "(415) 867-5309",35"trunk_sid": null,36"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",37"queue_time": "1000"38}
Check out Modifying Live Calls in the reference docs for more details.
See use cases for modifying live calls with Twilio Programmable Voice to learn about the different applications you can build with this feature.
After following this guide, you can programmatically modify live phone calls using Twilio Programmable Voice and the Twilio SDKs. You will have built code that can dynamically update call directions or explicitly update the call status to complete active sessions on your account.
Explore the following guides to build on what you've learned in this guide:
- Make outbound phone calls: Learn how to spin up completely new outbound calls from your web application.
- Respond to incoming phone calls: Learn how to handle incoming webhooks from Twilio to start initial phone call routines.