Search code examples
node.jstwiliotwilio-apitwilio-functions

How to delete the last (most recent) recording made by caller in Twilio in Node.JS


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!


Solution

  • 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');
    
    
    };