As a backend PHP CodeIgniter developer, I have built Interactive Voice Response (IVR) systems to manage call flows for businesses. Before I started building IVR systems, I got frustrated because I couldn’t find enough resources on Google on how to build with CodeIgniter.

I would give a quick guide on how to build a basic IVR system with Twilio, Ngrok, and CodeIgniter.  Twilio Programmable Voice API makes it easy for anyone to build an IVR system in any programming language or framework capable of interacting with REST API. This tutorial is focused on how to build with CodeIgniter.

Key terms:

IVR: Interactive voice response system is an automated system that interacts and receives inputs from callers via a keypad and also routes calls to appropriate recipients when necessary.

Codeigniter: is an open-source PHP web development framework.

Twilio: is a cloud communications platform for building SMS, Voice & Messaging applications using its web service API.

Ngrok: exposes a localhost server to the internet.

Composer: is a package manager for PHP.

The code for this project can be found at => https://github.com/learningdollars/adeoluwaakinsanya-IVR-with-Twilio-CodeIgniter-and-Ngrok

Technical Requirements (prerequisites)

Let’s run down through the Prerequisites and tools required to build our system:

  • Twilio Account (phone number and Voice API)
  • Linux OS (feel free to use any OS)
  • Git
  • Composer
  • PHP
  • Ngrok.

Part 1 – Install CodeIgniter

If you don’t already have PHP, Git, and Composer installed, open up a terminal or command-line interface on your computer and install by typing:

$ sudo apt-get install php git composer -y

CodeIgniter can be installed via Composer or cloned from GitHub. It is assumed that you know the basics of CodeIgniter. You can read more about it here. Navigate to your project directory and run either of the commands below in your terminal;

Installing CodeIgniter via Composer:

$ composer global require CodeIgniter

Installing CodeIgniter via GitHub:

$ git clone https://github.com/bcit-ci/CodeIgniter.git

After installing CodeIgniter, create a local web server by running;

$ php -S localhost:8000

Our local server should be running and can be accessed via http://localhost:8000

Part 2 – Install Ngrok

Ngrok would be used to make our localhost accessible over the internet for quick prototyping and testing.

Install Ngrok by running the command below in your terminal:

$ snap install ngrok

To generate a Ngrok hosted public address for your web application, navigate to your project directory and run;

$ ngrok http 8000

This public address or hostname would be used to configure the webhook URL for your Twilio phone number.

Part 3 – Building the IVR System

Twilio sends an HTTP request to your webhook URL configured within your Twilio number when a call comes in and fetches TwiML instructions from your app.

For our basic IVR system, we would build a system that uses the TwiML command, <Say> to say the welcome message, <Gather> to gather input from the caller so you can decide what to do next, <Dial> to transfer the caller to another number, <Redirect> to transfer the call to another URL and <Hangup> to hang up the call.

Navigate to application/config/config.php in your CodeIgniter project directory and update $config[‘base_url’] to your Ngrok public address.

Create a new controller, Flow.php under application/controllers and add the code below. See example

Flow Controller – Flow.php:

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Flow extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
    }

    /**
     * Outputs welcome message to webhook using TwiML
     */
    public function start()
    {
        /*After saying the welcome message and retrieving the caller’s input, Twilio will send the Digits parameter through an HTTP request to the action URL.*/
        header('Content-type: application/xml');
        echo '<?xml version="1.0" encoding="UTF-8"?>
		<Response>
		<Gather action="' . base_url() . 'index.php/flow/process" numDigits="1">
		<Say>Press one to talk to a sales agent or Press two to hang up now</Say>
		</Gather>
		<Say>We didn\'t receive any input. Goodbye!</Say>
		</Response>';
    }

    /**
     * Respond to User's Input using TwiML
     *
     * @var int $input Should contain an integer
     */
    public function process()
    {
        header('Content-type: application/xml');
        $input = $_REQUEST["Digits"];
        if ($input == 1) {
            //if caller selects one, transfer the call to another number (sales agent). Replace xxxx-xxxx-xxxx with an actual phone number
            echo '<?xml version="1.0" encoding="UTF-8"?>
			<Response>
			<Dial>+2347052481090</Dial>
			</Response>';
        } elseif ($input == 2) {
            //if caller selects two, hangup the call
            echo '<?xml version="1.0" encoding="UTF-8"?>
			<Response>
			<Hangup/>
			</Response>';
        } else {
            //if caller selects anything else, redirect to main menu
            echo '<?xml version="1.0" encoding="UTF-8"?>
			<Response>
			<Say>Invalid Input</Say>
			<Redirect method="POST">' . base_url() . 'index.php/flow/start</Redirect>
			</Response>';
        }
    }
}

You’re almost done building your simple IVR system.

Part 4 – Setting Up Twilio

Create a Twilio account if you don’t already have an account and purchase a voice-enabled phone number. If you sign up for the free trial, you’ll get your first phone number for free.

Click the red + button to add one of these numbers to your account.

Part 5 – Setting Twilio Webhooks

A Webhook URL needs to be configured for your Twilio number to send HTTP requests and allow Twilio to respond to TwiML instructions from your web application during a call.

Click on one of your numbers in the console and configure the Voice URL to point to your app. 

In this case, your Voice URL would be https://ngrok_public_address/index.php/flow/start

With your Twilio number configured, you are now ready to receive calls and respond to callers’ input on your Twilio number.

Learning tools

More resources on building IVR systems in PHP can be found on the following sites:

https://www.twilio.com/docs/voice/quickstart/php

https://stackoverflow.com/questions/tagged/twilio-php

Learning Strategy

During the course of this project, I browsed through Twilio’s website for PHP tutorials on how to build voice applications using its web service APIs. I also searched for tutorials on exposing the localhost environment to the internet using Ngrok. In case you encounter any issue in the course of trying to set up your project, you should browse through the learning tools above and also check google for tutorials on Ngrok, Twilio and Codeigniter.

Reflective Analysis

I learnt how to expose the localhost web server  running on your local machine to the internet using Ngrok by telling it what port your web server is listening on. Ngrok generates a URL which can be used to listen to webhook events on your twilio phone number. With Ngrok, we don’t have to spend money trying to host our application on paid servers during the course of our IVR system development.

Conclusion & Next Steps

Now that you have set up and configured your Twilio number to respond to callers’ input, you can repeat the same and start building IVR systems for customer service, financial service, campaign broadcast and so on.  The next time you need an IVR you can skip all of the long setup steps and just:

  • Download CodeIgniter into a new folder.
  • Generate a Ngrok public address that routes to your localhost web server port.
  • Buy a new Twilio number and set up your webhook URL.
  • Write the logic for your IVR system on CodeIgniter

Project source code => https://github.com/learningdollars/adeoluwaakinsanya-IVR-with-Twilio-CodeIgniter-and-Ngrok

Time Report

It took me about 15 hours to do this project