Search code examples
ivrvxmlvoicexml

How to loop to get multiple dynamic fields in VXML


How can I use starting and ending numbers to loop through fields saving the input to an array in VXML with or without PHP?

Psuedo code would be something like:

get startNo from caller
get endNo from caller
loop i from startNo to endNo
   audio prompt 'prompt' + i + '.wav'
   save input to results[i]
end loop
save results to database at end of loop or upon hangup

I know how to get the starting and ending numbers and how to save to the database, it is the loop that I'm not sure on.

Requirements / other notes:

  • A mix of VXML and PHP is fine, but I would prefer to keep most VXML in .VXML files to keep code completion in my editor
  • There could be up to 50 prompts (as required by business, don't ask me to redesign that)
  • The solution has to support the hangup event so if the connection is lost mid-way we can save the results that were gathered
  • Vendor-independent, VoiceXML 2.1 compliant

Solution

  • Your VXML could looks something like this:

    <?xml version="1.0" encoding="UTF-8"?> 
    <vxml version = "2.1"> 
    
    <var name="currNo" expr="startNo"/>
    <var name="userResponse"/>
    
    <form id="getUserInput"> 
    
       <field name="F_1" cond="currNo <= endNo"> 
         <grammar srcexpr="currNo + '.xml'" type="application/grammar-xml"/>
         <prompt>
           <audio expr="currNo + '.wav'"/>
         </prompt>
       </field>
       <filled>
          <assign name="userReponse" expr="F_1"/>
          <goto next="#handleResponse"/>
        </filled>
    </form>
    
    <form id="handleResponse">
       <block>
          <!-- Send each result to PHP app as you get them -->
          <!-- This way you will not loose any result on error or hangup -->
          <!-- Assumes you have some user ID to associate the result with -->
          <submit next="handleUserResponse.php" namelist="userId userResponse"/>
          <!-- Increment counter -->
          <assign name="currNo" expr="currNo + 1"/>
          <!-- If the counter is greater than endNo then we are done -->
          <if cond="currNo > endNo>
             <submit next="endApplication.vxml"/>
          <!-- Otherwise we loop back to get another response -->
          <else/>
              <goto next="#getUserInput"/>
          </if>
        </block>
    </form>
    </vxml>
    

    This gives you a good idea on how to handle the looping and submitting the results. I did not include any error handling.