While working on an application, we came up with a situation where we had a couple of dynamic questions, which need to be answered via IVR. We are using Twilio as our cloud communication platform.

What is IVR?

Interactive Voice Response(IVR) is a technology that allows a computer to interact with humans through the use of voice and DTMF tones input via a keypad. ( Source: Wikipedia)

Interactive Voice Response

What is Twilio?

Twilio is a cloud communications platform as a service company based in San Francisco, California. Twilio allows software developers to programmatically make and receive phone calls and send and receive text messages using its web service APIs.( Source: Power BI Geeks )

Here we are using twilio-ruby gem for IVR.

You can get the basic knowledge about IVR and implement it using Ruby on Rails with this link:

https://www.twilio.com/docs/voice/tutorials/ivr-phone-tree-ruby-rails

So let us start with the looping problem:

Approach 1:

We tried to use each loop, but it always returned after the first iteration and then didn’t go further for the second iteration.

So, we came up with the solution where we used the session variable for the dynamic looping in IVR.

Approach 2:

This is the complete flow diagram, how we achieved dynamic looping in IVR:

Flow Diagram Dynamic Looping in IVR

This process will keep on asking questions until all the questions are not answered. That’s how we achieved dynamic looping in IVR using Twilio.

So let us take one example. There is one app with multiple surveys.

Each survey has multiple questions.

We added two actions for handling this communication in a controller which will be used for asking and answering the single question.

So, what we are going to do is we will ask for the survey id from the caller, and on the basis of that, we will ask the question via IVR for that survey.

So in order to ask questions via looping, what we did, we made one hash that contains the key-pair value of question_id and question. Then we stored this hash into session[params[:Caller]].

Now we have the list of questions. So let us add it to params[:Caller]

Now we will pass redirect to the method which asks the questions.

Then we made one action for asking questions, where we picked the first key-pair value of the hash stored in the session and passed that value(question) to the action for answering.

After the question is answered we deleted that key-pair value from the hash stored in the session.

This procedure will make one loop of multiple actions of question-answer which will run until hash does not have any value(question).

Once the hash becomes empty, we clear the session and disconnect the call from the server-side.

Here is a complete controller file.

So,  This is how we can achieve Looping in IVR in case you are using Twilio.

Read Also: Integrating Google Cloud Vision API with Ruby for Image Object Detection

Click here for more details

Consulting is free – let us help you grow!