0
votes

I'm writing a Twilio Function that records a voicemail by the caller, then sends an SMS with the URL of the audio recording to a constant phone number. It works as expected when the caller ends the voicemail recording by pressing any of the onFinishKey keys. However, if the user ends the voicemail recording by hanging up, the code still reaches the console.log below but the text message never gets sent. I am guessing that the TwiML response is being ignored since the caller has hung up already. I tried having Record's action point to a TwiML bin but TwiML bin doesn't seem to support SMS/Message commands as response to a voice call (https://support.twilio.com/hc/en-us/articles/360017437774-Combining-Voice-SMS-and-Fax-TwiML-in-the-Same-Response). I also tried having Record's action point to a different Function that just sends the SMS but that led to same outcome when the SMS code was in the same Function.

Pseudocode:

exports.handler = function(context, event, callback) {
    let twiml = new Twilio.twiml.VoiceResponse();
    if (!event.RecordingUrl) {
      twiml.say('Starting to record...');
      twiml.record();
    } else {
      twiml.sms(`${event.RecordingUrl}`, {to: '+1-222-333-4444'});
      console.log('Prepared TwiML to send text message!');
    }
    callback(null, twiml);
}

Is there a way around this?

1

1 Answers

0
votes

You should use the messages resource to send the SMS instead of TwiML.

exports.handler = function(context, event, callback) {
    const twilioClient = context.getTwilioClient();
    
twilioClient.messages
  .create({
    body: 'Hello World',
    to: '+15555555555',
    from: '+1802123456 ',
  }).then(message =>  {
        console.log(message.sid);
        callback();
  })
    .catch(error => {
        console.log(error);
           callback("error");
    });
};