3

I have a function which lets you record and then delete your recording if you are not satisfied with it. You can delete your last recording when you press *.

My code for recording is:

exports.handler = function(context, event, callback) {
    const twiml = new Twilio.twiml.VoiceResponse();
    twiml.say('Welcome! Please record your announcement after the beep!');
    twiml.say('After your recording, hang up if you are satisfied or press star to delete the newest recording.');
    twiml.record({
      finishOnKey: '*',
      recordingStatusCallback: '/delete',
      action: '/delete'
    });

 callback(null, twiml);
};

The code for deleting the last recording (/delete):

exports.handler = function(context, event, callback) {

  const twiml = new Twilio.twiml.VoiceResponse();
  const id = event.RecordingSid;
  console.log(id);
  console.log(typeof id);
  const accountSid = 'AC1775ae53d952710bb1b4e47ea19c009c';
  const authToken = {my_token};
  const client = require('twilio')(accountSid, authToken);

client.recordings(id)
      .remove(function(err, data) {
            if (err) {
                twiml.say("there is an error");
                console.log(err.status);
                throw err.message;
            } else {
                console.log("deleted successfully.");
                twiml.say("deleted successfully");
            }
      })
     .then(recording => console.log(recording.sid))
     .done();
twiml.say('Your announcement is deleted');

  callback(null, twiml);
};

Right now, I am getting the correct id and no errors but the recording is still there when I check the recordings log. Why is that? Do I need to give it some time to let it delete?
The log from console.log(recording.sid) is also not showing up and I am not sure why it's not executing the entire block of code which is supposed to delete the recording.
Thanks in advance!

techCells
  • 67
  • 1
  • 9
  • You say that the log of the recording sid is not showing up, have you tried catching a potential error from that call? It may be that the recording is in fact not ready for deletion at this point and you need to mark the recording to be deleted and perform the action later instead. – philnash May 30 '18 at 04:36
  • Unless I need to wait a certain time to delete it, I don't think that is an issue. I refreshed the recordings log right after I pressed * and the audio recording indicated as "completed" during the entire execution of "/delete". If I do need to wait, how do I mark an audio so that it can be deleted later? – techCells May 30 '18 at 05:41
  • If you need to wait then I recommend you wait until the recordingStatusCallback makes a request. You could mark the audio to be deleted based on it's URL and the call SID. You'd have to store that somehow on your server and that depends on your implementation. – philnash May 30 '18 at 05:53
  • Hi, the recording is ready by the time I try to execute the block of code that deletes the recording. But it still doesn't execute it - almost like it doesn't exist. Is there a different way to delete the recording? – techCells May 30 '18 at 18:05
  • I would posit that if you execute the promise that makes the call to delete the recording but don't try to catch any errors, then you don't know if the code is executing correctly. I recommend you add error handling to your code (even just logging the response) and let me know what you find out. – philnash May 31 '18 at 09:44
  • I have added the error message like above bit the extremely eerie thing is that neither "deleted successfully" or "there is an error" is said. The entire remove code is not executed at all. The console logs the right id and then twiml says "Your announcement is deleted" and that's it. – techCells May 31 '18 at 14:10
  • I figured it out! I posted the code in the answer! – techCells May 31 '18 at 18:58

2 Answers2

1

Twilio developer evangelist here.

When you set the recordingStatusCallback to /delete as well as the action of your <Gather>, then Twilio will attempt to make two calls to /delete; once when the recording is complete and once when the user responds to the <Gather>.

The recordingStatusCallback is intended to do work asynchronously to the call itself, so I am going to concentrate on the action instead.

When the recording is finished (user presses *), Twilio will make a request to the <Record> action attribute. In that request you will receive, as part of the body of the request, the RecordingURL (even if the recording status is not yet complete). You should pass this RecordingURL through to the results of the <Gather> (or store it in the session) and use it to either perform the DELETE request or extract the Recording SID for use with the Twilio Node module if you need to delete the recording.

Does that help at all?

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Hi! Thank you! I still have the a problem which is that the recording is not being deleted from the logs. I have updated the question to reflect the new situation! Could you also explain the necessity of recordingStatusCallback since I am getting the id if I remove that parameter. Thank you for your guidance! – techCells May 29 '18 at 18:15
1

I figured it out! This is the correct code for /delete:
The main thing I changed was move my callback inside the remove(function(...)) so the function only returns after the HTTP request is done.

exports.handler = function(context, event, callback) {


  const twiml = new Twilio.twiml.VoiceResponse();
  const id = event.RecordingSid;
  var client = context.getTwilioClient();
  console.log(client);
  const URL = event.RecordingUrl;

    client.recordings(id)
    .remove(function(err, data) {
                if (err) {
                    twiml.say("there is an error");
                    console.log(err.status);
                    throw err.message;
                } else {
                    console.log("deleted successfully.");
                    twiml.say("deleted successfully");
                    callback(null, twiml);
                }
    })
    .then(recording => console.log(recording.sid))
    .done();
    twiml.say('Your announcement is deleted');


};
techCells
  • 67
  • 1
  • 9
  • Ah! I should have seen that. Twilio Functions will cease once you callback, so it was cancelling the delete operation. Glad you got it solved. – philnash Jun 04 '18 at 20:35