Join us in San Francisco on the 29/30th of October for two days of developer workshops and technical talks

Earmuff a call

This code snippet earmuffs and unearmuffs a call. When a call is earmuffed, the user cannot hear other participants, but other participants can hear the user.

Example

Replace the following variables in the example code:

Key Description
UUID The UUID of the call you want to update.

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Generate your JWT

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 earmuff-a-call.sh:

Copy to Clipboard
curl -X PUT https://api.nexmo.com/v1/calls/$UUID \
  -H "Authorization: Bearer "$JWT\
  -H "Content-Type: application/json"\
  -d '{"action": "earmuff"}'

sleep 5s

curl -X PUT https://api.nexmo.com/v1/calls/$UUID \
  -H "Authorization: Bearer "$JWT\
  -H "Content-Type: application/json"\
  -d '{"action": "unearmuff"}'

View full source

Run your code

Save this file to your machine and run it:

bash earmuff-a-call.sh

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

npm install nexmo
Initialize your dependencies

Create a file named earmuff-a-call.js and add the following code:

Copy to Clipboard
const Nexmo = require('nexmo');

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET,
  applicationId: NEXMO_APPLICATION_ID,
  privateKey: NEXMO_PRIVATE_KEY
}, {debug: true});

View full source

Write the code

Add the following to earmuff-a-call.js:

Copy to Clipboard
nexmo.calls.update(CALL_UUID, { action: 'earmuff' }, (err, res) => {
  if(err) { console.error(err); }
  else {
      console.log(res);
  }
});

function unearmuff (){
    nexmo.calls.update(CALL_UUID, { action: 'unearmuff' }, (err, res) => {
        if(err) { console.error(err); }
        else {
            console.log(res);
        }
    });
}

setTimeout(unearmuff, 3000); // delay 3 seconds

View full source

Run your code

Save this file to your machine and run it:

node earmuff-a-call.js

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

Add the following to build.gradle:

compile 'com.nexmo:client:5.1.0'
Initialize your dependencies

Create a class named EarmuffCall and add the following code to the main method:

Copy to Clipboard
NexmoClient client = NexmoClient.builder()
        .applicationId(NEXMO_APPLICATION_ID)
        .privateKeyPath(NEXMO_PRIVATE_KEY_PATH)
        .build();

View full source

Write the code

Add the following to the main method of the EarmuffCall class:

Copy to Clipboard
client.getVoiceClient().modifyCall(UUID, ModifyCallAction.EARMUFF);
Thread.sleep(3000);
client.getVoiceClient().modifyCall(UUID, ModifyCallAction.UNEARMUFF);

View full source

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.nexmo.quickstart.voice with the package containing EarmuffCall:

gradle run -Pmain=com.nexmo.quickstart.voice.EarmuffCall

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

Install-Package Nexmo.Csharp.Client
Initialize your dependencies

Create a file named VoiceController.cs and add the following code:

Copy to Clipboard
var client = new Client(creds: new Nexmo.Api.Request.Credentials
{
    ApiKey = "NEXMO_API_KEY",
    ApiSecret = "NEXMO_API_SECRET",
    ApplicationId = "NEXMO_APPLICATION_ID",
    ApplicationKey = "NEXMO_APPLICATION_PRIVATE_KEY"
});

View full source

Write the code

Add the following to VoiceController.cs:

Copy to Clipboard
var result = Client.Call.Edit(UUID, new Call.CallEditCommand
{
    Action = "earmuff"
});

Thread.Sleep(3000);

var unearmuffresult = Client.Call.Edit(UUID, new Call.CallEditCommand
{
    Action = "unearmuff"
});

View full source

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

composer require nexmo/client
Initialize your dependencies

Create a file named index.php and add the following code:

Copy to Clipboard
$keypair = new \Nexmo\Client\Credentials\Keypair(
    file_get_contents(NEXMO_APPLICATION_PRIVATE_KEY_PATH),
    NEXMO_APPLICATION_ID
);

$client = new \Nexmo\Client($keypair);

View full source

Write the code

Add the following to index.php:

Copy to Clipboard
$client->calls[UUID]->put(new \Nexmo\Call\Earmuff());
sleep(3);
$client->calls[UUID]->put(new \Nexmo\Call\Unearmuff());

View full source

Run your code

Save this file to your machine and run it:

php index.php

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

pip install nexmo
Initialize your dependencies

Create a file named earmuff-a-call.py and add the following code:

Copy to Clipboard
import nexmo
import time
from pprint import pprint

client = nexmo.Client(
    application_id=APPLICATION_ID,
    private_key=APPLICATION_PRIVATE_KEY_PATH,
)

View full source

Write the code

Add the following to earmuff-a-call.py:

Copy to Clipboard
response = client.update_call(UUID, action="earmuff")
pprint(response)
time.sleep(5)
response = client.update_call(UUID, action="unearmuff")
pprint(response)

View full source

Run your code

Save this file to your machine and run it:

python3 earmuff-a-call.py

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

gem install nexmo
Initialize your dependencies

Create a file named earmuff-a-call.rb and add the following code:

Copy to Clipboard
client = Nexmo::Client.new(
  application_id: NEXMO_APPLICATION_ID,
  private_key: File.read(NEXMO_APPLICATION_PRIVATE_KEY_PATH)
)

View full source

Write the code

Add the following to earmuff-a-call.rb:

Copy to Clipboard
client.calls.earmuff(UUID)

sleep(5)

client.calls.unearmuff(UUID)

View full source

Run your code

Save this file to your machine and run it:

ruby earmuff-a-call.rb

Try it out

When you run the code the call identified by UUID will be earmuffed or unearmuffed.