Record a call
A code snippets that shows how to answer an incoming call and set it up to
record, then connect the call. When the call is completed, the eventUrl
you specify in the record action of the NCCO will receive a webhook
including the URL of the recording for download.
Example
Replace the following variables in the example code:
| Key | Description |
|---|---|
VONAGE_VIRTUAL_NUMBER | Your Vonage Number. E.g. |
VOICE_TO_NUMBER | The recipient number to call, e.g. |
Prerequisites
Write the code
Add the following to record-a-call.js:
const Express = require('express');
const bodyParser = require('body-parser');
const app = new Express();
app.use(bodyParser.json());
const onInboundCall = (request, response) => {
const ncco = [
{
action: 'record',
eventUrl: [`${request.protocol}://${request.get('host')}/webhooks/recordings`],
},
{
action: 'connect',
from: VONAGE_VIRTUAL_NUMBER,
endpoint: [
{
type: 'phone',
number: VOICE_TO_NUMBER,
},
],
},
];
response.json(ncco);
};
const onRecording = (request, response) => {
const recording_url = request.body.recording_url;
console.log(`Recording URL = ${recording_url}`);
response.status(204).send();
};
app
.get('/webhooks/answer', onInboundCall)
.post('/webhooks/recordings', onRecording);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});Run your code
Save this file to your machine and run it:
Prerequisites
Add the following to build.gradle:
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:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.voice with the package containing RecordCall:
Prerequisites
Add the following to build.gradle:
Write the code
Add the following to the main method of the RecordCall file:
/*
* Route to answer and connect incoming calls with recording.
*/
Route answerRoute = (req, res) -> {
String recordingUrl = String.format("%s://%s/webhooks/recordings", req.scheme(), req.host());
RecordAction record = RecordAction.builder().eventUrl(recordingUrl).build();
ConnectAction connect = ConnectAction.builder(PhoneEndpoint.builder(VOICE_TO_NUMBER).build())
.from(VONAGE_VIRTUAL_NUMBER).build();
res.type("application/json");
return new Ncco(record, connect).toJson();
};
/*
* Route which prints out the recording URL it is given to stdout.
*/
Route recordingRoute = (req, res) -> {
System.out.println(EventWebhook.fromJson(req.body()).getRecordingUrl());
res.status(204);
return "";
};
Spark.port(3000);
Spark.get("/webhooks/answer", answerRoute);
Spark.post("/webhooks/recordings", recordingRoute);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:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.voice with the package containing RecordCall:
Prerequisites
Prerequisites
Write the code
Add the following to index.php:
require 'vendor/autoload.php';
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
define('TO_NUMBER', getenv('TO_NUMBER'));
define('VONAGE_NUMBER', getenv('VONAGE_NUMBER'));
$app = new \Slim\App();
$app->get('/webhooks/answer', function (Request $request, Response $response) {
//Get our public URL for this route
$uri = $request->getUri();
$url = $uri->getScheme() . '://'.$uri->getHost() . ($uri->getPort() ? ':'.$uri->getPort() : '') . '/webhooks/recording';
$record = new \Vonage\Voice\NCCO\Action\Record();
$record->setEventWebhook(new \Vonage\Voice\Webhook($url));
$connect = new \Vonage\Voice\NCCO\Action\Connect(new \Vonage\Voice\Endpoint\Phone(TO_NUMBER));
$connect->setFrom(VONAGE_NUMBER);
$ncco = new \Vonage\Voice\NCCO\NCCO();
$ncco->addAction($connect);
$ncco->addAction($record);
return new JsonResponse($ncco);
});
$app->post('/webhooks/recording', function (Request $request, Response $response) {
/** @var \Vonage\Voice\Webhook\Record */
$recording = \Vonage\Voice\Webhook\Factory::createFromRequest($request);
error_log($recording->getRecordingUrl());
return $response->withStatus(204);
});
$app->run();Run your code
Save this file to your machine and run it:
Prerequisites
Run your code
Save this file to your machine and run it:
Prerequisites
Run your code
Save this file to your machine and run it:
Try it out
You will need to:
- Answer and record the call (this code snippet).
- Download the recording. See the Download a recording code snippet for how to do this.
Further Reading
- Call Recording - Recording audio input from a caller or recording the conversation between two callers.