Search code examples
twiliotwilio-twiml

How to set voicemail using Twiml Bins?


I am doing a quick POC using Twiml Bins, I am trying to connect 2 calls and if the call is unanswered by the recipient it should go to their voicemail.

Use case:

  1. User A calls my twilio no.
  2. Twilio places A on hold and tries to call B.
  3. If B answers, B is asked to accept or deny the call. By pressing 1 or 2 (or hang up).
  4. If accepted, call connects and A and B talk.
  5. If rejected, A is able to leave a voicemail to B on his cell.
  6. If B does not answer, A is able to leave a voicemail to B on his cell.

I am stuck at 5 and 6. Unable to have a voicemail sent to B if either call is rejected or unanswered. The nesting of Twiml tags is getting little confusing for me. Here's what I tried so far: PutAOnholdAndCallB:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>Thank you for calling please wait while I connect you.</Say>
  <Dial timeout="15" action="Callhangup">
        <Number url="AcceptorDeny">UserBPhoneNo
        </Number>
    </Dial>
</Response>

AcceptorDeny:

    <?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Gather numDigits="1" timeout="20" action="DoNothing">
    <Say>You have a call, press 1 to answer or hang-up for it to go to voicemail.</Say><Pause length="3"/><Say>Connecting</Say>
  </Gather>
  <Hangup/>
</Response>

Callhangup:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say>
        You have reached the voicemail.
        Please leave a message at the beep. 
        Press the star key when finished. 
    </Say>

    <Record action="DoNothing"
        method="GET" 
        maxLength="20"
        finishOnKey="*"
    />
    <Hangup />
</Response>

DoNothing:

<?xml version="1.0" encoding="UTF-8"?>
<Response />

Solution

  • Your challenge is that TwiML Bin's do not offer the level of conditional logic you need to analyze the results of the Dial action URL you define. You need to evaluate the value of DialCallStatus, to properly direct the unanswered call (or calls where the dialed party doesn't press a number to answer the call).

    In those unanswered cases, which are where you want to send the call to your TwiML Bin voicemail logic, Callhangup, you check to see if DialCallStatus (of the Dial action URL) = no-answer otherwise you Hangup or return no more TwiML (since the dialed party pressed a key to answer the call, so no need to present voicemail to the caller).

    The logic is documented in this Twilio Function, Implement voicemail.

    I would suggest Twilio Studio to handle this however Studio doesn't currently support the Number URL feature you used for the whisper the option to the dialed party the option of accepting the call.

    If you did use Studio, you would need to look into the TwiML Redirect Widget to add that whisper functionality into your Studio Flow. I saw a blog doing what I wrote above using Studio (without whisper) last week here, Using Twilio To Make A Call Forwarding And Voice Messaging System.