3

I'm attempting to setup a simple voicemail system in twiml with this as my guide,

Desired Behaviour:

When a call arrives, connect to SIP. If no answer after 20 sec, play mp3, record message, email message.

<Response>
    <Dial action="/voicemail" timeout="20">
        <Sip>
            username@endpoint.sip.us1.twilio.com
        </Sip>
      </Dial>
  </Response>

with the voicemail function as follows

<Response>
    <Play>https://www.example.com/voicemail.mp3</Play>
    <Record transcribe="true" transcribeCallback="http://twimlets.com/voicemail?Email=somebody@somedomain.com" action="/hangup"/>
</Response>

Everything works fine, except that the caller is diverted to voicemail regardless of whether the call is picked up or not.

What am I missing that would provide the logic to hangup if the call is completed?

Can this be done purely in twiml since there is no conditional logic operators?

Thanks in advance for any help you can provide!

Kogenesis
  • 31
  • 2

1 Answers1

2

Twilio developer evangelist here.

There is no way to achieve this with pure TwiML, as you say there are no conditionals in TwiML (XML).

What you need to do is check within your /voicemail endpoint whether the call was "completed" or "answered". You would do so by checking the DialCallStatus parameter. If so, then you can hangup the call, otherwise you want to continue with voicemail.

You could do this using Node.js and a Twilio Function like so:

exports.handler = function(context, event, callback) {
    const twiml = new Twilio.twiml.VoiceResponse();
    if (event.DialCallStatus === 'completed' || event.DialCallStatus === 'answered') {
        twiml.hangup();
    } else {
        twiml.play("https://www.example.com/voicemail.mp3");
        twiml.record({
            transcribe: true,
            transcribeCallback: "http://twimlets.com/voicemail?Email=somebody@somedomain.com",
             action: "/hangup"
        });
    }
    callback(null, twiml);
};

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88