Code Snippet #1 app.py

import json
from flask import Flask
from flask import request

app = Flask(__name__)

@app.route("/incomingCalls", methods=['POST'])
def home():
  print(request.form)
  response = {
    "ivr": "https://46elks.com/static/sound/ivr-menu.mp3",
    "digits": 1,
    "next":"https://yourapp.example/elks/ivr"
    }
  return json.dumps(response)

@app.route("/ivr", methods=['POST'])
def calls_in():
  print(request.form)
  if request.form['result'] == "1":
    voice_start = {
    'play': "https://www.46elks.com/static/sound/voiceinfo.mp3"
    }
      
  if request.form['result'] == "2":
    voice_start = {
    'play': "https://www.46elks.com/static/sound/smsinfo.mp3"
    }
  return json.dumps(voice_start)

if __name__ == "__main__":
  app.run(host='0.0.0.0', port=5501)

Remember to initiate NGROK to forward traffic to your local server

You can do this with ./ngrok http 5501. Find a quick-start guide for NGROK here.

Quicklinks for this tutorial

Setting up Flask

For a more comprehensive guide to setting up your first Flask project, we recommend following this tutorial --> Flask by Example – Project Setup. This example uses python3.

If your still with us here, then follow these steps. Create a new working directory/folder and enter it:

mkdir flask-ivr && cd flask-ivr
Set up a virtual environment to use for our application:
python -m venv env
source env/bin/activate
Next, we're going to create a file called flask-ivr-app.py and add it to your “flask-ivr” folder:
touch app.py 
Next, install flask:
python -m pip install Flask==1.1.1

The code for app.py

Open up your favorite editor and add the example code to app.py

import json
from flask import Flask
from flask import request

app = Flask(__name__)

@app.route("/incomingCalls", methods=['POST'])
def home():
  print(request.form)
  response = {
    "ivr": "https://46elks.com/static/sound/ivr-menu.mp3",
    "digits": 1,
    "next":"https://00165bbo.ngrok.io/ivr"
    }
  return json.dumps(response)

@app.route("/ivr", methods=['POST'])
def calls_in():
  print(request.form)
  if request.form['result'] == "1":
    voice_start = {
    'play': "https://www.46elks.com/static/sound/voiceinfo.mp3"
    }
  elif request.form['result'] == "2":
    voice_start = {
    'play': "https://www.46elks.com/static/sound/smsinfo.mp3",
    }
  return json.dumps(voice_start)

if __name__ == "__main__":
  app.run(host='0.0.0.0', port=5501)

Configuring Flask endpoints

In the first section of code you will see this line of code:

"next":"https://00165bbo.ngrok.io/ivr"
The "next" parameter is a url that 46elks will post towards with the details of which buttons are pressed. The "next" parameter should be used anytime you want to continue the call. To put it a different way, anytime you offer callers an option of which you would like to know the outcome, you should provide a "next" url.

In this case our Flask application will be running on an NGROK server and the location where we would like to receive information is at "/ivr" where we have the next set of instructions.

Configuring your 46elks virtual phone number

We need to configure our 46elks virtual phone number so that our flask app is triggered whenever we receive a phone call. To do this head to your dashboard. In this example we are using NGROK, so we have added details of our NGROK server with /incomingCalls on the end.

Where to get your voice enabled virtual number in your account dashboard

Now we are ready to test our app

You should now be able to test your application.
  1. Initiate your server or NGROK tunnel if you have not already.
  2. Initiate app.py to start your flask application
  3. Call your 46elks number to test

What success looks like

Below are two images. The first shows the terminal running the app.py server and the second is the NGROK server.

The terminal print out of the IVR call data

You can see in the image the different types of data that 46elks posts towards each Flask endpoint and how that data changes. In the example the caller selects the first option by pressing "1" and this is the result we obtain from the the API.

The terminal print out of the IVR call data

You can see in this image the two posts 46elks has made to the two Flask endpoints that we setup in our code example.

Need some more help or have some suggestions?

We would love to hear what you have to say. You can send us an email to help@46elks.com.

Written 2020-04-20 by Ben Chatwin.