TaCode Tuesdays: How to Build an Interactive Voice Response System (IVR) Using Zang [Part 2]

Posted by Alex Misevski on November 1, 2016


Welcome back to TaCode Tuesdays! This is the only place you can find snippets of code for use in your very own text/voice apps, along with a weekly dose of taco puns. I’m a developer here at Zang and not only am I a big fan of tacos (if that wasn’t already apparent), I’m also a fan of open source. My goal is to share a new app idea each week that you're free to use “as is” or modify and use as the basis for your next app.

Last week, I began detailing how to build an interactive voice response system (IVR) using Zang—you can check out PART 1 here and as always, if you’d like to learn how to get started on Zang, take a look at our very first post.

This week I’m going finish it out with Part 2, but before I get into it, let's get into our Taco Tip of the week!

The most important aspect of tacos is balancing the taste and textures of your ingredients. I find that combining tastes is easy—if you like how its flavored, add it in...no one's going to stop you! The part I struggled with when I first started to regularly make tacos was that they would come out a gloopy mess, but I've learned how to avoid it—texture. By that I mean, try adding thinly sliced onions, cabbage, radishes, or even pickles. Add them close to serving, so they retain their crunch and you'll notice just how much of a difference it can make.

Again, please check out PART 1 here before you continue reading - we'll pick up from step 3.

Now, Let’s Finish Up the App!

Step 3: Create a class that will render the menu items for your web service.
Inside the IVR class constructor, name the PHP native extension SimpleXML class. This provides a very simple and easily usable toolset for converting XML to an object that can be used with normal property selectors and array iterators. Keep in mind that the biggest difference between an XML and a PHP array is that in an XML file, the name elements can be the same as even if they’re connected or siblings; while in PHP array, the keys are not the same.
class IVR
//declare your ivr properties that will hold your menu
     public $xml_menu;
     public $xml_submenu;
     function __construct()
          //display main menu tree by creating an instance of the
               // SimpleXMLElement object
          $menu_tree = new SimpleXMLElement($this->xml_menu);
          echo $menu_tree->main_menu;
           //call this method for retrieving the information on your <gather> tag
           //example action=”http://www.mydomain.com/api/parse”
     function parse()
           $ch = $_GET['ch']; // where 'ch' is the value from the <gather> response xml tag thats been submitted
           switch ($ch) {
                case 1 : $this->GetCustomerService(); break;
                case 2 : $this->GetTechnicalSupport(); break;
                case 3 : $this->GetSalesAndMarketing(); break;
                case 4 : $this->GetExitIvrSession(); break;
//from the parse method above route to your desired xml rendering method basing from the switch statement.
     function GetCustomerService()
/* remember that SimpleXML will return a reference to an object containing the node value and you cant use reference in session variabls as there is no feasible way to restore a reference to another value.*/
     $menu_tree = new SimpleXMLElement($this->xml_submenu);
     echo $menu_tree->submenu;
     function invoke_agent() {
     //implement your call to agent
     //... implement your routing to different menu items
     function GetTechnicalSupport() {}
     function GetSalesAndMarketing() {}
     function GetExitIvrSession() {}
     //destroy all object properties
     //Be aware of potential memory leaks caused by circular references within objects.
     function __destruct() {
          $this->xml_menu = null ;
          $this->xml_submenu = null;
          echo __METHOD__ . PHP_EOL;
Step 4: Create the object and its implementation for your IVR class
This last step consolidates everything you’ve built so far. Create an object of the type IVR class and name it $ivr and then assign the variables $xml_menu and $xml_submenu into the two properties of the ivr class, namely xml_menu and xml_submenu. This will provide an output of your desired menu hierarchy from Figure 1.0 (that figure can be found in PART 1).
The final step is to call the IVR method display_menu to present the user with your menu items. And eventually call your custom invoke_agent method of the ivr class to route the necessary information for your call center agents.
$ivr = new IVR;
$ivr->xml_menu = $xml_menu;
$ivr->xml_submenu = $xml_submenu;
$ivr->display_menu(); // show menu items
$ivr->invoke_agent(); // route to agents
$ivr->exit(); // end session

Summing it Up

Creating a custom IVR, especially for companies that are just starting to build their call centers, could save a lot in capital investments. By utilizing Zang’s Say, Play, and Gather XML tags, all you need to do is follow three easy steps to implement them in PHP or the programming language of your choice. IVRs can help in managing a high volume of calls and improving call handling time and service level agreements. Rather than spending thousands of dollars in hardware, build one of your own so you can implement an IVR workflow that exactly fits your business process for only a fraction of the cost.

Well, that’s it for now! If you have any thoughts about the app or just want to share your own taco-related thoughts, you can comment below. If you want a reminder, sign up to get notifications of new blog posts. I'll be back in TWO weeks with another installment of TaCode Tuesday.

Topics: Communication Apps, Ideas, TaCode Tuesday, cPaaS