Search code examples
twiliotwilio-twimltwilio-php

How can I handle reaching voicemail using Twilio's <dial> verb


I know that on making a call Twilio can detect an answering machine, and react differently.

However if I use the <dial> verb, there's no obvious place to add this feature, even though it's essentially the same thing.

My intended flow is:

  • Customer enters their phone number
  • Twilio calls Customer and plays a voice message
  • Twilio dials an agent number, likely a mobile
  • If the Agent picks up, connect the customer to the agent
  • If the Agent busies the call or does not answer, call will likely go to Agent's voicemail.
    • Terminate call to Agent
    • Record voicemail from Customer
    • Email voicemail to Agent

Solution

  • From the official docs on the <Dial> verb (emphasis mine):

    This is the simplest case for Dial. Twilio will dial 415-123-4567. If someone answers, Twilio will connect the caller to the called party. If the caller hangs up, the Twilio session ends. If the line is busy, if there is no answer, or if the called party hangs up, <Dial> exits and the <Say> verb is executed for the caller before the call flow ends.

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- page located at http://example.com/simple_dial.xml -->
    <Response>
        <Dial>415-123-4567</Dial>
        <Say>Goodbye</Say>
    </Response>
    

    Placing a <Record> verb after the <Say> verb sounds like what you are looking for. You can change the timeout from the default value of 30s like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <Response>
        <Dial timeout="9001">415-123-4567</Dial>
        <Say>Please leave a message</Say>
        <Record action="/process_new_voicemail" />
    </Response>