Transfer a call
A code snippet that shows how to transfer control of the current call to control by a new NCCO.
Example
Replace the following variables in the example code:
Key | Description |
---|---|
UUID |
The UUID of the call leg |
NCCO_URL |
The URL of the NCCO to execute. URLs must be array type. |
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
Execute the following command at your terminal prompt to create the JWT for authentication:
export JWT=$(nexmo jwt:generate $PATH_TO_PRIVATE_KEY application_id=$NEXMO_APPLICATION_ID)
Write the code
Add the following to transfer-a-call.sh
:
curl -X PUT https://api.nexmo.com/v1/calls/$UUID \
-H "Authorization: Bearer "$JWT \
-H "Content-Type: application/json"\
-d '{"action": "transfer",
"destination": {"type": "ncco", "url": ["https://developer.nexmo.com/ncco/tts.json"]}}'
Run your code
Save this file to your machine and run it:
bash transfer-a-call.sh
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
npm install @vonage/server-sdk
Create a file named transfer-call.js
and add the following code:
const Vonage = require('@vonage/server-sdk');
const vonage = new Vonage({
apiKey: VONAGE_API_KEY,
apiSecret: VONAGE_API_SECRET,
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY
}, { debug: true });
Write the code
Add the following to transfer-call.js
:
const NCCO_URL = "https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json";
vonage.calls.update(UUID, {
action: 'transfer',
destination: {
"type": "ncco",
"url": [NCCO_URL]
}
}, (err, res) => {
if (err) {
console.error(err);
} else {
console.log(res);
}
});
Run your code
Save this file to your machine and run it:
node transfer-call.js
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
Add the following to `build.gradle`:
compile 'com.vonage:client:6.2.0'
Create a class named TransferCall
and add the following code to the main
method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();
Write the code
Add the following to the main
method of the TransferCall
class:
/*
Establish a call for testing purposes.
*/
final String ANSWER_URL = "https://nexmo-community.github.io/ncco-examples/long-tts.json";
CallEvent call = client.getVoiceClient().createCall(new Call(
TO_NUMBER,
VONAGE_NUMBER,
ANSWER_URL
));
/*
Give them time to answer.
*/
Thread.sleep(10000);
final String UUID = call.getUuid();
final String NCCO_URL = "https://nexmo-community.github.io/ncco-examples/talk.json";
client.getVoiceClient().transferCall(UUID, NCCO_URL);
Run your code
We can use the application
plugin for Gradle to simplify the running of our application.
Update your build.gradle
with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''
Run the following gradle
command to execute your application, replacing com.vonage.quickstart.voice
with the package containing TransferCall
:
gradle run -Pmain=com.vonage.quickstart.voice.TransferCall
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
Install-Package Vonage
Create a file named TransferCall.cs
and add the following code:
using Vonage;
using Vonage.Request;
using Vonage.Voice;
Add the following to TransferCall.cs
:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);
Write the code
Add the following to TransferCall.cs
:
var callEditCommand = new CallEditCommand()
{
Action = CallEditCommand.ActionType.transfer,
Destination = new Destination()
{
Url = new[] { NCCO_URL }
}
};
var response = client.VoiceClient.UpdateCall(UUID, callEditCommand);
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
composer require vonage/client
Create a file named index.php
and add the following code:
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);
Write the code
Add the following to index.php
:
$call = $client->voice()->get(UUID);
$client->voice()->transferCall(
$call->getUuid(),
new \Vonage\Voice\NCCO\Action\Transfer("https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/transfer.json")
);
Run your code
Save this file to your machine and run it:
php index.php
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
pip install vonage
Create a file named transfer-a-call.py
and add the following code:
client = vonage.Client(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)
Write the code
Add the following to transfer-a-call.py
:
voice = vonage.Voice(client)
dest = {"type": "ncco", "url": ["https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json"]}
response = voice.update_call(UUID, action="transfer", destination=dest)
pprint(response)
Run your code
Save this file to your machine and run it:
python3 transfer-a-call.py
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
gem install vonage
Create a file named transfer-a-call.rb
and add the following code:
client = Vonage::Client.new(
application_id: VONAGE_APPLICATION_ID,
private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
Write the code
Add the following to transfer-a-call.rb
:
ncco = {"type": "ncco", "url": ["https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/transfer.json"]}
response = client.voice.transfer(UUID, destination: ncco)
Run your code
Save this file to your machine and run it:
ruby transfer-a-call.rb
Try it out
You will need to:
- Set up a call and obtain the call UUID. You could use the 'connect an inbound call' code snippet to do this.
- Run the example code to transfer the call.
- Control will be transferred to a new NCCO, and you will hear a text-to-speech message to confirm this.