My twilio code is:
const express = require('express');
const VoiceResponse = require('twilio').twiml.VoiceResponse;
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/health', (req, res) => {
res.send('ok')
})
// Returns TwiML which prompts the caller to record a message
app.post('/record', (request, response) => {
// Use the Twilio Node.js SDK to build an XML response
const twiml = new VoiceResponse();
twiml.say("Hi!");
// Use <Record> to record the caller's message
twiml.record();
console.log(twiml.toString())
response.send(twiml.toString());
});
// Create an HTTP server and listen for requests on port 3000
app.listen(PORT);
But I want to know the recording ID so I can access the raw file programatically. How would I do that?
To get the recording ID, (RecordingSid), you need to tell Twilio an action
URL, with something like this:
twiml.record({
action: '/finished'
});
You can read more here: (https://www.twilio.com/docs/voice/twiml/record#attributes). Also, read about the recordingStatusCallback
URL attribute, maybe that's something you need too.
Then, you need to parse the body of this second request Twilio will make to your app.
You can read more about this here: (https://www.twilio.com/blog/2016/07/how-to-receive-a-post-request-in-node-js.html).
For this you can use body-parser
, which you can get with npm install body-parser
.
The recording ID will be part of the parameters under body.RecordingSid
.
Anyway, here is a rough modification of your code, to get started:
// npm install express body-parser
const express = require('express');
const bodyParser = require('body-parser');
const VoiceResponse = require('twilio').twiml.VoiceResponse;
const app = express();
// Tell express to use the body-parser middleware and to not parse extended bodies
app.use(bodyParser.urlencoded({
extended: false
}))
const PORT = process.env.PORT || 3000;
app.get('/health', (req, res) => {
res.send('ok')
})
// Returns TwiML which prompts the caller to record a message
app.post('/record', (request, response) => {
// Use the Twilio Node.js SDK to build an XML response
const twiml = new VoiceResponse();
twiml.say("Hi!");
// Use <Record> to record the caller's message
twiml.record({
action: '/finished'
});
console.log(twiml.toString())
response.send(twiml.toString());
});
app.post('/finished', function (req, res) {
const body = req.body;
res.set('Content-Type', 'text/plain');
res.send(``);
console.log(body);
console.log(body.RecordingSid);
});
// Create an HTTP server and listen for requests on port 3000
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})