Record a message
A code snippet that shows how to record a conversation. Answer an incoming
call and return an NCCO that includes a record action. When the call is
complete, a webhook is sent to the eventUrl you specify. The webhook includes
the URL of the recording.
Example
Prerequisites
npm install express body-parserWrite the code
Add the following to record-a-message.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: 'talk',
text: 'Please leave a message after the tone, then press #. We will get back to you as soon as we can.',
},
{
action: 'record',
endOnKey: '#',
beepStart: 'true',
endOnSilence: '3',
eventUrl: [`${request.protocol}://${request.get('host')}/webhooks/recordings`],
},
{
action: 'talk',
text: 'Thank you for your message. Goodbye.',
},
];
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:
Write the code
Add the following to the main method of the RecordMessage class:
embeddedServer(Netty, port = 8000) {
routing {
get("/webhooks/answer") {
call.response.header("Content-Type", "application/json")
call.respond(
Ncco(
talkAction("Please leave a message after the tone, then press #."),
recordAction {
eventUrl(call.request.path().replace("answer", "recordings"))
beepStart(true)
endOnSilence(3)
endOnKey('#')
},
talkAction("Thank you for your message. Goodbye!")
).toJson()
)
}
post("/webhooks/recordings") {
val event = EventWebhook.fromJson(call.receive())
println("Recording URL: ${event.recordingUrl}")
call.respond(204)
}
}
}.start(wait = true)Run your code
We can use the アプリケーション 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 RecordMessage:
Prerequisites
Add the following to build.gradle:
Write the code
Add the following to the main method of the RecordMessage class:
/*
* 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());
TalkAction intro = TalkAction.builder(
"Please leave a message after the tone, then press #. We will get back to you as soon as we can.").build();
RecordAction record = RecordAction.builder()
.eventUrl(recordingUrl)
.endOnSilence(3)
.endOnKey('#')
.beepStart(true)
.build();
TalkAction outro = TalkAction.builder("Thank you for your message. Goodbye").build();
res.type("application/json");
return new Ncco(intro, record, outro).toJson();
};
/*
* Route which prints out the recording URL it is given to stdout.
*/
Route recordingRoute = (req, res) -> {
EventWebhook recordEvent = EventWebhook.fromJson(req.body());
System.out.println(recordEvent.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 アプリケーション 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 RecordMessage:
Prerequisites
Install-Package VonagePrerequisites
composer require slim/slim:^3.8 vonage/clientRun your code
Save this file to your machine and run it:
Prerequisites
pip install vonage python-dotenv fastapi[standard]Run your code
Save this file to your machine and run it:
Prerequisites
gem install sinatra sinatra-contribWrite the code
Add the following to record-a-message.rb:
require 'sinatra'
require 'sinatra/multi_route'
require 'json'
before do
content_type :json
end
helpers do
def parsed_body
JSON.parse(request.body.read)
end
end
route :get, :post, '/webhooks/answer' do
[
{
"action": "talk",
"text": "Please leave a message after the tone, then press #. We will get back to you as soon as we can"
},
{
"action": "record",
"eventUrl": ["#{request.base_url}/webhooks/recordings"],
"endOnSilence": "3",
"endOnKey": "#",
"beepStart": "true"
},
{
"action": "talk",
"text": "Thank you for your message. Goodbye"
}
].to_json
end
route :get, :post, '/webhooks/recordings' do
recording_url = params['recording_url'] || parsed_body['recording_url']
puts "Recording URL = #{recording_url}"
halt 204
end
set :port, 3000Run your code
Save this file to your machine and run it:
Try it out
You will need to:
- Record a message by dialling your Vonage Number, and leaving your message after the tone (this code snippet).
- Download the recording. See the Download a recording code snippet for how to do this.
Further Reading
- Voicemail - Learn how to record audio from inbound calls using .NET and the Vonage Voice API.