3

I'm trying to write a simple Twilio setup that will make an outbound call, record a user's voice message and immediately replay it back to him. I have created two functions: startcall and msgin. StartCall calls the user and specifies MsgIn in the call's url parameter. MsgIn then has two main modes of operation: initially when there is no recording attached it returns a TwiML response starting the recording, and later when called as a webhook with the appropriate parameter it plays the recording back and hangs up. At least that's what it should do.

According to my understanding of the documentation, I have to attach a webhook to recordingStatusCallback, as the recording may not yet be available when action webhook is invoked. However, while the function console indicates that both webhooks are executed, the call log only shows one follow-up invocation of msgin with a null value for event.RecordingStatus (which corresponds to action), and the call indeed hangs up without playing the recording back. What am I missing here?

// this function's path is /msgin

exports.handler = function(context, event, callback) {
    if (!event.RecordingStatus && !event.RecordingUrl) {
        let twiml = new Twilio.twiml.VoiceResponse();
        console.log("Initial MsgIn");
        twiml.say({ voice: 'man', language: 'en-us' }, 'Leave your message');
        twiml.record({
            playBeep: false,
            transcribe: false,
            trim: "trim-silence",
            timeout: 1,
            recordingStatusCallback: "/msgin",
            recordingStatusCallbackEvent: "completed",
            action: "/msgin"
        });
        console.log("Recording started");
        callback(null, twiml);
    }
    else if (event.RecordingStatus == "completed") {
        let twiml = new Twilio.twiml.VoiceResponse();
        console.log("Supposedly callback");
        twiml.say({ voice: 'man', language: 'en-us' }, 'You said');
        twiml.play(event.RecordingUrl);
        twiml.hangup();
        callback(null, twiml);
    }
    else {
        console.log("Supposedly action");
        callback(null, "");
    }
};
dpq
  • 9,028
  • 10
  • 49
  • 69
  • Discovered the following Q&A: https://stackoverflow.com/questions/43599336/recording-multiple-user-answers-in-twilio-call. This helps to understand why my callback processing doesn't work, but I don't understand how to use it if I want to keep the delay between recording and replay as low as possible, and I don't know how long the utterance may last. – dpq Jan 21 '19 at 12:10
  • One idea is to launch another function which relies in memcached to set/get recording status (set is invoked by callback, get is invoked by action in a 50-msec step loop). – dpq Jan 21 '19 at 12:11

1 Answers1

0

I've followed the instructions in the accepted answer here: Recording multiple user answers in Twilio call to solve this problem. There is a substantial delay before the audio recording becomes available, though.

dpq
  • 9,028
  • 10
  • 49
  • 69
  • Glad you found that answer and that it helped. There is no guarantee on how long a recording will take to become available though I'm afraid. It might improve perceived performance to a caller if you give them the option to hear again afterwards and require a keypress. That would give the recording chance to complete while the user interacted with the call. – philnash Jan 23 '19 at 00:47
  • Good idea. Or just to play some pre-recorded text along the lines of "Replaying your message now". I've also noticed that call redirection make take up to 2-3 seconds; in case this is normal and not some kind of misconfiguration on my part such an approach would also take care of this delay, too. – dpq Jan 23 '19 at 08:32
  • Redirection shouldn't take that long. How is the response time on the URL the call is redirected to? – philnash Jan 23 '19 at 08:58