<

Getting Started with Voice Alerts & Broadcasting

In this tutorial we will see how we can broadcast a message to multiple phone numbers at the same time. This is useful in case you want to broadcast the same information to a group of people.

For the purpose of this tutorial we will take the case of a voice notification that needs to be sent to a group of people. This can be done using Plivo bulk calls.

Note: In order to use bulk calls you need to be in a ‘Standard’ Plivo account. Your account is automatically upgraded to a standard account as soon as you recharge your account for the first time.

Broadcasting to multiple numbers

Broadcasting a message to multiple numbers consists of the following two steps.

  1. Initiating a call to all the intended numbers. This can be done in one step as explained later.
  2. Providing an XML with the message that needs to be played.

Let us look at each of these steps in detail.

Initiating calls to all intended numbers

Using Plivo API, you can initiate multiple calls to different numbers by using the ‘<’ operator in the ‘to’ field. For example in case you want to call the numbers 12345678, 23456789, 34567890, 45678901, the ‘to’ field should be provided as below.

'to': '12345678<23456789<34567890<45678901'

The code snippet for initiating the calls are as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def send_otp():
    url = 'https://api.plivo.com/v1/Account/MAM2I4NGU1ODM2NDM1M2/Call/'
    answer_url = 'http://c3bb61b6.ngrok.io/return_xml/'
    values = {
        'from': '56560',
        'to': '12345678<23456789<34567890<45678901',
        'answer_url': answer_url,
        'answer_method': 'GET'
    }
    headers = {'Content-type': 'application/json'}
    req = requests.post(
        url,
        data=dumps(values),
        auth=HTTPBasicAuth(username, password),
        headers=headers)
    return Response(str(req.text))

Playing the message once the calls are answered

Once the above calls are answered, Plivo servers will invoke the answer_url that is specified will initiating the call. The answer_url is expected to return a valid Plivo XML to speak the desired message. In our case the XML should look at follows:

1
<Response><Speak>Message goes here</Speak></Response>

We can serve this XML using the code snippet below. For more information on serving XML refer to our tutorial on serving XML from a server.

1
2
3
def return_xml():
    resp = '<Response><Speak>Message goes here</Speak></Response>'
    return Response(resp, mimetype='text/xml')

You can find the full source code of this example here.

Once this XML is retrieved, the message will be played to all the customers to whom we initiated the call in the first step.