You can enable answering machine or voicemail detection by including the machine_detection parameter when you make an outbound call using the Call API. Plivo runs machine detection in the background. When it finds a machine, Plivo makes an HTTP request, sending a Machine parameter with the value true to the machine_detection_url you specify. Your application can then make a decision based on this parameter. Chances are you’ll use the Transfer API to change the flow of the call. See the Asynchronous machine detection page of our API reference documentation for details on the parameters sent.

Getting started

  1. Sign up for a free Plivo trial account.
  2. Check out our server SDKs page and install the SDK for the programming language you want to use.
  3. Buy a Plivo phone number (optional). You need a Plivo phone number to receive calls. You can buy a Plivo phone number in more than 20 countries by visiting Phone Numbers > Buy Numbers in the Plivo console. Check the Voice API coverage page to see the supported countries.
  4. Use a web hosting service to host your web application. Many inexpensive cloud hosting providers cost just a few dollars a month. Follow the instructions of your hosting provider to host your web application.

Implementation

  1. Copy the code below into a text file and save it.
  2. Replace the placeholders <auth_id> and <auth_token> with your account’s Auth ID and Auth Token, which you can find on the overview page of the Plivo console.
  3. Add your from (source) phone number, which will show up as your caller ID. All phone numbers should include country code, area code, and phone number without spaces or dashes (e.g., 14153336666).
  4. Add your to (destination) phone number. To place bulk calls to more than one number, separate the destination phone numbers with the < character (e.g., 14156667777<14157778888<14158889999).
Note: If you’re using a trial account, your destination number needs to be verified with Plivo. Phone numbers can be verified at the Phone Numbers > Sandbox Numbers page of the console.
  1. Edit the answer_url field and supply a URL for Plivo to request when the call is answered. This URL supplies XML code that Plivo what to do with the call.
  2. Give the answer_method field a value of either GET or POST.
  3. Edit the machine_detection_url field and supply a URL for Plivo to request if an answering machine or voicemail is detected on the call.
  4. Give the machine_detection_method field a value of either GET or POST.

Code

import plivo, plivoxml
from flask import Flask

auth_id = "<auth_id>"
auth_token = "<auth_token>"
p = plivo.RestAPI(auth_id, auth_token)

# Machine detection using Call API

params = {
    'to': '<destination_number>', # The phone numer to which the call will be placed
    'from': '<caller_id>', # The phone number to use as the caller id
    # The URL invoked by Plivo when the outbound call is answered
'answer_url': "https://<yourdomain>.com/detect/",
'answer_method': "GET", # Method to request the answer_url
'machine_detection': "true", # Used to detect if the call has been answered by a machine. Valid values are "true" and "hangup".
'machine_detection_time': "10000", # Time allotted to analyze if the call has been answered by a machine. The default value is 5000 ms.
'machine_detection_url': "https://<yourdomain>.com/machine_detection/", # A URL where machine detection parameters will be sent by Plivo.
'machine_detection_method': "GET" # Method used to invoke machine_detection_url
}

# Make an outbound call
response = p.make_call(params)
print str(response)


# As soon as the voicemail finishes and there is a silence for minSilence
# milliseconds, the next element in the XML is processed, without waiting for
# the whole period of length seconds to pass

@app.route('/detect/', methods=['GET','POST'])
def detect():
    try:
        r = plivoxml.Response()
        params = {
            'length': "1000", # Time to wait in seconds
            'silence' : "true", # When silence is set to true, if no sound is detected for minSilence milliseconds, end the wait and continue to the next element in the XML immediately
            'minSilence' : "3000" # Only used when silence is set to true. The minimum length in milliseconds of silence that needs to be present to qualify as silence
        }
        r.addWait(**params)
        r.addSpeak("Hello Voicemail")
        print r.to_xml()
        return Response(str(r), mimetype='text/xml')
    except Exception as e:
        print '\n'.join(traceback.format_exc().splitlines())

# Machine Detection URL example

@app.route('/machine_detection/',methods=['POST', 'GET'])
def machine_detection():
    from_number = request.args.get('From') # The number calling
    to_number = request.args.get('To') # The number being called
    machine = request.args.get('Machine') # This parameter will be true if a machine has been detected on the call.
    call_uuid = request.args.get('CallUUID') # The ID of the call.
    event = request.args.get('Event') # The event of the notification. When requesting the machine_detection_url, this parameter will always have the value 'MachineDetection'
    call_status = request.args.get('CallStatus') # The status of the call. This will hold the value of in-progress.

    print "From: %s " % (from_number)
    print "To: %s " % (to_number)
    print "Machine: %s " % (machine)
    print "Call UUID: %s " % (call_uuid)
    print "Event: %s " % (event)
    print "Call Status: %s " % (call_status)
    return "OK"

Sample Response

Make Outbound call

(201, {
        u'message': u'call fired',
        u'request_uuid': u'a52a7ae0-0551-462c-9cf0-1f79f79737c8',
        u'api_id': u'45305402-959f-11e4-b932-22000ac50fac'
    }
)

XML returned by the answer_url

<Response>
    <Wait minSilence="3000" silence="true" length="10"/>
    <Speak>Hello Voicemail</Speak>
</Response>

Machine Detection URL output

From : <caller_id>
To : <destination_number>
Machine : true
Call UUID : 45704ba2-959f-11e4-802f-e9b058eeb9e5
Event : MachineDetection
Call Status : in-progress