Reputation: 1
Hi I'm trying to set up voice mail for incoming calls through Twilio Functions and sending the call information with the recorded URL to external webhook appreciate your suggestions on how to implement it
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
//twiml.say("Welcome We are not available at this moment. Please leave a message, and we will call you back.");
twiml.record({ maxLength: 20,});
twiml.hangup();
callback(null, twiml);
};
Upvotes: 0
Views: 558
Reputation: 11702
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:
<?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
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)
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.
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));
Upvotes: 1