You're close, except you can't do anything after twiml.record()
but to return with callback(null, twiml);
So, what you could to do is:
- Create a TwiML Bin, so you can hang up the call when the recording ends.
Here is the code for the bin, it will just return an empty response, which in turn will hang up the call.
<?xml version="1.0" encoding="UTF-8"?>
<Response />
When you save the bin, you can copy the URL for the bin, you'll need it in your function. The URL looks something like https://handler.twilio.com/twiml/EHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- Change your function code to something like this (you can add more options you can find in the docs, I'll put the link at the end of this answer).
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
// twiml.say("Welcome... Please leave a message, and we will call you back.");
twiml.record({
maxLength: 20,
action: 'https://handler.twilio.com/twiml/EHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
recordingStatusCallback: 'https://your_webhook_url',
recordingStatusCallbackMethod: 'POST',
recordingStatusCallbackEvent: 'in-progress, completed, absent'
});
callback(null, twiml);
};
If you look at the code, you can see action
which points to the TwiML Bin you created, that is where Twilio's engine will go after the recording is done. If you omit the action
URL, when the record is done, Twilio will come back to this function, and you'll end up in a loop (you can try it if you want).
Next, because the recording might be done, but the recording file might not be available to access, you need to use recordingStatusCallback
, a URL on your server, a webhook, where Twilio will let you know. One of the request parameters will be RecordingUrl
, "The URL of the recorded audio." (docs: https://www.twilio.com/docs/voice/twiml/record#attributes-recording-status-callback-parameters)
Next, you set the method POST
or GET
you want Twilio to use when it hits your webhook.
Next, you tell for which events you want Twilio to hit your webhook.
You can read about all this in Twilio's docs here:
(https://www.twilio.com/docs/voice/twiml/record#attributes)
Note: If you think you can wait for some time before you use the recording URL, you can only provide the
action
with your webhook and handle the hang up on your side (respond with TwiML from your application), without the need for the TwiML bin and the
recordingStatusCallback... things
. The
RecordingUrl
is a parameter of the request to
action
too, except, like I said the file might not be available immediately.
How to get the "caller number":
Along with the RecordingUrl
you get CallSid
something like CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
and you can use it to get (making another request from your application) information about the call including the from
number with this code:
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);
client.calls('CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
.fetch()
.then(call => console.log(call.from));
(https://www.twilio.com/docs/voice/api/call?code-sample=code-fetch-a-call&code-language=Node.js&code-sdk-version=3.x)