How to leave a message after the beep using Twilio’s Answering Machine Detection

This short technical article contains the key details that I had to dig up from Twilio’s API docs when we needed to record a message after the beep.

This article does not contain the code specifics, you can use any language or SDK’s available to use Twilio’s API. Its contains the key information you need to know to do this task.

By using twilio’s API and its answering machine detection mechanism we can do this very easily.

When making a call with your program to a number, you can set a parameter “AnsweredBy” to enable machine detection.

“AnsweredBy” parameter can be set to “Enable” or “DetectMessageEnd”

If set to “Enable”,

Twilio API answering machine detection - record a message after the beep

the events can be,

  • machine_start
  • human
  • fax
  • unknown

This helps in detecting at the start of the call whether it was human or machine which returned the call.

Twilio then sends a request to your set URL/ application and you can send a recording based on the detection, the parameters sent to your application by Twilio look something like this,

But if you want to leave a message after the beep you will have to set it to “DetectMessageEnd” as shown below,

AnsweredBy: "DetectMessageEnd"

If you set it to “DetectMessageEnd” the events generated are,

  • machine_end_beep (this is what you need)
  • machine_end_silence
  • machine_end_other
  • human
  • fax
  • unknown.

So the event you are looking for is “machine_end_beep” which as it reads means the event that occurs when the voicemail beep is ended and now you can start playing your message. This is extremely useful when sending out (bulk) voice messages and getting hit by a voicemail. Instead of playing the recording as soon as the call is picked you can play your message when the voicemail recording has finished by doing your logic on this event.

You can see the event in request sent by Twilio as shown above in the screenshot and respond with a recording URL to play like this,

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Play>https://YOUR_WEBSITE_URL/YOUR_RECORDING_TO_PLAY.mp3</Play>
</Response>

As easy as that.

If you want to learn more about how it works have a look at API documentation here https://www.twilio.com/docs/voice/answering-machine-detection

If you have more questions write a comment below or reach out to me at https://peham.dev/#contact

Leave a Reply

%d bloggers like this: