In this code snippet, we take the well-known functionality of an interactive voice response system (IVR) and introduce a simple yet effective mechanism to make it almost infinitely flexible.
We're all familiar with the menu system that greets us on many of the calls we make to companies in order to route us to the proper agent. They are, perhaps, loved (by the companies using them for effective call direction) and hated (by those calling the companies) in equal measure.
By applying some good sense to their construction, they can be more palatable to callers. We have some ideas about how to build an IVR that won't send your customers running.
This is where the mechanism in this post comes in. We've created a framework that can be populated with the contents of a JSON file, thus making menu systems both flexible and scalable without having to go back to the code.
You will need a machine with Python installed, the SignalWire SDK, a SignalWire phone number (which you can purchase at any time in your dashboard), and optionally, Docker if you decide to run it in a container.
Methods and Endpoints
Endpoint: /get_menu Methods: GET OR POST
Endpoint: /get_voicemail Methods: GET OR POST
Requests generate a menu. get_menu will default to main menu if no menu is specified. The get_menu will look for dtmf entries to select an action for routing and moving along a menu tree.
Requests to get_voicemail produce a LaML Voice response to simulate an endpoint.
Set up Menus
Edit the menus.json file.
This example file has 3 menus. Each menu contains an index, which is equal to the key press on the menu, a verbiage, and an action. You can edit the verbiage, action, and key presses. You can also add as many menu options as you want.
Set up the Environment File
Copy from example.env and fill in your values, and save new file called .env.
Your file should look something like this:
Build and Run on Docker
You can use our pre-built image from Docker Hub. For Python:
docker pull signalwire/snippets-simple-dynamic-ivr:python
Or, build your own image:
docker build -t snippets-simple-dynamic-ivr
Run your image:
docker run --publish 5000:5000 --env-file .env snippets-simple-dynamic-ivr
The application will run on port 5000.
Build and Run Natively
Replace the environment variables, and from command line run python3 app.py